First Look: Sitecore Personalize Code Assistant

45 min read

Published:

Hello friends! It's been a while since I last posted, but today I'm excited to share a first look at a newly released feature for Sitecore Personalize and CDP. This new feature combines my two favorite things—personalization and artificial intelligence (AI). It addresses one of the key challenges I've been trying to solve: the difficulty of building experiences and experiments with Sitecore CDP and Personalize.

What is the Code Assistant?

The Code Assistant is an AI-powered tool that integrates directly into Sitecore Personalize's code editing experience. It helps developers write code specifically within the Sitecore Personalize context. The product has traditionally required specific technologies and JavaScript variations for creating assets—a complexity that challenges even developers familiar with modern JavaScript.

This is where the new Code Assistant will provide significant value for developers. I believe this is just the beginning—there may be opportunities for marketers to use the code assistant to create experiences as well. Last year, I wrote an article Revolutionizing Sitecore Personalize: Unleashing the Power of Generative AI Function Calling on a similar topic, exploring how to use an LLM to generate experiences through prompts. While that approach had limitations and was intended as a proof of concept, it makes me excited to see these innovations coming to the product.

In this initial release, you can use the Code Assistant to craft code for Conditions (compatible with XM Cloud when used alongside Personalize), Session Traits (a recent addition), and Audience Export. You'll find it in any section that requires code writing. While Session Traits and Conditions are straightforward to use, for Audience Export you'll need to specify an output format using the Advanced option.

For more information about how to use the new feature, refer to the documentation or the release notes.

Hands-on Use Cases

Working with Conditions

In my exploration of the Code Assistant, I tested its capabilities with conditions and discovered several key features:

  • The assistant analyzes existing code and suggests performance improvements
  • It provides a code output view with copy and replace options
  • Though it can refactor code to improve readability (such as creating separate functions), performance gains are often modest
  • I encountered a few limitations:
    • The assistant lacks conversational feedback—more context around its suggestions would be helpful
    • When seeking code improvements, I had to manually compare the full suggestion against my existing code

I tested a use case for a Condition that would determine the user's state—specifically whether they were a Visitor or Known user. I provided this prompt to the Code Assistant: Create a Condition that determines the user guest Type. It should allow me to specify the type as a parameter and then returns true or false if the user has that type. Here's the snippet it generated:

1(function() {
2 var specifiedGuestType = "[[guest type | enum(customer, visitor, member) | customer | { required: true }]]";
3 var returnValue = false;
4
5 if (guest && typeof guest.guestType === "string") {
6 returnValue = (guest.guestType === specifiedGuestType);
7 }
8
9 return returnValue;
10})();

The output isn't perfect—member and customer aren't valid types, and it doesn't generate a description (such as "Is the current visitor [guesttype]"). Like with all AI tools, we can't blindly trust its suggestions, but it does provide a solid starting point.

Session Traits Implementation

One of the most valuable applications I discovered was creating session traits. Let me show you an example of how the assistant helped develop a keyword tracking system:

1(function ai_generated_function() {
2 var keywordTracker = {};
3
4 for (var i = 0; i < triggerSession.events.length; i++) {
5 var event = triggerSession.events[i];
6
7 if (event.type === 'VIEW' && event.arbitraryData.keywords) {
8 var keywords = event.arbitraryData.keywords.split(',');
9
10 for (var j = 0; j < keywords.length; j++) {
11 var keyword = keywords[j].trim();
12 keywordTracker[keyword] = (keywordTracker[keyword] || 0) + 1;
13 }
14 }
15 }
16
17 var sortableKeywords = [];
18 for (var key in keywordTracker) {
19 sortableKeywords.push([key, keywordTracker[key]]);
20 }
21
22 sortableKeywords.sort(function(a, b) {
23 return b[1] - a[1];
24 });
25
26 var topFiveKeywords = sortableKeywords.slice(0, 5).map(function(keywordArray) {
27 return keywordArray[0];
28 });
29
30 print("Top five keywords: " + topFiveKeywords);
31 return topFiveKeywords.length > 0 ? topFiveKeywords : null;
32})();
33

I haven't used Session Traits before myself, but here's what they do: they let you collect metrics from the current viewing session and tracked events, then use JavaScript to store specific data points back to the guest's profile. For example, you could analyze a user's entire viewing history from their last session to make determinations about them. This capability would greatly enhance the behavioral personalization strategies I covered in my previous article—I'll need to update that piece to incorporate these Session Traits.

Audience Export

As mentioned, the Code Assistant also supports formatting output for Audience Export. While I haven't extensively tested this feature yet, I wanted to note its availability. I plan to cover Audience Exports more thoroughly in a future blog post.

Limitations and Areas for Improvement

  • The chat interface needs to be closed and reopened to start new sessions
    • However, this limitation may not be significant since each additional message only considers your current code state and prompt, rather than the entire conversation context.
  • I encountered several cases where the tool's performance fell short, which I won't detail in this post. However, the good news is that as AI models improve, these limitations should become less frequent. Also I know they ground the model suggestions as well, so as they fine-tune the output, these will also decrease these scenarios.
  • As mentioned previously, it would be helpful to have a way to merge the suggested code snippets with existing code.

Conclusion

The Sitecore Personalize Code Assistant marks a significant advancement in simplifying personalization development. Though it has current limitations, the tool shows great potential for speeding up code creation and boosting developer efficiency. As it matures, it's likely to become essential to the Sitecore Personalize development process—especially for developers working with conditions and session traits.

General

Home
About

Stay up to date


© 2025 All rights reserved.