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
AI in CDP
Glossary
  • Sitecore CDP
  • Audience export
  • JavaScript functions
  • Code snippets

Code snippets

Here's a list of example JavaScript functions you can use in an audience export.

Guest age

This function returns the guest's approximate age from their date of birth:

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 }
})()

Example return value: {"age": "25"}

If you're using Basic mode, you can include this in the output as ${jsResult.age}.

Alternatively, if you're using Advanced mode, the returned JSON object will automatically be included in the output.

Order in previous 24 hours

This function searches for an order completed by the guest in the previous 24 hours. If such an order exists, the JavaScript returns the order reference and order status:

RequestResponse
(function () {
  load("classpath:moment.js");

  // Get guest's orders
  var orders = guest.orders;
  var orderRef = "";
  var orderStatus = "";

  // Get yesterday's date
  yesterdayDate = moment.utc().subtract(1, "day").format("YYYY-MM-DD");

  for (var orderCount in orders) {
    var orderDate = moment(orders[orderCount].orderedAt).format("YYYY-MM-DD");
    // Log last order date to the console
    console.log(orderDate);
    // Check if order date matches yesterday's date
    if (orderDate === yesterdayDate) {
      orderRef = orders[orderCount].referenceId;
      orderStatus = orders[orderCount].status;
    }
  }
  
  // Return the order data in the "orderRef" and "orderStatus" variables. Then, reference them as ${jsResult.orderRef} and ${jsResult.orderStatus} on the Output structure tab.
  return { orderRef: orderRef, orderStatus: orderStatus };
})();

Example return values: {"orderRef": "B-321741", "orderStatus": "PURCHASED"}

If you're using Basic mode, you can include this in the output as ${jsResult.orderRef} and ${jsResult.orderStatus}.

Alternatively, if you're using Advanced mode, the returned JSON object will automatically be included in the output.

Date of most recent session

This function returns the date and time of the guest's most recent session:

RequestResponse
(function() {
  if (guest.sessions) {
    var lastSession = guest.sessions[0];
    var date = "";

    if (lastSession) {
      if (lastSession.events) {
        for (var event in lastSession.events) {
          if (lastSession.events[event].createdAt) {
            date = lastSession.events[event].createdAt;
          }
        }
      }
    }
  }

  // Return the guest's last session date in the "lastSessionDate" variable. Then, reference "lastSessionDate" as ${jsResult.lastSessionDate} on the Output structure tab. 
  return {
    "lastSessionDate": date
  };
})();

Example return value: {"lastSessionDate": "2026-04-15T09:11:07.078Z"}

If you're using Basic mode, you can include this in the output as ${jsResult.lastSessionDate}.

Alternatively, if you're using Advanced mode, the returned JSON object will automatically be included in the output.

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2026 Sitecore