Initializing the Cloud SDK
How you initialize the Cloud SDK depends on what SDK functionalities you want to set up, the type of application you have, and whether you want to run SDK code on the browser side or on the server.
SDK functionalities are available in packages
Initializing the SDK involves installing all the SDK packagespackages you want to use, and then importing their modulesmodules into your application.
Because all Cloud SDK functionalities are available in packages, you'll install packages depending on which functionalities you want to set up. The core
package contains the initialization logic, so you must install core
to initialize the SDK. The other packages represent a set of Sitecore digital experience platform (DXP) capabilities you can set up on your site, such as tracking, personalization, and search.
In each package, browser
and server
modules contain all the functions you need to set up these capabilities on the browser side and on the server side, respectively. For example, to set up browser-side personalization, you'll install the core
and the personalize
packages and import their browser
modules.
Application type
The type of app you want to use the Cloud SDK in determines what you have to manually install and initialize.
You can install Cloud SDK packages by running the following commands:
Browser- and server-side code
In each SDK package, you can use the browser
modules to run Cloud SDK code on the browser side, or the server
modules to run it on the server side. You can also run some functionality on the browser side, and some other on the server.
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.
If you want to use the SDK on both the browser and server side, set cookies either from the browser or from the server, not both.
Understanding the initialization process
Depending on your application requirements, you might need to modify the initialization code provided in this documentation. This section explains the initialization process so you can adjust the code to your needs.
Successfully initializing the SDK involves the following steps:
-
Import the modules of all the SDK packages you want to use.
-
Use the
CloudSDK
function to provide your initialization settings, such as your Context ID and site name. -
Chain the package initialization methods, such as
addEvents
, on theCloudSDK
function. -
Chain
initialize
as the last method on theCloudSDK
function to run the initialization logic.
When you start the initialization, you must import the modules of all the SDK packages you want to use:
import { CloudSDK } from "@sitecore-cloudsdk/core/browser";
import "@sitecore-cloudsdk/events/browser";
import "@sitecore-cloudsdk/personalize/browser";
import "@sitecore-cloudsdk/search/browser";
The core
import lets you access the CloudSDK
function.
The other imports give the CloudSDK
function access to the package initialization methods, such as to addEvents
, addPersonalize
, and addSearch
. These are required to initialize the events
, personalize
, and search
packages, respectively.
The CloudSDK
function initializes the SDK itself when you run the initialize
method. The initialize
method runs the initialization logic and sets cookies. The following code example does all this, but it doesn't initialize any other SDK packages on its own:
CloudSDK({ /* Your initialization settings */ })
.initialize();
To initialize the other SDK packages, you have to chain the package initialization methods, such as addEvents
, on the CloudSDK
function, before the initialize
method. The following code example initializes the SDK and all its packages. After this code runs, you can run other SDK functions available in the packages:
CloudSDK({ /* Your initialization settings */ })
.addEvents()
.addPersonalize({ enablePersonalizeCookie: true, webPersonalization: true })
.addSearch()
.initialize();
Behind the scenes, the initialization logic uses the JavaScript Dynamic Prototype Pattern. This is the pattern that gives the CloudSDK
function access to the package initialization methods after you import their corresponding modules.
You can initialize any number of SDK packages on the CloudSDK
function after you import their corresponding modules. The order in which you run the package initialization methods does not matter, but they must precede the initialize
method.
Here's a list of all package initialization methods:
-
addEvents
initializes theevents
package. -
addPersonalize
initializes thepersonalize
package. -
addSearch
initializes thesearch
package.
Certain functionality can be enabled directly in the package initialization methods. For example, you enable web personalizations on the browser side in the addPersonalize
method, using the webPersonalization
property.
Although each Cloud SDK package contains a distinct set of functionalities, some functionalities depend on multiple packages. In this scenario, you have to install all the required packages, import their modules, and then run all the package initialization methods.
For example, web personalization and capturing events in search experiences require that you also initialize the events
package.
The initialization logic
The initialization logic makes it easy to set up the SDK and initialize packages dynamically, according to your application requirements. For example, the logic:
-
Centralizes the SDK setup, making your SDK code easier to scale and maintain.
-
Makes it easy to enable more SDK packages in your code in the future.
-
Separates package initialization into independent methods: initializing a package will not initialize another one.
-
Contains code autocompletion and IntelliSense to help with the initialization process.