Skip to content

Commit

Permalink
#30443 adding the event source to the code, rest, wf and rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Oct 31, 2024
1 parent bd454e6 commit 15ad451
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ public CollectorPayloadBean collect(final CollectorContextMap collectorContextMa

// Include default value for other boolean fields in the Clickhouse table
collectorPayloadBean.put(COME_FROM_VANITY_URL, false);
collectorPayloadBean.put(ISEXPERIMENTPAGE, false);
collectorPayloadBean.put(ISTARGETPAGE, false);
collectorPayloadBean.put(IS_EXPERIMENT_PAGE, false);
collectorPayloadBean.put(IS_TARGET_PAGE, false);

if (Objects.nonNull(collectorPayloadBean.get(EVENT_SOURCE))) {
// this is the default event source
collectorPayloadBean.put(EVENT_SOURCE, EventSource.DOT_CMS.getName());
}

return collectorPayloadBean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public interface Collector {
String PERSONA = "persona";
String RENDER_MODE = "renderMode";
String COME_FROM_VANITY_URL = "comeFromVanityURL";
String ISEXPERIMENTPAGE = "isexperimentpage";
String ISTARGETPAGE = "istargetpage";
String IS_EXPERIMENT_PAGE = "isexperimentpage";
String IS_TARGET_PAGE = "istargetpage";
String URL = "url";
String HOST = "host";
String LANGUAGE = "language";
Expand All @@ -47,6 +47,7 @@ public interface Collector {
String VANITY_QUERY_STRING = "vanity_query_string";
String RESPONSE = "response";
String LANGUAGE_ID = "language_id";
String EVENT_SOURCE = "event_source";

/**
* Test if the collector should run
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dotcms.analytics.track.collectors;

/**
* Enum for the Event Sources
* @author jsanca
*/
public enum EventSource {

DOT_CMS("DOT_CMS"),
REST_API("REST_API"),
WORKFLOW("WORKFLOW"),
RULE("RULE");

private final String name;

EventSource(final String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.dotcms.analytics.content.ContentAnalyticsAPI;
import com.dotcms.analytics.content.ReportResponse;
import com.dotcms.analytics.model.ResultSetItem;
import com.dotcms.analytics.track.collectors.Collector;
import com.dotcms.analytics.track.collectors.EventSource;
import com.dotcms.analytics.track.collectors.WebEventsCollectorServiceFactory;
import com.dotcms.analytics.track.matchers.UserCustomDefinedRequestMatcher;
import com.dotcms.rest.InitDataObject;
Expand All @@ -20,6 +22,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -237,7 +240,13 @@ public ResponseEntityStringView fireUserCustomEvent(@Context final HttpServletRe
DotPreconditions.checkNotNull(userEventPayload.get("event_type"), IllegalArgumentException.class, "The 'event_type' field is required");
Logger.debug(this, ()->"Creating an user custom event with the payload: " + userEventPayload);
request.setAttribute("requestId", Objects.nonNull(request.getAttribute("requestId")) ? request.getAttribute("requestId") : UUIDUtil.uuid());
WebEventsCollectorServiceFactory.getInstance().getWebEventsCollectorService().fireCollectorsAndEmitEvent(request, response, USER_CUSTOM_DEFINED_REQUEST_MATCHER, userEventPayload);

final Map<String, Serializable> userEventPayloadWithDefaults = new HashMap<>(userEventPayload);
userEventPayloadWithDefaults.put(Collector.EVENT_SOURCE, EventSource.REST_API.getName());

WebEventsCollectorServiceFactory.getInstance().getWebEventsCollectorService().fireCollectorsAndEmitEvent(request, response,
USER_CUSTOM_DEFINED_REQUEST_MATCHER, userEventPayloadWithDefaults);

return new ResponseEntityStringView("User event created successfully");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dotmarketing.portlets.rules.actionlet;

import com.dotcms.analytics.track.collectors.Collector;
import com.dotcms.analytics.track.collectors.EventSource;
import com.dotcms.analytics.track.collectors.WebEventsCollectorService;
import com.dotcms.analytics.track.collectors.WebEventsCollectorServiceFactory;
import com.dotcms.analytics.track.matchers.UserCustomDefinedRequestMatcher;
Expand Down Expand Up @@ -36,10 +38,6 @@ public class RuleAnalyticsFireUserEventActionlet extends RuleActionlet<RuleAnaly
public static final String RULE_OBJECT_ID = "objectId";
public static final String RULE_REQUEST_ID = "requestId";
public static final String RULE_CONTENT = "CONTENT";
public static final String RULE_ID = "id";
public static final String RULE_OBJECT_CONTENT_TYPE_VAR_NAME = "object_content_type_var_name";
public static final String RULE_OBJECT = "object";
public static final String RULE_EVENT_TYPE1 = "event_type";

public RuleAnalyticsFireUserEventActionlet() {
this(WebEventsCollectorServiceFactory.getInstance().getWebEventsCollectorService());
Expand Down Expand Up @@ -70,12 +68,13 @@ public boolean evaluate(final HttpServletRequest request, final HttpServletRespo
final HashMap<String, String> objectDetail = new HashMap<>();
final Map<String, Serializable> userEventPayload = new HashMap<>();

userEventPayload.put(RULE_ID, Objects.nonNull(instance.objectId) ? instance.objectId : identifier);
userEventPayload.put(Collector.ID, Objects.nonNull(instance.objectId) ? instance.objectId : identifier);

objectDetail.put(RULE_ID, identifier);
objectDetail.put(RULE_OBJECT_CONTENT_TYPE_VAR_NAME, Objects.nonNull(instance.objectType) ? instance.objectType : RULE_CONTENT);
userEventPayload.put(RULE_OBJECT, objectDetail);
userEventPayload.put(RULE_EVENT_TYPE1, instance.eventType);
objectDetail.put(Collector.ID, identifier);
objectDetail.put(Collector.CONTENT_TYPE_VAR_NAME, Objects.nonNull(instance.objectType) ? instance.objectType : RULE_CONTENT);
userEventPayload.put(Collector.OBJECT, objectDetail);
userEventPayload.put(Collector.EVENT_TYPE, instance.eventType);
userEventPayload.put(Collector.EVENT_SOURCE, EventSource.RULE.getName());
webEventsCollectorService.fireCollectorsAndEmitEvent(request, response, USER_CUSTOM_DEFINED_REQUEST_MATCHER, userEventPayload);

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dotmarketing.portlets.workflows.actionlet;

import com.dotcms.analytics.track.collectors.Collector;
import com.dotcms.analytics.track.collectors.EventSource;
import com.dotcms.analytics.track.collectors.WebEventsCollectorService;
import com.dotcms.analytics.track.collectors.WebEventsCollectorServiceFactory;
import com.dotcms.analytics.track.matchers.UserCustomDefinedRequestMatcher;
Expand Down Expand Up @@ -36,10 +38,6 @@ public class AnalyticsFireUserEventActionlet extends WorkFlowActionlet {
public static final String OBJECT_ID = "objectId";
public static final String REQUEST_ID = "requestId";
public static final String CONTENT = "CONTENT";
public static final String ID = "id";
public static final String OBJECT_CONTENT_TYPE_VAR_NAME = "object_content_type_var_name";
public static final String OBJECT = "object";
public static final String EVENT_TYPE1 = "event_type";
public static final String EVENT_TYPE_DISPLAY = "Event type";
public static final String OBJECT_TYPE_DISPLAY = "Object type";
public static final String OBJECT_ID_DISPLAY = "Object ID";
Expand Down Expand Up @@ -95,12 +93,13 @@ public void executeAction(final WorkflowProcessor processor,
final HashMap<String, String> objectDetail = new HashMap<>();
final Map<String, Serializable> userEventPayload = new HashMap<>();

userEventPayload.put(ID, UtilMethods.isSet(objectId) ? objectId.trim() : identifier);
userEventPayload.put(Collector.ID, UtilMethods.isSet(objectId) ? objectId.trim() : identifier);

objectDetail.put(ID, identifier);
objectDetail.put(OBJECT_CONTENT_TYPE_VAR_NAME, UtilMethods.isSet(objectType) ? objectType.trim() : CONTENT);
userEventPayload.put(OBJECT, objectDetail);
userEventPayload.put(EVENT_TYPE1, UtilMethods.isSet(eventType)? eventType.trim(): eventType);
objectDetail.put(Collector.ID, identifier);
objectDetail.put(Collector.CONTENT_TYPE_VAR_NAME, UtilMethods.isSet(objectType) ? objectType.trim() : CONTENT);
userEventPayload.put(Collector.OBJECT, objectDetail);
userEventPayload.put(Collector.EVENT_SOURCE, EventSource.WORKFLOW.getName());
userEventPayload.put(Collector.EVENT_TYPE, UtilMethods.isSet(eventType)? eventType.trim(): eventType);
webEventsCollectorService.fireCollectorsAndEmitEvent(request, response, USER_CUSTOM_DEFINED_REQUEST_MATCHER, userEventPayload);
} else {
Logger.warn(this, "The request or response is null, can't send the event for the contentlet: " + identifier);
Expand Down

0 comments on commit 15ad451

Please sign in to comment.