Created
August 16, 2022 07:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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