Skip to main content
Users
CloudPortalLogin
  • Powered byPowered by
Introduction to Sitecore CDP
Data availability
Managing your account
Managing system settings
Managing guests
Batch segmentation
Data browser
Audience export
Dashboards
Developer center
AI in CDP
Glossary
  • Sitecore CDP
  • AI in CDP
  • Code Assistant

Code Assistant

Note

Code Assistant is only available if Sitecore Stream is enabled for your organization.

Code Assistant is an AI-powered tool in CDP that understands the prompts you write in natural language, and responds with JavaScript code. The code is tailored to the specific CDP feature you use the assistant in, such as audience export, conditions, programmables, and session traits.

This topic describes how the Code Assistant works behind the scenes, and how you can best prompt it to enhance your work with JavaScript in CDP.

Data privacy and security

At Sitecore, data privacy and security are top priorities. Your data is never used to train the LLM, ensuring it remains secure and confidential. Our AI capabilities feature built-in AI guardrails to protect sensitive brand information throughout the marketing workflow. All brand documents and data from interactions with our AI-enabled tools are safeguarded under strict data privacy and security policies.

See the Gen AI FAQ for more information on the use of AI in Sitecore.

Prompting the Code Assistant

When you prompt the Code Assistant, it responds only with generated code using supported JavaScript functions. It doesn’t provide any natural language explanation or feedback. To generate the response, it considers the underlying data schema in your instance of CDP, the current prompt, and the code that's currently in your code editor, if present. It does not consider your prompt history or previous JavaScript in your code editor.

Therefore, when working with the assistant:

  • Do not copy and paste the current code into your prompt. If there’s code in your editor, the assistant uses it automatically.

  • Prompt the assistant to generate or modify code. The assistant is designed to output JavaScript, not engage in general conversation.

Note

The Code Assistant might make mistakes. Always check your code and test it in the applicable CDP feature before using it in production.

AI responses can vary. Running the same prompt more than once might produce different results.

Ideas for prompts

Here are a few prompt ideas for the Code Assistant.

Understand and refactor code

You can use the Code Assistant to explain and refactor your code, update the syntax to match your preferences, and add helpful logs to the CDP test console.

Prompt

Result

Add a code comment to every line to help me understand the code.

Updates the code with line-by-line comments so you can better understand what it does.

Include more logs in the code so I can review them during testing.

Updates the code with print() statements so you can trace code execution during testing in Test > Response > Logs.

Refactor the code to make it shorter.

Updates the code to be more concise.

  • Convert if-else statements to switches.

  • Convert arrow functions to traditional functions.

  • Simplify the for loop.

Updates the JavaScript syntax to help code readability, depending on your preferences.

  • Make this JavaScript compatible with ES5.

  • Make this JavaScript compatible with ES6.

Converts the code into an older or newer version of JavaScript syntax, depending on your preferences.

Audience export

You can use the Code Assistant in an audience export in the JavaScript code editor to compute data in addition to what's already in the guest profile. This lets you enrich the data that you export from Sitecore CDP.

Note

After generating code on the JavaScript tab, make sure to include the attributes in the output on the Output structure tab by using the ${jsResult.objectKeyName} notation.

Prompt

Result

Export number of sessions in last 30 days

Returns the number of sessions the guest has had in the past month.

Example output:

RequestResponse
{ "recentSessions": 4 }

Export the total value of the last 10 orders

Returns the total value of the guest's last 10 orders.

Example output:

RequestResponse
{ "totalOrderValue": 299 }

Export average order price

Returns the average price of all the guest's orders.

Example output:

RequestResponse
{ "averageOrderPrice": 64.88 }

Export customer loyalty points

Returns the guest's total number of loyalty points if your organization captures that information using data extensions.

Example output:

RequestResponse
{ "totalExternalLoyaltyPoints": 363 }

Conditions

You can use the Code Assistant to quickly create a condition to filter the audience by evaluating specific criteria.

Prompt

Result

Customer purchased in the past 2 days

Returns true if the customer has made a purchase within the last 48 hours; returns false otherwise.

Example output:

RequestResponse
{ false }

Returning customer

Returns true if the customer has at least one order; returns false if there are no past purchases.

Example output:

RequestResponse
{ true }

Customer in a segment

Returns true if the customer belongs to a specified segment; returns false if not.

Example output:

RequestResponse
{ false }

Purchase started but incomplete

Returns true if the guest started an order but the order status is not COMPLETED. Returns false if the order is completed or not started.

Example output:

RequestResponse
{ false }

Session traits

You can use the Code Assistant to easily create a session trait to retrieve and store session-based values for individual customers. You can enter a short prompt or be as descriptive as you like to define the session trait.

Prompt

Result

The most viewed page of the session

Returns the page viewed most often during the customer's session.

Example output:

RequestResponse
{ "cmp" }

Count the total view events

Returns the total number of page VIEW events during the customer's session.

Example output:

RequestResponse
{ 2 }

Last viewed page

Returns the last page the customer viewed during the session.

Example output:

RequestResponse
{ "/select" }

Store time in session

Returns the total time the customer spent during the session, from the first to the last event.

Example output:

RequestResponse
{ "0 hour(s), 0 minute(s), 15 second(s)"}

Store how many times a customer added something to their cart

Returns the number of ADD TO CART events in the session.

Example output:

RequestResponse
{ 1 }

Store the top 3 product categories viewed by the guest

Returns the top three product categories viewed during the session.

Example output:

RequestResponse
{ [ "undefined",  "select" ] }

Store the product type name of the item purchased in the last order

Returns the product type from the customer's most recent purchase.

Example output:

RequestResponse
{ "FLIGHT" }

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2026 Sitecore