@sitecore-cloudsdk/events
v0.4.2
Published
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.
Downloads
78,738
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-cloudsdk/events
Usage
- Initialize the package using the
CloudSDK
function, available in thecore
package. - 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 { CloudSDK } from '@sitecore-cloudsdk/core/browser';
import { pageView } from '@sitecore-cloudsdk/events/browser';
export default function Home() {
useEffect(() => {
CloudSDK({
/* Initialization settings. See `core` package code examples. */
})
.addEvents()
.initialize();
// Send VIEW event:
pageView();
}, []);
return <></>;
}
Capture and send a VIEW event from the server side:
import type { NextRequest, NextResponse } from 'next/server';
import { CloudSDK } from '@sitecore-cloudsdk/core/server';
import { pageView } from '@sitecore-cloudsdk/events/server';
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
await CloudSDK(request, response, {
/* Initialization settings. See `core` package code examples. */
})
.addEvents()
.initialize();
// Send VIEW event:
await pageView(request);
return response;
}