---
title: "Create a session trait"
slug: "create-a-session-trait"
abstract: "How to create a JavaScript-based session trait in Sitecore CDP to capture and calcluate attributes from events using custom JavaScript."
---
# Create a session trait

Sitecore CDP lets you create session traits that capture attributes from events, perform calculations on these attributes at the end of a customer's session, and store the results in their guest profile. You can customize the logic of a session trait using JavaScript to meet your specific business needs.

To create a session trait:

1. On the navigation pane, click **Developer center** > **Session traits**.
2. Click **Create**.
3. Enter a name for the new session trait. Sitecore CDP automatically creates a `[friendlyId](manage-session-traits.html#UUID-d6fd963c-d749-e238-9bd3-af2cff77e8fd_section-idm234411970915559 "Copy the friendly ID of a session trait")` when you name the session trait. Click **Create**.

    > [!note]
    > You can modify the `friendlyId` while creating the session trait, but after the session trait is activated, the ID can no longer be changed. The `friendlyId` identifies the session trait in the guest profile, and you must reference this ID to [access the session trait value](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/defining,-storing,-and-accessing-session-traits.html#UUID-79889d12-06ee-5109-5ac2-083d17151698_section-idm234360237762829 "Accessing session trait values in Sitecore CDP").

4. In the **JavaScript** section, click **Start coding**.
5. In the code editor, enter the custom JavaScript code that you want to use to capture the session trait. You can use the provided [sample code snippets](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/code-snippets-1481735.html "Code snippets") as a starting point for writing the JavaScript needed for your session traits.

    ![Write a custom JavaScript code in the code editor to define the value you want to capture in the session trait.](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/SessionTraitJScodecustom.png)

6. To insert a guest data model attribute or path into your code, on the **Data** tab, click ![Edit icon](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/edit_pencil.png) and do one of the following:
    - To select a random guest based on guest type, click in the search box and select **Visitor** or **Customer**.
    - To select a specific guest, enter their unique identifier into the search box. You can search for a guest using, for example, an email address, other unique identifier, or browser ID.

    After selecting a guest, in the **Guest data model** tree, click ![Copy icon](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/copy_data.png) next to the attribute you want to copy into your code and click **Copy Path as JavaScript**.

    > [!tip]
    > If there is a guest whose data you often use when configuring or testing code, click the **Bookmark** ![bookmark.png](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/bookmark.png) icon so you no longer have to search for them.

7. To automatically format and indent the JavaScript code in the code editor, click ![Format icon](https://doc.sitecore.com/api/markdown-image/cdp/en/users/sitecore-cdp/images/preetify.png).
8. Click **Save**.

    > [!important]
    > We recommend that you [test the session trait](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/test-a-session-trait.html "Test a session trait") before you [activate](https://doc.sitecore.com/cdp/en/users/sitecore-cdp/developer-center/session-traits/activate-a-session-trait.html "Activate a session trait") it.