@sitecore-content-sdk/analytics-core
v2.1.0
Published
Provides shared logic and runtime initialization. Required for the Content SDK 'events' and 'personalize' packages to function.
Maintainers
Keywords
Readme
analytics-core
This package provides the analytics core functionality for the Sitecore Content SDK.
Installation
npm install @sitecore-content-sdk/analytics-coreTo use other Content SDK packages, first install them:
npm install @sitecore-content-sdk/events
npm install @sitecore-content-sdk/personalizeUsage
- Import the
initContentSdkfunction from@sitecore-content-sdk/core. - Import the plugins and adapters from the packages you want to use.
- Initialize the Content SDK with the plugins array.
Code examples
Initialize the Content SDK on 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 } from '@sitecore-content-sdk/personalize';
export default function Home() {
useEffect(() => {
initContentSdk({
config: {
contextId: '<YOUR_CONTEXT_ID>',
siteName: '<YOUR_SITE_NAME>',
},
plugins: [
analyticsPlugin({
options: { enableCookie: true },
adapter: analyticsBrowserAdapter(),
}),
eventsPlugin(),
personalizeBrowserPlugin({
options: { enablePersonalizeCookie: true },
adapter: personalizeBrowserAdapter(),
}),
],
});
}, []);
return <></>;
}Initialize the Content SDK on 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 { eventsPlugin } from '@sitecore-content-sdk/events';
import { personalizeServerPlugin } from '@sitecore-content-sdk/personalize';
import { analyticsProxyAdapter, personalizeProxyAdapter } from '@sitecore-content-sdk/nextjs';
export async function proxy(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),
}),
eventsPlugin(),
personalizeServerPlugin({
options: { enablePersonalizeCookie: true },
adapter: personalizeProxyAdapter(request, response),
}),
],
});
return response;
}