@sitecore-content-sdk/events
v2.1.0
Published
Enables real-time, unified tracking to send events to Sitecore.
Maintainers
Keywords
Readme
events
This package provides browser- and server-side functions to capture events in your app and send them to Sitecore. Events are for collecting behavioral data about your users as they interact with your app.
Installation
npm install @sitecore-content-sdk/eventsUsage
- Initialize the package using the
initContentSdkfunction, available in thecorepackage. - Send events using the following functions:
pageView- send a VIEW event.identity- send an IDENTITY event.form- send a FORM event (browser-side only).event- send SC_SEARCH events, other standard events, or a custom event.
Code examples
Capture and send a VIEW event 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, pageView } from '@sitecore-content-sdk/events';
export default function Home() {
useEffect(() => {
initContentSdk({
config: {
contextId: '<YOUR_CONTEXT_ID>',
siteName: '<YOUR_SITE_NAME>',
},
plugins: [
analyticsPlugin({
options: { enableCookie: true },
adapter: analyticsBrowserAdapter(),
}),
eventsPlugin(),
],
});
// Send VIEW event:
pageView();
}, []);
return <></>;
}Capture and send a VIEW event 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 { eventsPlugin, pageView } from '@sitecore-content-sdk/events';
import { analyticsProxyAdapter } 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(),
],
});
// Send VIEW event:
await pageView();
return response;
}