@sitecore-content-sdk/personalize
v2.1.0
Published
Provides personalization capabilities to build tailored experiences for site visitors.
Maintainers
Keywords
Readme
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-content-sdk/personalizeUsage
- Initialize the package using the
initContentSdkfunction, available in thecorepackage. - To run web personalization (browser-side only):
- Initialize the
eventspackage. - Enable web personalization during initialization.
- Initialize the
- To run interactive personalization, use the
personalizefunction.
Code examples
Run personalizations from the browser side:
'use client';
import { useEffect } from 'react';
import { initContentSdk } from '@sitecore-content-sdk/core';
import { analyticsPlugin, analyticsBrowserAdapter } from '@sitecore-content-sdk/analytics-core';
import { eventsPlugin } from '@sitecore-content-sdk/events';
import { personalizeBrowserPlugin, personalizeBrowserAdapter, personalize } from '@sitecore-content-sdk/personalize';
export default function Home() {
const getPersonalizeData = async () => {
// Run interactive personalization:
const data = await personalize({
channel: 'WEB',
currency: 'EUR',
friendlyId: '<YOUR_EXPERIENCE_FRIENDLY_ID>'
});
console.log(data);
};
useEffect(() => {
initContentSdk({
config: {
contextId: '<YOUR_CONTEXT_ID>',
siteName: '<YOUR_SITE_NAME>',
},
plugins: [
analyticsPlugin({
options: { enableCookie: true },
adapter: analyticsBrowserAdapter(),
}),
eventsPlugin(),
personalizeBrowserPlugin({
options: { enablePersonalizeCookie: true, webPersonalization: true },
adapter: personalizeBrowserAdapter(),
}),
],
});
getPersonalizeData();
}, []);
return <></>;
}Run personalizations from the server side:
import type { NextRequest, NextResponse } from 'next/server';
import { initContentSdk } from '@sitecore-content-sdk/core';
import { analyticsPlugin } from '@sitecore-content-sdk/analytics-core';
import { personalizeServerPlugin, personalize } from '@sitecore-content-sdk/personalize';
import { analyticsProxyAdapter, personalizeProxyAdapter } from '@sitecore-content-sdk/nextjs';
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
await initContentSdk({
config: {
contextId: '<YOUR_CONTEXT_ID>',
siteName: '<YOUR_SITE_NAME>',
},
plugins: [
analyticsPlugin({
options: { enableCookie: true },
adapter: analyticsProxyAdapter(request, response),
}),
personalizeServerPlugin({
options: { enablePersonalizeCookie: true },
adapter: personalizeProxyAdapter(request, response),
}),
],
});
// Run interactive personalization:
const data = await personalize({
channel: 'WEB',
currency: 'EUR',
friendlyId: '<YOUR_EXPERIENCE_FRIENDLY_ID>'
});
console.log(data);
return response;
}