JavaScript in the export
When creating an audience export, you can use JavaScript to compute data in addition to the data that's already in the guest profile. This lets you enrich the data that you export from Sitecore CDP.
For example, if the guest's date of birth is in the guest profile, you can include it in the output as ${guest.dateOfBirth}
. If the guest's age, however, isn't in the guest profile, you can compute it with JavaScript and then include it in the output.
To use JavaScript in an audience export, you can:
-
Enter your own JavaScript functions in the JavaScript code editor. See our code snippets for examples of useful JavaScript functions.
-
Call a utility function in a field on the Output structure tab.
Here's a video that introduces you to using JavaScript in audience exports:
Computing data using functions
You can use JavaScript functions in an audience export by clicking Output format > Define output > JavaScript code editor. Make sure to have all functions inside a single function:
(function () {
/* first function */
/* second function */
/* third function */
return {}
})()
In the JavaScript, you can work with data from the guest profile. As you start typing guest in the code editor, a list of references to the guest data model appears:

To prevent errors, we recommend that you log messages to the test log using print()
. You'll use the test log when you test the export.
In the JavaScript, you must return a single object. You can then use the object key names to include the data in the output on the Output structure tab.
Here's an example JavaScript function that computes the guest's approximate age by referencing the guest's date of birth in the guest profile:
In the returned object, the key name is age
. To include the guest's age in the output, write ${jsResult.age}
in a column on the Output structure tab.
Including the computed data in the output
In an audience export, on the Output structure tab, you can include the computed data in the Attribute value field of a string or number field. Use the ${jsResult.objectKeyName}
notation, for example, ${jsResult.age}
:

Calling utility functions
Utility functions help you access data in the guest profile and make data transformations. For example, you can join strings, transform dates, or hash sensitive data so the data is securely exported.
You can call these functions in an audience export by clicking Output format > Define output > Add column, then entering the utility function into the Attribute value field of a string or number field. Make sure to replace the function parameters with values of your choice.
Here's an example of calling the ${sha256()}
utility function to securely export a guest's email address:

Or consider a guest profile with the following data extension object:
You can call ${getDataExtension(guest.dataExtensions, "ext1", "MemberCardCategory", "not-available")} in the Attribute value field to access the value for MemberCardCategory
and include it in the output.
Testing the export
After saving your changes, we recommend that you test the audience export to make sure that the JavaScript computation is successful.