---
title: "Defining, storing, and accessing session traits"
slug: "defining-storing-and-accessing-session-traits"
abstract: "How to use JavaScript to define the logic to retrieve session-specific values, store them within the guest context, and access them within Sitecore CDP."
---
# 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](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/create-a-session-trait.html "Create a session trait") 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](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/create-a-session-trait.html "Create a session trait") to track a customer's page views. Here's an example JavaScript snippet to [count page view events](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/code-snippets-1481735.html#UUID-1443721e-1d90-6001-5ab4-70083c02890f_section-idm234572777668165 "Count a customer's page views"):

```javascript
(function () {
  var counter = 0;
  triggerSession.events.forEach(event => {
    if (event.type == "VIEW") {
      counter++;
    }
  })
  return counter;
})();

```

When you [activate](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/activate-a-session-trait.html "Activate a session trait") 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](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/session-trait-limitations.html "Session trait limitations") stored. Here's an example of a guest context with a `traits` object:

```json
{
    "email": "jane_doe@sitecore.com",
    "emails": [
        "jane_doe@sitecore.com"
    ],
    "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](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/create-a-session-trait.html "Create a session trait"), 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](urn:resource:component:1386699).

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](manage-session-traits.html#UUID-d6fd963c-d749-e238-9bd3-af2cff77e8fd_section-idm234411970915559 "Copy the friendly ID of a session trait")` to retrieve its stored value calculated at the end of the customer's session.