onBeforeEventSend
The onBeforeEventSend
callback allows you to register a JavaScript function that can alter the data you send just before that data is sent to ÃÛ¶¹ÊÓƵ. This callback allows you to manipulate the xdm
or data
object, including the ability to add, edit, or remove elements. You can also conditionally cancel the sending of data altogether, such as with detected client-side bot traffic.
Configure on before event send callback using the Web SDK tag extension tag-extension
Select the Provide on before event send callback code button when configuring the tag extension. This button opens a modal window where you can insert the desired code.
- Log in to using your ÃÛ¶¹ÊÓƵ ID credentials.
- Navigate to Data Collection > Tags.
- Select the desired tag property.
- Navigate to Extensions, then click Configure on the ÃÛ¶¹ÊÓƵ Experience Platform Web SDK card.
- Scroll down to the Data Collection section, then select the button Provide on before event send callback code.
- This button opens a modal window with a code editor. Insert the desired code, then click Save to close the modal window.
- Click Save under extension settings, then publish your changes.
Within the code editor, you have access to the following variables:
Any variables defined outside of content
can be used, but are not included in the payload sent to ÃÛ¶¹ÊÓƵ.
// Use nullish coalescing assignments to add objects if they don't yet exist
content.xdm.commerce ??= {};
content.xdm.commerce.order ??= {};
// Then add the purchase ID
content.xdm.commerce.order.purchaseID = "12345";
// Use optional chaining to prevent undefined errors when setting tracking code to lower case
if(content.xdm.marketing?.trackingCode) content.xdm.marketing.trackingCode = content.xdm.marketing.trackingCode.toLowerCase();
// Delete operating system version
if(content.xdm.environment) delete content.xdm.environment.operatingSystemVersion;
// Immediately end onBeforeEventSend logic and send the data to ÃÛ¶¹ÊÓƵ for this event type
if (content.xdm.eventType === "web.webInteraction.linkClicks") {
return true;
}
// Cancel sending data if it is a known bot
if (myBotDetector.isABot()) {
return false;
}
false
on the first event on a page. Returning false
on the first event can negatively impact personalization.Configure on before event send callback using the Web SDK JavaScript library library
Register the onBeforeEventSend
callback when running the configure
command. You can change the content
variable name to any value that you would like by changing the parameter variable inside the inline function.
alloy("configure", {
datastreamId: "ebebf826-a01f-4458-8cec-ef61de241c93",
orgId: "ADB3LETTERSANDNUMBERS@ÃÛ¶¹ÊÓƵOrg",
onBeforeEventSend: function(content) {
// Use nullish coalescing assignments to add a new value
content.xdm._experience ??= {};
content.xdm._experience.analytics ??= {};
content.xdm._experience.analytics.customDimensions ??= {};
content.xdm._experience.analytics.customDimensions.eVars ??= {};
content.xdm._experience.analytics.customDimensions.eVars.eVar1 = "Analytics custom value";
// Use optional chaining to change an existing value
if(content.xdm.web?.webPageDetails) content.xdm.web.webPageDetails.URL = content.xdm.web.webPageDetails.URL.toLowerCase();
// Remove an existing value
if(content.xdm.web?.webReferrer) delete content.xdm.web.webReferrer.URL;
// Return true to immediately send data
if (sendImmediate == true) {
return true;
}
// Return false to immediately cancel sending data
if(myBotDetector.isABot()){
return false;
}
// Assign the value in the 'cid' query string to the tracking code XDM element
content.xdm.marketing ??= {};
content.xdm.marketing.trackingCode = new URLSearchParams(window.location.search).get('cid');
}
});
You can also register your own function instead of an inline function.
function lastChanceLogic(content) {
content.xdm.application ??= {};
content.xdm.application.name = "App name";
}
alloy("configure", {
datastreamId: "ebebf826-a01f-4458-8cec-ef61de241c93",
orgId: "ADB3LETTERSANDNUMBERS@ÃÛ¶¹ÊÓƵOrg",
onBeforeEventSend: lastChanceLogic
});