Skip to content

Instantly share code, notes, and snippets.

@DaveGoosem
Created August 16, 2022 07:17
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 DaveGoosem/aba3f5af47bee47669b1d3583beaca61 to your computer and use it in GitHub Desktop.
Save DaveGoosem/aba3f5af47bee47669b1d3583beaca61 to your computer and use it in GitHub Desktop.
For usage with Sitecore CDP/Personalize - access some properties added to your page view events for UTM attribution to use with campaigns in CDP
// Use server-side JavaScript to filter your audience further
// You have full access to the guest context which can be accessed under guest, e.g. guest.email
// Any truthy return value will pass the audience filter, it is recommended to return an object
// The value returned can be accessed from the variant API response as 'filter'
(function () {
var converted = "[[Has/Has not | enum(has, has not) | has | { required: true }]]";
var campaignName = "[[Campaign(s) | multiselect() | | { required: true }]]";
var campaignValues = campaignName.split(',');
var expectedType = "WEB";
var expectedStatus = "OPEN";
var campaignMatch = false;
if (guest && guest.sessions && guest.sessions.length > 0) {
loop:
for (var i = 0; i < guest.sessions.length; i++) {
if (guest.sessions[i]) {
if (
guest.sessions[i].sessionType !== expectedType
|| guest.sessions[i].status !== expectedStatus
) {
continue loop;
} else if (
guest.sessions[i].events
) {
for (var j = 0; j < guest.sessions[i].events.length; j++) {
if (guest.sessions[i].events[j].arbitraryData) {
if (guest.sessions[i].events[j].arbitraryData.utm_campaign) {
var campaign = guest.sessions[i].events[j].arbitraryData.utm_campaign;
for (var k = 0; k < campaignValues.length; k++) {
if (campaign === campaignValues[k]) {
campaignMatch = true;
break loop;
}
}
}
}
}
}
}
}
}
return (converted === "has") && campaignMatch || (converted === "has not") && !campaignMatch;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment