Integrate a Next app using the Engage SDK package (client-set cookies)
If your organization has a JSS Next.js or a JSS Angular application connected to SitecoreAI, implement Sitecore Personalize-related functionality on your SitecoreAI site using the Sitecore Cloud SDK instead. See also a comparison of the Cloud SDK and the Engage SDK.
This topic explains how to integrate your Next.js app using the Engage SDK package. In this walkthrough, you'll use client-set cookies.
This walkthrough describes how to:
-
Collect the required details about your Sitecore Personalize instance.
-
Have a Next.js app to integrate. This walkthrough was tested on Next.js versions 12, 13, and 14.2.5, both for the Pages Router and the App Router.
Install and initialize @sitecore/engage
The first step to integrating your app is to install and initialize the @sitecore/engage package.
To install and initialize the package:
-
In your terminal, open the root folder of your Next.js app.
-
Install the Engage SDK by running the following command:
-
In your code editor, open the root folder of your Next.js app.
-
Depending on your router type:
- If using the Pages Router - in the
pages/apifolder, create a file calledengage.js. - If using the App Router - in the
appfolder, create a subfolder called_api. Then, in the_apifolder, create a file calledengage.js.
- If using the Pages Router - in the
-
In
engage.js, paste the following code:Replace the placeholder values with the required details from your Sitecore Personalize instance.
Cookie consentIn production, only load the Engage SDK and set cookies if your site visitor grants consent. See also a code example to check if your site visitor accepts cookies.
Content Security Policy (CSP)To ensure that the Engage SDK script loads, you might have to add the following to your Content Security Policy (CSP):
-
https://d1mj578wat5n4o.cloudfront.net -
If you run web personalization and your environment is AP Region, EU Region, or US Region:
https://d35vb5cccm4xzp.cloudfront.net- If you run web personalization and your environment is JP Region:
https://d2ez8k04aaol9g.cloudfront.netThis script:
- Imports the
init()function from the@sitecore/engagepackage. - Asynchronously loads the Engage API using details about your Sitecore Personalize instance and sets cookies from the client.
- Exports your instance of the Engage API as a variable called
engage.
-
To import modules:
-
If using the Pages Router - in the
pagesfolder, in theindex.jsfile, importuseEffectfrom React andengagefromengage.js: -
If using the App Router - in the
appfolder, in thepage.jsfile, importuseEffectfrom React andengagefrom_api/engage. Also, add the"use client"directive at the top of of the file, above all the imports:
-
Send your first VIEW event
After you have installed and initialized the @sitecore/engage package, you collect and send data to Sitecore Personalize. You'll send a VIEW event because the VIEW event triggers every time your webpage loads.
To send a VIEW event:
-
To send VIEW event data:
-
If using the Pages Router - in the
index.jsfile, in theHomefunction, call theengage.pageView()function: -
If using the App Router - in the
page.jsfile, in theHomefunction, call theengage.pageView()function:
Replace the placeholder values with the required details from your Sitecore Personalize instance.
This script creates a VIEW event object and sends the event data to Sitecore Personalize immediately after the component renders for the first time. It also logs the browser ID to the console. You'll use the browser ID in the next procedure to find the VIEW event in Sitecore Personalize.
-
-
In your terminal, enter
npm run devto start your Next.js app. When the webpage loads, the VIEW event triggers and the event data is sent.
Verify that Sitecore Personalize captured your VIEW event
After you have sent a VIEW event, you log in to Sitecore Personalize and find the event.
To verify that Sitecore Personalize captured your VIEW event:
-
In your web browser's console, find a text similar to:
a38b230c-11eb-4cf9-8d5d-274e9f344925 -
Copy the text.
-
In Sitecore Personalize, click Developer center > Event viewer, then in the search dropdown, select Browser ID and paste the copied text into the search field. A list of events associated with this guest displays. The list contains a VIEW event. This is the event that was triggered in a previous procedure.
NoteIf you have both Sitecore CDP and Sitecore Personalize, you can find the data in Guests.
Next steps
You've now successfully integrated your app with Sitecore Personalize. You sent an event from your app and verified that Sitecore Personalize captures data about your users in real time.
Next, you can:
- Send other behavioral data, for example, an IDENTITY event or a custom event.
- Send transactional data as the user interacts with products and shopping carts on your site.
- Run experiences and experiments.