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
  • Sitecore CDP
  • Audience export
  • Understanding audience export
  • JavaScript in the export

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:

RequestResponse
(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:

Writing JavaScript and referencing data from the guest data model.
Note

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:

RequestResponse
(function () {
    // Calculate age based on a date
    function getAge(date) {
    var today = new Date();
    var birthDate = new Date(date);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
    }
    
    // Calculate the guest's age based on their date of birth
    var guestAge = "";
    if (guest.dateOfBirth) {
        guestAge = getAge(guest.dateOfBirth);
    }
  
    // Return the guest's age in the "age" variable. Then, reference "age" as ${jsResult.age} on the Output structure tab.
    return { age: guestAge }
})()

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}:

Including the computed data from the JavaScript in the output structure.

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:

RequestResponse
{
  "dataExtensions": [
    {
      "name": "ext1",
      "values": {
        "MemberCardCategoryRenewalDate": "18/05/2025",
        "MemberCardBalance": 22000,
        "MemberCardType": "shop&miles",
        "MemberCardCategory": "premium",
        "MilesRequiredForMemberCardCategory": 3000
      }
    }
  ]
}

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.

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2025 Sitecore