Building Custom Decision Templates in Sitecore Personalize: A Step-by-Step Guide

 Introduction

In Sitecore Personalize, decision templates are a powerful way to configure and reuse programmable decision logic. By creating decision templates, marketers can apply predefined logic directly onto the decision canvas without writing or modifying code. This flexibility helps non-technical users craft personalized experiences while enabling developers to write complex logic in the backend.

In this blog post, we’ll walk through how to create a decision template that takes dynamic input parameters and returns the number of times a particular event occurred for a guest in Sitecore Customer Data Platform (CDP). We’ll use parameters like page name, event type, last days, and channel to track the event occurrences.

Use Case: Tracking Event Occurrences

Let’s say you need to find how many times a specific event occurred for a guest, such as when a user submits a form or clicks on a button. The number of occurrences will be filtered by specific parameters such as:

  • Page Name – The page on which the event is triggered.

  • Last Days – The number of days in the past to filter the event occurrences.

  • Event Type – The type of event you want to track (e.g., "Form Submitted").

  • Channel – The communication channel through which the event was triggered (e.g., Web, Email, SMS).

We will use these parameters to create a dynamic form and a decision template that returns the number of events triggered for the selected criteria.

Steps to Achieve the Solution

Step 1: Create the Decision Template

  1. Log in to Sitecore Personalize and navigate to Decision Templates.

  2. Create a new decision template or modify an existing one to include your custom logic.For my demo purpose the name is test

  3. Once the template is created, go to the JavaScript tab to input the custom script.



Step 2: Script for Dynamic Form Input

The goal is to build a decision template that accepts the following dynamic parameters through a form:

  • Last Days – A number that defines the range of days for event calculation.

  • Page Name – The name of the page on which the event is fired.

  • Event Type – The type of event, like "VIEW" or "SUBMIT".

  • Channel – The communication channel (e.g., Web, Email, SMS).

Here’s the JavaScript code that will create the form and process the data to count the events:

(function() { var numberOfEvents = 0; // Form creation for dynamic input var lastDays = `[[Last Days | number | 2 | {order: 2, required: true } ]]`; var pageName = `[[Page Name | string | Home Page | {order: 1, required: true } ]]`; var eventType = `[[Event Type | string | VIEW | {order: 3, required: true } ]]`; var channelName = `[[Channel | enum(WEB, EMAIL, SMS) | WEB | {order: 4, required: true } ]]`; // End of form creation // Get the starting date based on "Last Days" const fromDate = getStartDate(lastDays); var totEvents = 0; // Loop through the guest's sessions and count events for (var i = 0; i < guest.sessions.length; i++) { totEvents += (guest.sessions[i].endedAt >= fromDate) ? countEvents(guest.sessions[i], fromDate, eventType, channelName, pageName) : 0; } return totEvents; })();

Explanation of the Code

  • Form Inputs: The script defines four parameters (Last Days, Page Name, Event Type, Channel), which will be populated dynamically through the form.

  • getStartDate(): This function calculates the starting date based on the "Last Days" input. For example, if the user enters "2", it will fetch events from the last 2 days.

    function getStartDate(lastDays) { var today = new Date(); var startDate = new Date(); startDate.setDate(today.getDate() - lastDays); return startDate.toISOString().split('T')[0]; // Format as YYYY-MM-DD }
  • countEvents(): This function processes each session’s events. It compares the event type, channel, page, and timestamp to ensure the event matches the user input. If it matches, it increments the numberOfEvents.

    function countEvents(session, startingFrom, eventType, channelName, pageName) { var numberOfEvents = 0; for (var i = 0; i < session.events.length; i++) { var event = session.events[i]; if (event.type === eventType && event.status === 'PROCESSED' && event.channel === channelName && event.arbitraryData.page === pageName && event.createdAt >= startingFrom) { numberOfEvents++; } } return numberOfEvents; }

Note: Publish it after changes are done

Step 3: Reusing the Decision Template

Once the decision template is created and tested, it can be reused across any decision model in Sitecore Personalize. This makes it a modular, reusable component for tracking event occurrences. The output from this template can be used as an input to other decision models or decision tables.

Step 4: Testing the Decision Template

After creating the decision template, follow these steps to test it:

  1. Create a Decision Model: Use the newly created decision template in a decision model.



  1. Input Test Data: In your test case, use specific values for the parameters, such as:

    • Event Type: PERSONAL_DETAILS_FORM_SUBMITTED

    • Page Name: personal-details

    • Last Days: 2 (for events triggered in the last 2 days)

    • Channel: WEB

  2. Run the Decision Model: Execute the decision model to verify if it correctly returns the number of event occurrences for the given parameters.



Conclusion

Creating a custom decision template in Sitecore Personalize provides an efficient way to track and analyze events based on dynamic parameters. By leveraging JavaScript, you can create reusable templates that marketers can configure without needing to write code. This not only saves time but also empowers marketers to create more personalized and data-driven experiences for their audiences.

By following the steps outlined in this blog, you can build your own event-tracking templates, customize them to your needs, and incorporate them into various decision models across your Sitecore Personalize platform.

Comments

Popular posts from this blog

Solrcloud With Zookeeper -Single server setup

Render Sitecore Experience Forms Using Sitecore XP 10.4 with a Headless Approach (Next.js + JSS SDK)

Next.js with XM Cloud EDGE and GraphQL