Defining, storing, and accessing session traits
This topic explains how to capture and store session traits for guests identified as customers. It includes defining the logic of a session trait using JavaScript to retrieve and calculate attributes, storing them against a guest, and accessing their values within Sitecore CDP.
Defining a session trait with JavaScript
Session traits are defined using a JavaScript code that runs at the end of each customer's web session. The custom code calculates a specific value based on all events in a web session and available customer data, such as guest properties, data extensions, and order data.
For example, you might create a session trait to track a customer's page views. Here's an example JavaScript snippet to count page view events:
(function () {
var counter = 0;
triggerSession.events.forEach(event => {
if (event.type == "VIEW") {
counter++;
}
})
return counter;
})();
When you activate a session trait, the JavaScript code runs at the end of each customer's web session to apply the custom logic and calculate the desired value. In this example, it retrieves the number of pages viewed by a customer and stores it for later use.
Storing a session trait against a guest
The value captured by a session trait is stored as an attribute within a traits object at the root level of the guest context. Each customer can have up to 10 session traits stored. Here's an example of a guest context with a traits object:
{
"email": "[email protected]",
"emails": [
"[email protected]"
],
"ref": "99378e77-34e8-477d-bc82-4951082fc1f0",
"clientKey": "<client_key>",
"createdAt": "2024-02-07T09:21:57.764Z",
"modifiedAt": "2024-02-07T09:24:13.295Z",
"title": "Ms",
"firstName": "Jane",
"lastName": "Doe",
"identifiers": [],
"firstSeen": "2024-02-07T09:21:57.764Z",
"lastSeen": "2024-02-07T09:21:57.764Z",
"guestType": "customer",
"subscriptions": [],
"dataExtensions": [],
"sessions": [],
"orders": [],
"segmentMemberships": [],
"traits": {
"session": {
"count_views": {
"name": "Count_views",
"createdAt": "2024-07-26T10:38:39.688Z",
"value": 4,
"modifiedAt": "2024-07-26T10:44:58.788Z"
},
"countpageviews": {
"name": "Count-page-views",
"createdAt": "2024-07-26T10:38:39.688Z",
"value": 4,
"modifiedAt": "2024-07-26T10:44:58.788Z"
}
}
}Within the traits object, a session sub-object stores all session traits related to the customer. Each session trait object is uniquely identified by its friendlyId, which is automatically generated upon its creation, and includes the following attributes:
-
name- a user-defined name for the session trait upon its creation. -
createdAt- an automatically generated timestamp indicating when the session trait was created. -
modifiedAt- an automatically generated timestamp indicating when the session trait was last modified. -
value- a calculated session trait value, which can be a string, number, Boolean, or list of strings, depending on the JavaScript logic.
Session traits are sorted in descending order within the traits object based on their modifiedAt timestamp, with the most recently updated session trait positioned at the top of the traits object.
Accessing session trait values in Sitecore CDP
To access a customer's session traits in CDP, you can view their guest profile and access guest properties.
To use or retrieve the stored value of a session trait within Sitecore CDP, you can reference its value from the guest context using its known structure. For example:
guest.traits.session.sessionTrait1.value.
Replace sessionTrait1 with the session trait's friendlyId to retrieve its stored value calculated at the end of the customer's session.