Code snippets
Here's a list of JavaScript functions you can use as a starting point for writing the code for session traits.
Trigger session function
This function is designed to identify the specific sessions that triggered the calculation of a session trait for a customer. Instead of manually searching and iterating over all events in the current session, you can use this function to directly access the events that are relevant to your calculations. It simplifies the process by returning an array of event types that initiated the session trait calculation.
(function () {
return triggerSession.events.map(e => e.type);
})();For example, if the session trait you created is triggered by events like VIEW, CLICK, and SUBMIT, the function will return an array containing these values: [ "VIEW", "CLICK", "SUBMIT"].
You can use this function whenever you need to identify the triggering sessions, making it an efficient and versatile tool, as demonstrated in the following code snippets.
Count a customer's page views
This function counts the number of page VIEW events in the customer's session. Using the trigger session function, it loops through the session events and increments a counter each time it finds a matching VIEW event. Once the loop is complete, it returns the total count of VIEW events. If no VIEW events are found, it returns 0.
(function() {
var counter = 0;
if (triggerSession && Array.isArray(triggerSession.events)) {
for (var i = 0; i < triggerSession.events.length; i++) {
if (triggerSession.events[i].type === 'VIEW') {
counter++;
}
}
}
return counter;
})();Identify the last page viewed by a customer
This function identifies the last page viewed by a customer based on the recorded events in the customer's session. Using the trigger session function, it loops through the session events for a matching VIEW event. If a matching VIEW event is found, it returns the page associated with that event. If no VIEW events are found, the function returns the string "No VIEW Events".
(function () {
if (triggerSession && Array.isArray(triggerSession.events)) {
for (var i = 0; i < triggerSession.events.length; i++) {
var event = triggerSession.events[i];
if (event.type === 'VIEW') {
return event.arbitraryData.page;
}
}
}
return "No VIEW Events";
})(); Check if a customer has viewed the terms and conditions page
This function checks the customer's current session events to determine if the POLICY page (which represents the terms and conditions) has been viewed. It loops through all events in the session using using the trigger session function.. If a VIEW event for the POLICY page is found, it returns 1. Otherwise, it returns 0, indicating that the page has not been viewed during the session.
(function () {
var viewedPolicy = 0;
if (triggerSession && Array.isArray(triggerSession.events)) {
for (var i = 0; i < triggerSession.events.length; i++) {
var event = triggerSession.events[i];
if (
event.type === 'VIEW' &&
event.arbitraryData &&
event.arbitraryData.page === 'POLICY'
) {
viewedPolicy = 1;
break; // stop checking once found
}
}
}
return viewedPolicy;
})();
Check if a customer has viewed a specific banner
This function checks if the customer has viewed a specific banner during their session. Using the trigger session function, it loops through the session events and looks for a VIEW event that matches the specified banner reference. If a matching event is found, the function returns true, indicating that the banner was viewed. If no matching event is found, it returns false.
(function () {
// Replace with the actual banner reference
var bannerRef = "/banners/special-offer";
if (triggerSession && Array.isArray(triggerSession.events)) {
for (var i = 0; i < triggerSession.events.length; i++) {
var event = triggerSession.events[i];
if (event.type === 'VIEW' && event.arbitraryData.page === bannerRef) {
return true;
}
}
}
return false;
})(); Capture customer preferences
This function captures and stores customer preferences from the session data. It initializes a userPreferences object with default values, such as empty strings for t-shirt size and pants sizes. Then, using the trigger session function, it loops through the session events and updates the preferences accordingly. For example, if a CLICK event is found for the t-shirt size page, the function updates the tshirtSize value. Finally, it returns a concatenated string with the user preferences.
(function () {
// Initialize an object to store user preferences with empty strings instead of null
var userPreferences = {
language: triggerSession.language,
currency: triggerSession.currency,
tshirtSize: "",
pantsSize: ""
};
if (triggerSession && Array.isArray(triggerSession.events)) {
for (var i = 0; i < triggerSession.events.length; i++) {
var event = triggerSession.events[i];
if (event.type === 'CLICK' && event.arbitraryData.page === 'tShirtSize') {
userPreferences.tshirtSize = event.arbitraryData.value;
}
if (event.type === 'CLICK' && event.arbitraryData.page === 'pantsSize') {
userPreferences.pantsSize = event.arbitraryData.value;
}
}
}
// Log the final user preferences
console.log('User preferences:');
console.log('Language: ' + userPreferences.language);
console.log('Currency: ' + userPreferences.currency);
console.log('T-shirt size: ' + userPreferences.tshirtSize);
console.log('Pants size: ' + userPreferences.pantsSize);
// Return a concatenated string of the user preferences instead of an object
return "Language: " + userPreferences.language +
", Currency: " + userPreferences.currency +
", T-shirt size: " + userPreferences.tshirtSize +
", Pants size: " + userPreferences.pantsSize;
})();