Run a headless experience

After a headless personalization or A/B/n test is Live, your application can run it to receive the configured response for the personalization or assigned test variants.

For details about what your application sends, what SitecoreAI returns, and what to do if no response is returned, see Request and render a headless experience.

Use the Integration guide in SitecoreAI for instructions and code specific to the selected experience.

You can run a headless experience using Content SDK, REST API, or Cloud SDK.

Open the Integration guide

To open the Integration guide:

  1. In the navigation menu, click Optimization.

  2. In the left sidebar, click Tests & personalizations.

  3. Select Headless.

  4. Select the live headless experience you want to run.

    The Configuration page opens.

  5. In the right pane, click the Connect tab.

  6. Click Open guide.

    The Integration guide pane opens.

  7. Select the integration method that matches your application:

Note

The Integration guide provides the request or code specific to the selected experience. SitecoreAI returns the configured API response, which your application uses to render the experience.

Content SDK

Before you begin

Make sure:

Your application uses Content SDK 2.x and is initialized with the analytics, events, and personalize plugins. For details, see the Content SDK initialization guide.

To run the experience from a Content SDK application:

  1. In the Integration guide pane, click the Content SDK tab.

  2. In Step 1 - Run the personalization, click the copy icon to copy the TypeScript or JavaScript code.

  3. Add the code to your application and run it to request the headless experience.

    The SDK reuses the visitor's client ID and site information from initialization, so you only need to specify the experience's friendly ID and channel.

    For example:

    import { personalize } from "@sitecore-content-sdk/personalize";
    
    const personalizeResponse = await personalize({
      channel: "WEB",
      friendlyId: "headlesspersonalization",
    });
    Note

    The sample sets channel to WEB for a website. For a mobile app, set it to MOBILE_APP. For a kiosk, set it to KIOSK. Use the same value in the goal event.

  4. If the Integration guide includes Step 2 - Send the goal event, click the copy icon to copy the code and send it after the personalization request. A goal is optional. Skip this step when the experience does not track one.

    For example, for a Page views goal:

    import { pageView } from "@sitecore-content-sdk/events";
    
    await pageView({
       channel: "WEB",
       language: "EN",
       page: "/speakers",
    });

    The SDK reuses the same visitor and site information as the personalization request.

REST API

Before you begin

Make sure:

To run the experience using the REST API:

  1. In the Integration guide pane, click the REST API tab.

  2. In the Sitecore context ID field, enter the context ID for your environment.

    If necessary, click View context IDs to find the ID.

  3. In the Analytics identifier list, select the analytics identifier to use for tracking.

  4. In Step 1 - Get a client ID, click the copy icon to copy the request template and send the request.

    A client ID identifies the visitor across requests. For a new visitor, retrieve the id from the response, store it, and reuse it as <CLIENT_ID> in subsequent requests for the same visitor.

    For example:

    curl -X POST '<baseURL>/v1/events/api/dp/v1/clients' \
      -H 'Content-Type: application/json' \
      -H 'Accept: application/json' \
      -H 'x-sitecore-contextid: <SITECORE_CONTEXT_ID>' \
      --data-raw '{
        "clientKey": ""
      }'

    Send this request from the visitor's browser or device so SitecoreAI can derive location and device information from the request.

  5. In Step 2 - Run the personalization, click the copy icon to copy the request template and send the personalization request.

    Use the analytics identifier selected in step 3 and the client ID obtained in step 4.

    For example:

    curl -X POST '<baseURL>/v1/personalize/v2/callFlows?siteId=<ANALYTICS_IDENTIFIER>' \
      -H 'Content-Type: application/json' \
      -H 'Accept: application/json' \
      -H 'x-sitecore-contextid: <SITECORE_CONTEXT_ID>' \
      --data-raw '{
        "clientKey": "",
        "channel": "web",
        "pointOfSale": "<ANALYTICS_IDENTIFIER>",
        "browserId": "<CLIENT_ID>",
        "friendlyId": "headlesspersonalization"
      }'

    The response contains the data defined in the headless experience's API response template. Your application uses this data to display the experience to visitors.

  6. If the Integration guide includes Step 3 – Send the goal event, click to copy the request template and send the page view event after the personalization request.

    Use the same client ID and analytics identifier as in the personalization request.

    For example, for a Page views goal:

    curl -X POST '<baseURL>/v1/events/api/dp/v1/events' \
       -H 'Content-Type: application/json' \
       -H 'Accept: application/json' \
       -H 'x-sitecore-contextid: <SITECORE_CONTEXT_ID>' \
       --data-raw '{
         "clientKey": "",
         "clientId": "<CLIENT_ID>",
         "siteId": "<ANALYTICS_IDENTIFIER>",
         "channel": "WEB",
         "language": "EN",
         "type": "pageView",
         "name": "/speakers"
    }'

Cloud SDK

Before you begin

Make sure:

Your application uses the Sitecore Cloud SDK and is initialized with the events and personalize packages by using addEvents() and addPersonalize(). For details, see the Cloud SDK initialization guide.

To run the experience from a Cloud SDK application:

  1. In the Integration guide pane, click the Cloud SDK tab.

  2. In Step 1 - Run the personalization, click the copy icon to copy the TypeScript or JavaScript code.

  3. Add the code to your application and run it to request the headless experience.

    The SDK reuses the visitor's client ID and site information from initialization, so you only need to specify the experience's friendly ID and channel.

    For example:

    import { personalize } from "@sitecore-cloudsdk/personalize/browser";
    
    const personalizeResponse = await personalize({
      channel: "WEB",
      friendlyId: "headlesspersonalization",
    });

    The response contains the data defined in the headless experience's API response template. Your application uses this data to display the experience to visitors.

  4. If the Integration guide includes Step 2 – Send the goal event, click and send it after the personalization request. Skip this step when the experience does not track a goal.

    For example, for a Page views goal:

       import { pageView } from "@sitecore-cloudsdk/events/browser";
    
       await pageView({
          channel: "WEB",
          language: "EN",
          page: "/speakers",
       });

    The SDK reuses the same visitor and site information as the personalization request.

If you have suggestions for improving this article, let us know!