Install and initialize the Cloud SDK
Before you can start using the Cloud SDK, you have to install and initialize all the SDK packagespackages you want to use, and then import their modulesmodules. For more information, see Initializing the Cloud SDK.
JSS Next.js
In this topic, you install and initialize all SDK packages on the browser side in a JSS Next.js app (JSS version 22.7).
JSS Next.js apps already initialize the Cloud SDK core and events packages on the browser side, in Bootstrap.tsx, with no coding required. If you don't want to use other SDK packages in your app, you don't need to complete this walkthrough.
Only complete this walkthrough if you want to use other SDK packages on the browser side, such as the personalize package.
-
Make sure you're ready to start development with the Cloud SDK.
-
In production, only initialize the Cloud SDK and set cookies if your site visitor grants consent. See also a code example to check if your site visitor accepts cookies.
To install and initialize the Cloud SDK on the browser side:
-
In your code editor, open your JSS Next.js app folder. This is the folder where you added environment variables when you set up your local development environment.
-
Open
package.jsonand check that the following SDK packages are installed in your project:-
@sitecore-cloudsdk/core -
@sitecore-cloudsdk/events
-
-
Open the terminal and install any other SDK packages you want to use by running the following command:
npm install @sitecore-cloudsdk/personalize npm install @sitecore-cloudsdk/search -
Open
Bootstrap.tsx. This component already initializes the Cloud SDK and theeventspackage. You'll update the component code in the next step to initialize other SDK packages, such aspersonalize. -
At the top of the file, directly below the
CloudSDKimport statement, import thebrowsermodule of the packages you installed in a previous step:// ... import "@sitecore-cloudsdk/personalize/browser"; import "@sitecore-cloudsdk/search/browser"; -
In the Effect Hook, in the
CloudSDKfunction, directly before theinitializemethod, paste the following code, then save your changes:// ... .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } }) .addSearch() // ...This code initializes the
personalizeand thesearchpackages and enables the cookie required for personalization. It also enables Sitecore Personalize web personalizations. If you don't want to enable web personalizations, setwebPersonalization: false.NoteThe
languageparameter was added to theaddPersonalizemethod'swebPersonalizationproperty in Cloud SDK 0.5.6 to support languages for web personalization. -
In your terminal, enter
npm run start:productionto start your JSS Next.js app. -
In your web browser, navigate to your JSS Next.js app, typically at
http://localhost:3000. -
In your web browser console, enter
window.scCloudSDK. If an object returns, you have successfully initialized the SDK.TipThis object lists all the packages you have initialized and their version. Use this in any future implementations to quickly verify that the SDK is correctly initialized.
-
Optionally, in your web browser's developer tools, in Cookies, find at least one cookie that starts with
sc_. This is another way to verify that you've successfully initialized the Cloud SDK.You can now run other browser-side SDK code in the rest of your app.
Next.js
In this topic, you install and initialize all SDK packages in a Next.js app. In production, only initialize the packages you need depending on your application requirements and your Sitecore subscriptions.
-
Make sure you're ready to start development with the Cloud SDK.
Browser side
On the browser side, initialize the SDK once, in a centralized place, on app initialization. For example, in a Next.js App Router app, initialize the SDK in an Effect Hook in a component, then add that component to layout.tsx. This ensures that the initialization runs only once, centrally.
To install and initialize the Cloud SDK on the browser side:
-
In your code editor, in the root folder of your web app, open the terminal and install the
corepackage and any other SDK packages you want to use by running the following commands:npm install @sitecore-cloudsdk/core npm install @sitecore-cloudsdk/events npm install @sitecore-cloudsdk/personalize npm install @sitecore-cloudsdk/searchThe
corepackage contains the SDK initialization logic. The other packages contain code for setting up Sitecore DXPSitecore DXP capabilities. -
Create a component, for example,
/components/CloudSDK.tsx. -
Depending on your router type, paste the following code into
CloudSDK.tsx, then save your changes:-
If using the Pages Router:
import { useEffect } from "react"; // Import SDK modules -> import { CloudSDK } from "@sitecore-cloudsdk/core/browser"; import "@sitecore-cloudsdk/events/browser"; import "@sitecore-cloudsdk/personalize/browser"; import "@sitecore-cloudsdk/search/browser"; // <- Import SDK modules export default function CloudSDKComponent() { useEffect(() => { CloudSDK({ sitecoreEdgeContextId: "<YOUR_SITECORE_EDGE_CONTEXT_ID>", siteName: "<YOUR_SITE_NAME>", enableBrowserCookie: true }) .addEvents() // Initialize the events package .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } }) // Initialize the personalize package .addSearch() // Initialize the search package .initialize(); // Run the initialization logic and set cookies }, []); return null; }; -
If using the App Router:
"use client"; import { useEffect } from "react"; // Import SDK modules -> import { CloudSDK } from "@sitecore-cloudsdk/core/browser"; import "@sitecore-cloudsdk/events/browser"; import "@sitecore-cloudsdk/personalize/browser"; import "@sitecore-cloudsdk/search/browser"; // <- Import SDK modules export default function CloudSDKComponent() { useEffect(() => { CloudSDK({ sitecoreEdgeContextId: "<YOUR_SITECORE_EDGE_CONTEXT_ID>", siteName: "<YOUR_SITE_NAME>", enableBrowserCookie: true }) .addEvents() // Initialize the events package .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } }) // Initialize the personalize package .addSearch() // Initialize the search package .initialize(); // Run the initialization logic and set cookies }, []); return null; };
Replace the placeholder values with the required details you collected when you prepared for development.
This code runs the
CloudSDKfunction, which initializes the Cloud SDK using your Context ID and site name, and it sets cookies from the browser side. It also initializes theevents,personalize, andsearchpackages, and it enables Sitecore Personalize web personalizations. If you don't want to enable web personalizations, setwebPersonalization: false.The component is now ready to be used elsewhere in your app.
-
-
Depending on your router type:
-
If using the Pages Router - add the component you just created to
_app.tsx, in thereturnstatement directly above<Component />:// ... import CloudSDKComponent from "@/components/CloudSDK"; export default function App({ Component, pageProps }: AppProps) { return ( <> <CloudSDKComponent /> <Component {...pageProps} /> </> ); } -
If using the App Router - add the component you just created to
layout.tsx, directly inside the<body>tag, before{children}, then save your changes:import CloudSDKComponent from "../components/CloudSDK"; // ... export default function RootLayout({ children }) { return ( <html> <body> <CloudSDKComponent /> {children} </body> </html> ); }
This code initializes the SDK once, on app initialization. Because this code runs in the app's root layout, the SDK code will be available globally, on all routes of your app.
-
-
Start your app by entering the following command in your terminal:
npm run dev -
In your web browser, navigate to your Next.js app, typically at
http://localhost:3000. -
In your web browser console, enter
window.scCloudSDK. If an object returns, you have successfully initialized the SDK.TipThis object lists all the packages you have initialized and their version. Use this in any future implementations to quickly verify that the SDK is correctly initialized.
-
Optionally, in your web browser's developer tools, in Cookies, find at least one cookie that starts with
sc_. This is another way to verify that you've successfully initialized the Cloud SDK.You can now run other browser-side SDK code in the rest of your app. For example, you can start setting up tracking by capturing
VIEWevents.
Server side
On the server side, initialize the SDK in every file that is intended to run Cloud SDK code. Depending on your application, these files might contain a page request, an API request, or a middleware request.
To install and initialize the Cloud SDK on the server side, in a middleware:
-
In your code editor, in the root folder of your web app, open the terminal and install the
corepackage and any other SDK packages you want to use by running the following commands:npm install @sitecore-cloudsdk/core npm install @sitecore-cloudsdk/events npm install @sitecore-cloudsdk/personalize npm install @sitecore-cloudsdk/searchThe
corepackage contains the SDK initialization logic. The other packages contain code for setting up Sitecore DXPSitecore DXP capabilities. -
Create
middleware.tsin thesrcor thesrc/appfolder, depending on your project structure. -
Paste the following code into
middleware.ts, then save your changes:import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; // Import SDK modules -> import { CloudSDK } from "@sitecore-cloudsdk/core/server"; import "@sitecore-cloudsdk/events/server"; import "@sitecore-cloudsdk/personalize/server"; import "@sitecore-cloudsdk/search/server"; // <- Import SDK modules export default async function middleware(request: NextRequest) { const response = NextResponse.next(); await CloudSDK(request, response, { sitecoreEdgeContextId: "<YOUR_SITECORE_EDGE_CONTEXT_ID>", siteName: "<YOUR_SITE_NAME>", enableServerCookie: true }) .addEvents() // Initialize the events package .addPersonalize({ enablePersonalizeCookie: true, webPersonalization: { language: 'en' } }) // Initialize the personalize package .addSearch() // Initialize the search package .initialize(); // Run the initialization logic and set cookies return response; };Replace the placeholder values with the required details you collected when you prepared for development.
This code runs the
CloudSDKfunction, which initializes the Cloud SDK using your Context ID and site name, and it sets cookies from the server side. It also initializes theevents,personalize, andsearchpackages. -
Start your app by entering the following command in your terminal:
npm run dev -
In your web browser, navigate to your Next.js app, typically at
http://localhost:3000. -
In your web browser's developer tools, in Cookies, find at least one cookie that starts with
sc_. If such a cookie is present, you've successfully initialized the Cloud SDK.You can now run other server-side SDK code in this file. For example, you can start setting up tracking by capturing
VIEWevents.To run SDK code in other server-side files that handle page requests, API requests, or middleware requests, you'll have to import
CloudSDKand the other SDK package modules into those files, and you'll have to run theCloudSDKfunction and initialize the SDK packages, depending on your application requirements.
You've now successfully installed and initialized the Cloud SDK. You are now ready to set up the following Cloud SDK capabilities to ensure that your web app uses the full potential of Sitecore DXP products:
We also recommend that you:
-
Explore the reference documentation for all Cloud SDK packages and modules.
-
Learn about Cloud SDK cookies.
-
Learn more about the SDK initialization process.
The Cloud SDK consists of packages. Each package represents a set of Sitecore DXP capabilities. Cloud SDK packages consist of browser and server modules.
Cloud SDK packages consist of browser and server modules. Each module contains all the functions required to set up specific Sitecore DXP capabilities. The browser module is for setting them up on the browser side, and the server module is for setting them up on the server side.
A suite of SaaS-enabled Sitecore products, each offering rich digital experience capabilities such as tracking, personalization, and search. The Cloud SDK supports the following Sitecore products:
-
Sitecore CDP
-
Sitecore Personalize
-
Sitecore Search