Create a JS module
Technical users can create a JavaScript function for reuse within programmable decisions. When a JS module is published, it is made available for importing into a programmable decision. After a module is published it cannot be edited. After a module is imported into a programmable, it cannot be edited or deleted.
We recommend saving the module often while working on it in draft status - this runs validation checks and enables you to quickly correct errors. In addition to syntax and format validations, the system checks the limitations outlined in the following section.
To avoid errors when including timestamps in your JavaScript, use the moment.js library. The library enables you to parse, validate, manipulate, and display dates and time formats in JavaScript. Additionally, you can load the time zone library to format dates to a specific time zone.
load("classpath:moment.js");
load("classpath:moment-timezone.js");Restrictions
-
The maximum size of a single module is 100 KB.
-
The JavaScript must be ES5 compatible.
-
Calls to external services are not allowed.
At any stage of creating the module, you can save your changes. The module remains in draft status, and is therefore editable, until you publish it.
Team members with a User, Developer, or Admin role in Sitecore Cloud Portal can create a JS module.
To create a JS module:
-
On the navigation menu, click Developer center > JS modules.
-
On the JS Modules page, click Create.
-
In the Name your new module field, enter an intuitive name for the JS module. The allowed characters for the ID are Latin alphabet, numbers, and underscore. As you type, the name of the module displays in the ID field.
TipGive the module a meaningful name that helps users locate it and understand its functionality when importing the module.
-
Click Create.
-
Optionally, in the module editor, on the Details pane, click the following:
-
Hypothesis - enter a description for the JS module. We recommend this so that other users can understand its functionality when using the module.
-
Add tags - enter tags for the module. These are used to filter the list of modules on the JS modules page.
-
-
In the Code section, click Start coding. In the code editor, in the JavaScript tab, enter the JavaScript module. To automatically format and indent the JavaScript code, click
.The following example is a simple function returns a
trueresponse when the guest type is customer:RequestResponsefunction isCustomer(guest) { return guest.guestType === 'customer'; }NoteWe recommend familiarizing yourself with the supported and unsupported JavaScript functions.
-
Click the Data tab in the right pane to copy a guest data model attribute or path and paste into the JavaScript editor. Click
and choose one of the following options:-
To view a random guest's attributes based on guest type, click in the search box and choose Quick Select Customer or Quick Select Visitor. The system selects a guest who was recently active.
-
To select fields from a specific guest, enter their unique identifier into the search box. For example, if you want to use segment membership as dynamic data, search for a guest who is a member of the segment. You can search for a guest using, for example, an email address, other unique identifier or browser ID.
TipIf there is a guest whose data you often use when configuring or testing code, click the star icon to bookmark the guest so you no longer have to search for them.
-
-
We recommend that you test the module. To save the JS module, click Save.
-
To publish the JS module, click the back arrow and click Publish.
-
In the confirmation dialog, click Publish. The JS module is available for users to import into programmable decisions.
WarningOnce you publish the module, you can not edit or unpublish it.