personalize
This package provides browser- and server-side functions to run personalizations in your app. Personalization is for showing the most relevant content to your users.
Installation
npm install @sitecore-cloudsdk/personalize
Usage
- Initialize the package using the
CloudSDK
function, available in the core
package. - To run web personalization (browser-side only):
- Initialize the
events
package. - Enable web personalization during initialization.
- To run interactive personalization, use the
personalize
function.
Code examples
Run personalizations from the browser side:
'use client';
import { useEffect } from 'react';
import { CloudSDK } from '@sitecore-cloudsdk/core/browser';
import { personalize } from '@sitecore-cloudsdk/personalize/browser';
export default function Home() {
const getPersonalizeData = async () => {
const data = await personalize({
channel: 'WEB',
currency: 'EUR',
friendlyId: '<YOUR_EXPERIENCE_FRIENDLY_ID>'
});
console.log(data);
};
useEffect(() => {
CloudSDK({
})
.addEvents()
.addPersonalize({ enablePersonalizeCookie: true, webPersonalization: true })
.initialize();
getPersonalizeData();
}, []);
return <></>;
}
Run personalizations from the server side:
import type { NextRequest, NextResponse } from 'next/server';
import { CloudSDK } from '@sitecore-cloudsdk/core/server';
import { personalize } from '@sitecore-cloudsdk/personalize/server';
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
await CloudSDK(request, response, {
})
.addPersonalize({ enablePersonalizeCookie: true })
.initialize();
const data = await personalize(request, {
channel: 'WEB',
currency: 'EUR',
friendlyId: '<YOUR_EXPERIENCE_FRIENDLY_ID>'
});
console.log(data);
return response;
}
Documentation
Official Sitecore Cloud SDK documentation