Skip to content

Instantly share code, notes, and snippets.

@jfkhoury
Last active November 22, 2021 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfkhoury/b390688267854c75d75245ed0c12253b to your computer and use it in GitHub Desktop.
Save jfkhoury/b390688267854c75d75245ed0c12253b to your computer and use it in GitHub Desktop.
/**
* Default all events to `LinkClick`, unless the event is a Page View:
*
* NOTE: This script will only execute if the `web.webInteraction.type` was not already set.
*
* Here's what constitutes a Page View:
*
* 1. If the `Event Type` is: web.webpagedetails.pageViews
* 2. If the `Page View` metric XDM FieldGroup is populated: `xdm.web.webPageDetails.pageViews.value`
*
* Here's how this script transforms an event to `LinkClick`:
*
* 1. It sets the `xdm.web.webInteraction.type: "other"` field.
* 2. It sets the `xdm.web.webInteraction.name: "..."` field.
*
* The name is set as follows:
*
* 1. If the `Event Type` is `display` or `click; name becomes: `ADBE-A4T:display` or `ADBE-A4T:click`
* 2. Otherwise, the `name` is set as the `Event Type`.
*
* version: 1.0
*/
// This is a list of events we consider a page view:
// By default, this script considers "web.webpagedetails.pageViews" is a page view event type.
// Feel free to add other event types to this array if you decide to use a custom type as page view.
var pageViewEventTypes = ["web.webpagedetails.pageViews"];
function isWebInteractionTypeSet() {
try {
return Boolean(content.xdm.web.webInteraction.type);
} catch (ex) {
return false;
}
}
// This helper looks into the event type OR the page view metric to decide of the event is a page view.
function isPageViewEvent(eventType) {
var isPageViewEventType =
eventType && pageViewEventTypes.indexOf(eventType) !== -1;
var isPageViewMetricSet;
try {
isPageViewMetricSet = content.xdm.web.webPageDetails.pageViews.value > 0;
} catch (ex) {
isPageViewMetricSet = false;
}
return isPageViewEventType || isPageViewMetricSet;
}
function convertEventToLinkClick() {
var eventType = content.xdm.eventType;
if (!content.xdm.web) {
content.xdm.web = {};
}
if (!content.xdm.web.webInteraction) {
content.xdm.web.webInteraction = {};
}
content.xdm.web.webInteraction.type = "other";
content.xdm.web.webInteraction.name = "ExperienceEventType:" + (eventType || "NONE");
}
function shouldConvertEventToLinkClick(eventType) {
// We won't convert the event to link click if its type if considered a `Page View`.
// By default, "web.webpagedetails.pageViews" is considered a `Page View`.
if (isPageViewEvent(eventType)) {
return false;
}
if (!isWebInteractionTypeSet()) {
return true;
}
}
if (shouldConvertEventToLinkClick(content.xdm.eventType)) {
convertEventToLinkClick();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment