Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sitecore-cloudsdk/events

Package Overview
Dependencies
Maintainers
3
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sitecore-cloudsdk/events - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

dist/cjs/src/lib/events/send-event/sendEvent.d.ts

8

dist/cjs/package.json
{
"name": "@sitecore-cloudsdk/events",
"version": "0.1.3",
"version": "0.2.0",
"license": "Apache-2.0",

@@ -35,3 +35,7 @@ "exports": {

"server.d.ts"
]
],
"engines": {
"node": ">=18",
"npm": ">=9"
}
}

@@ -5,1 +5,11 @@ /**

export declare const LIBRARY_VERSION: string;
export declare enum ErrorMessages {
IE_0001 = "[IE-0001] The \"window\" object is not available on the server side. Use the \"window\" object only on the client side, and in the correct execution context.",
IE_0004 = "[IE-0004] You must first initialize the \"events/browser\" module. Run the \"init\" function.",
IE_0005 = "[IE-0005] You must first initialize the \"events/server\" module. Run the \"init\" function.",
IV_0002 = "[IV-0002] Incorrect value for \"dob\". Format the value according to ISO 8601.",
IV_0003 = "[IV-0003] Incorrect value for \"email\". Set the value to a valid email address.",
IV_0004 = "[IV-0004] Incorrect value for \"expiryDate\". Format the value according to ISO 8601.",
IV_0005 = "[IV-0005] This event supports maximum 50 attributes. Reduce the number of attributes.",
MV_0003 = "[MV-0003] \"identifiers\" is required."
}

@@ -6,6 +6,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.LIBRARY_VERSION = void 0;
exports.ErrorMessages = exports.LIBRARY_VERSION = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
/* eslint-disable @typescript-eslint/naming-convention */
const package_json_1 = __importDefault(require("../../package.json"));
const consts_1 = require("./events/consts");
/**

@@ -15,1 +16,12 @@ * Returns the version of the library.

exports.LIBRARY_VERSION = package_json_1.default.version;
var ErrorMessages;
(function (ErrorMessages) {
ErrorMessages["IE_0001"] = "[IE-0001] The \"window\" object is not available on the server side. Use the \"window\" object only on the client side, and in the correct execution context.";
ErrorMessages["IE_0004"] = "[IE-0004] You must first initialize the \"events/browser\" module. Run the \"init\" function.";
ErrorMessages["IE_0005"] = "[IE-0005] You must first initialize the \"events/server\" module. Run the \"init\" function.";
ErrorMessages["IV_0002"] = "[IV-0002] Incorrect value for \"dob\". Format the value according to ISO 8601.";
ErrorMessages["IV_0003"] = "[IV-0003] Incorrect value for \"email\". Set the value to a valid email address.";
ErrorMessages["IV_0004"] = "[IV-0004] Incorrect value for \"expiryDate\". Format the value according to ISO 8601.";
ErrorMessages["IV_0005"] = "[IV-0005] This event supports maximum 50 attributes. Reduce the number of attributes.";
ErrorMessages["MV_0003"] = "[MV-0003] \"identifiers\" is required.";
})(ErrorMessages || (exports.ErrorMessages = ErrorMessages = {}));

@@ -1,6 +0,4 @@

import type { Settings } from '@sitecore-cloudsdk/core';
import { EventAttributesInput } from './common-interfaces';
export declare class BaseEvent {
private baseEventData;
protected settings: Settings;
private readonly browserId;

@@ -16,3 +14,3 @@ private readonly language;

*/
constructor(baseEventData: BaseEventData, settings: Settings, id: string);
constructor(baseEventData: BaseEventData, id: string);
/**

@@ -19,0 +17,0 @@ * A function that returns the properties for sending events to Sitecore EP

@@ -14,5 +14,4 @@ "use strict";

*/
constructor(baseEventData, settings, id) {
constructor(baseEventData, id) {
this.baseEventData = baseEventData;
this.settings = settings;
this.browserId = id;

@@ -19,0 +18,0 @@ this.language = this.baseEventData.language ?? (0, core_1.language)();

import { BaseEvent } from '../base-event';
import { EventAttributesInput } from '../common-interfaces';
import { EventApiClient } from '../../ep/EventApiClient';
import { SendEvent } from '../send-event/sendEvent';
import { EPResponse, Settings } from '@sitecore-cloudsdk/core';

@@ -8,4 +8,5 @@ import { BasicTypes, NestedObject } from '@sitecore-cloudsdk/utils';

customEventPayload: CustomEventPayload;
private eventApiClient;
private sendEvent;
private extensionData;
private settings;
/**

@@ -26,3 +27,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface CustomEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: CustomEventData;

@@ -29,0 +30,0 @@ id: string;

@@ -8,2 +8,3 @@ "use strict";

const consts_1 = require("../consts");
const consts_2 = require("../../consts");
class CustomEvent extends base_event_1.BaseEvent {

@@ -16,5 +17,6 @@ /**

const { channel, currency, language, page, ...rest } = args.eventData;
super({ channel, currency, language, page }, args.settings, args.id);
super({ channel, currency, language, page }, args.id);
this.extensionData = {};
this.eventApiClient = args.eventApiClient;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
this.customEventPayload = {

@@ -28,3 +30,3 @@ type: args.type,

if (numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
throw new Error(consts_2.ErrorMessages.IV_0005);
if (numberOfExtensionDataProperties > 0)

@@ -40,5 +42,5 @@ this.customEventPayload.ext = this.extensionData;

const fetchBody = Object.assign({}, this.customEventPayload, baseAttr);
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}
}
exports.CustomEvent = CustomEvent;

@@ -5,4 +5,6 @@ "use strict";

exports.event = void 0;
const core_1 = require("@sitecore-cloudsdk/core");
const custom_event_1 = require("./custom-event");
const initializer_1 = require("../../initializer/browser/initializer");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -16,9 +18,11 @@ * A function that sends an event to SitecoreCloud API with the specified type

*/
function event(type, eventData, extensionData) {
const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
async function event(type, eventData, extensionData) {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
return new custom_event_1.CustomEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent: sendEvent_1.sendEvent,
settings,

@@ -25,0 +29,0 @@ type,

@@ -7,3 +7,3 @@ "use strict";

const custom_event_1 = require("./custom-event");
const initializer_1 = require("../../initializer/server/initializer");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -18,10 +18,10 @@ * A function that sends an event to SitecoreCloud API with the specified type

function eventServer(type, eventData, request, extensionData) {
const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
const settings = (0, core_1.getSettingsServer)();
const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
return new custom_event_1.CustomEvent({
eventApiClient,
eventData,
extensionData,
id,
settings: settings,
sendEvent: sendEvent_1.sendEvent,
settings,
type,

@@ -28,0 +28,0 @@ }).send();

@@ -5,4 +5,6 @@ "use strict";

exports.form = void 0;
const core_1 = require("@sitecore-cloudsdk/core");
const custom_event_1 = require("./custom-event");
const sendEvent_1 = require("../send-event/sendEvent");
const initializer_1 = require("../../initializer/browser/initializer");
const custom_event_1 = require("./custom-event");
/**

@@ -15,6 +17,7 @@ * A function that sends a form event to SitecoreCloud API

*/
function form(formId, interactionType) {
const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
async function form(formId, interactionType) {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
const formEvent = new custom_event_1.CustomEvent({
eventApiClient,
eventData: {},

@@ -26,2 +29,3 @@ extensionData: {

id,
sendEvent: sendEvent_1.sendEvent,
settings,

@@ -28,0 +32,0 @@ type: 'FORM',

import { BaseEvent } from '../base-event';
import { ExtensionData, EventAttributesInput } from '../common-interfaces';
import { EventApiClient } from '../../ep/EventApiClient';
import { FlattenedObject } from '@sitecore-cloudsdk/utils';
import { EPResponse, Infer, Settings } from '@sitecore-cloudsdk/core';
import { SendEvent } from '../send-event/sendEvent';
export declare class IdentityEvent extends BaseEvent {
private eventData;
private eventApiClient;
private sendEvent;
private extensionData;
private numberOfExtensionDataProperties;
private settings;
/**

@@ -92,3 +93,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface IdentityEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: IdentityEventAttributesInput;

@@ -95,0 +96,0 @@ extensionData?: ExtensionData;

@@ -8,2 +8,3 @@ "use strict";

const utils_1 = require("@sitecore-cloudsdk/utils");
const consts_2 = require("../../consts");
class IdentityEvent extends base_event_1.BaseEvent {

@@ -16,3 +17,3 @@ /**

const { channel, currency, language, page } = args.eventData;
super({ channel, currency, language, page }, args.settings, args.id);
super({ channel, currency, language, page }, args.id);
this.extensionData = {};

@@ -22,3 +23,4 @@ this.numberOfExtensionDataProperties = 0;

this.eventData = args.eventData;
this.eventApiClient = args.eventApiClient;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
if (args.extensionData)

@@ -28,3 +30,3 @@ this.extensionData = (0, utils_1.flattenObject)({ object: args.extensionData });

if (this.numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
throw new Error(consts_2.ErrorMessages.IV_0005);
}

@@ -37,11 +39,11 @@ /**

if (eventData.identifiers.length === 0)
throw new Error(`[MV-0003] "identifiers" is required.`);
throw new Error(consts_2.ErrorMessages.MV_0003);
if (eventData.dob !== undefined && !(0, utils_1.isShortISODateString)(eventData.dob))
throw new Error(`[IV-0002] Incorrect value for "dob". Format the value according to ISO 8601.`);
throw new Error(consts_2.ErrorMessages.IV_0002);
eventData.identifiers.forEach((identifier) => {
if (identifier.expiryDate && !(0, utils_1.isShortISODateString)(identifier.expiryDate))
throw new Error(`[IV-0004] Incorrect value for "expiryDate". Format the value according to ISO 8601.`);
throw new Error(consts_2.ErrorMessages.IV_0004);
});
if (eventData.email && !(0, utils_1.isValidEmail)(eventData.email))
throw new Error(`[IV-0003] Incorrect value for "email". Set the value to a valid email address.`);
throw new Error(consts_2.ErrorMessages.IV_0003);
}

@@ -90,5 +92,5 @@ /**

const fetchBody = Object.assign({}, eventAttrs, baseAttr);
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}
}
exports.IdentityEvent = IdentityEvent;
"use strict";
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
Object.defineProperty(exports, "__esModule", { value: true });
exports.identity = void 0;
const core_1 = require("@sitecore-cloudsdk/core");
const identity_event_1 = require("./identity-event");
const initializer_1 = require("../../initializer/browser/initializer");
const identity_event_1 = require("./identity-event");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -14,9 +15,11 @@ * A function that sends an IDENTITY event to SitecoreCloud API

*/
function identity(eventData, extensionData) {
const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
async function identity(eventData, extensionData) {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
return new identity_event_1.IdentityEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent: sendEvent_1.sendEvent,
settings,

@@ -23,0 +26,0 @@ }).send();

@@ -6,4 +6,4 @@ "use strict";

const core_1 = require("@sitecore-cloudsdk/core");
const initializer_1 = require("../../initializer/server/initializer");
const identity_event_1 = require("./identity-event");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -18,9 +18,9 @@ * A function that sends an IDENTITY event to SitecoreCloud API

function identityServer(eventData, request, extensionData) {
const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
const settings = (0, core_1.getSettingsServer)();
const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
return new identity_event_1.IdentityEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent: sendEvent_1.sendEvent,
settings: settings,

@@ -27,0 +27,0 @@ }).send();

import { EPResponse, Infer, Settings } from '@sitecore-cloudsdk/core';
import { FlattenedObject, NestedObject } from '@sitecore-cloudsdk/utils';
import { BaseEvent } from '../base-event';
import { EventApiClient } from '../../ep/EventApiClient';
import { EventAttributesInput } from '../common-interfaces';
import { SendEvent } from '../send-event/sendEvent';
export declare class PageViewEvent extends BaseEvent {
static isFirstPageView: boolean;
private eventApiClient;
private sendEvent;
private eventData;

@@ -13,2 +13,3 @@ private extensionData;

private includeUTMParameters;
settings: Settings;
/**

@@ -51,3 +52,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface PageViewEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: PageViewEventInput;

@@ -54,0 +55,0 @@ id: string;

"use strict";
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
Object.defineProperty(exports, "__esModule", { value: true });

@@ -8,2 +7,3 @@ exports.PageViewEvent = void 0;

const consts_1 = require("../consts");
const consts_2 = require("../../consts");
class PageViewEvent extends base_event_1.BaseEvent {

@@ -21,5 +21,7 @@ /**

page,
}, args.settings, args.id);
}, args.id);
this.extensionData = {};
this.eventData = args.eventData;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
this.urlSearchParams = new URLSearchParams(decodeURI(args.searchParams));

@@ -30,4 +32,3 @@ if (args.extensionData)

if (numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
this.eventApiClient = args.eventApiClient;
throw new Error(consts_2.ErrorMessages.IV_0005);
this.includeUTMParameters =

@@ -99,3 +100,3 @@ args.eventData.includeUTMParameters === undefined ? true : args.eventData.includeUTMParameters;

PageViewEvent.isFirstPageView = false;
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}

@@ -102,0 +103,0 @@ /**

@@ -6,4 +6,4 @@ "use strict";

const core_1 = require("@sitecore-cloudsdk/core");
const initializer_1 = require("../../initializer/server/initializer");
const page_view_event_1 = require("./page-view-event");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -18,3 +18,3 @@ * A function that sends a VIEW event to SitecoreCloud API

function pageViewServer(eventData, request, extensionData) {
const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
const settings = (0, core_1.getSettingsServer)();
const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);

@@ -24,3 +24,2 @@ // Host is irrelevant but necessary to support relative URL

return new page_view_event_1.PageViewEvent({
eventApiClient,
eventData,

@@ -30,2 +29,3 @@ extensionData,

searchParams: requestUrl.search,
sendEvent: sendEvent_1.sendEvent,
settings,

@@ -32,0 +32,0 @@ }).send();

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pageView = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const core_1 = require("@sitecore-cloudsdk/core");
const page_view_event_1 = require("./page-view-event");
const initializer_1 = require("../../initializer/browser/initializer");
const page_view_event_1 = require("./page-view-event");
const sendEvent_1 = require("../send-event/sendEvent");
/**

@@ -13,6 +16,7 @@ * A function that sends a VIEW event to SitecoreCloud API

*/
function pageView(eventData, extensionData) {
const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
async function pageView(eventData, extensionData) {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
return new page_view_event_1.PageViewEvent({
eventApiClient,
eventData,

@@ -22,2 +26,3 @@ extensionData,

searchParams: window.location.search,
sendEvent: sendEvent_1.sendEvent,
settings,

@@ -24,0 +29,0 @@ }).send();

@@ -9,2 +9,2 @@ import { CustomEventInput, ExtensionData } from '../events';

*/
export declare function addToEventQueue(type: string, eventData: CustomEventInput, extensionData?: ExtensionData): void;
export declare function addToEventQueue(type: string, eventData: CustomEventInput, extensionData?: ExtensionData): Promise<void>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addToEventQueue = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const initializer_1 = require("../initializer/browser/initializer");
const eventStorage_1 = require("./eventStorage");
const core_1 = require("@sitecore-cloudsdk/core");
/**

@@ -12,4 +15,6 @@ * A function that adds event to the queue

*/
function addToEventQueue(type, eventData, extensionData) {
const { id, settings, eventQueue } = (0, initializer_1.getDependencies)();
async function addToEventQueue(type, eventData, extensionData) {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
const queueEventPayload = {

@@ -22,4 +27,4 @@ eventData,

};
eventQueue.enqueueEvent(queueEventPayload);
eventStorage_1.eventQueue.enqueueEvent(queueEventPayload);
}
exports.addToEventQueue = addToEventQueue;
/**
* Deletes the queue from session.
*/
export declare function clearEventQueue(): void;
export declare function clearEventQueue(): Promise<void>;

@@ -6,9 +6,10 @@ "use strict";

const initializer_1 = require("../initializer/browser/initializer");
const eventStorage_1 = require("./eventStorage");
/**
* Deletes the queue from session.
*/
function clearEventQueue() {
const { eventQueue } = (0, initializer_1.getDependencies)();
eventQueue.clearQueue();
async function clearEventQueue() {
await (0, initializer_1.awaitInit)();
eventStorage_1.eventQueue.clearQueue();
}
exports.clearEventQueue = clearEventQueue;

@@ -1,14 +0,7 @@

import { EventApiClient } from '../ep/EventApiClient';
import { CustomEventArguments } from '../events';
export declare class EventQueue {
private storage;
private eventApiClient;
declare class EventQueue {
/**
* Initialize the Event Storage
* @param storage - Interface that describes the storage functionality
* @param eventApiClient - The API client which sends events to EP
* @param infer - The instance of the infer class
*/
private key;
constructor(storage: Storage, eventApiClient: EventApiClient);
/** Returns the stored array of data with type QueueEventPayload, or empty array if the given key does not exist. */

@@ -30,2 +23,3 @@ private getEventQueue;

clearQueue(): void;
private getSessionStorage;
}

@@ -41,1 +35,3 @@ /**

export type QueueEventPayload = Pick<CustomEventArguments, 'eventData' | 'extensionData' | 'type' | 'settings' | 'id'>;
export declare const eventQueue: EventQueue;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventQueue = void 0;
exports.eventQueue = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const core_1 = require("@sitecore-cloudsdk/core");
const events_1 = require("../events");
const sendEvent_1 = require("../events/send-event/sendEvent");
class EventQueue {
constructor(storage, eventApiClient) {
this.storage = storage;
this.eventApiClient = eventApiClient;
constructor() {
/**
* Initialize the Event Storage
* @param storage - Interface that describes the storage functionality
* @param eventApiClient - The API client which sends events to EP
* @param infer - The instance of the infer class
*/

@@ -21,5 +17,4 @@ this.key = 'EventQueue';

getEventQueue() {
const storedQueue = this.storage.getItem(this.key);
if (!storedQueue)
return [];
const sessionStorage = this.getSessionStorage();
const storedQueue = sessionStorage.getItem(this.key) ?? '""';
try {

@@ -39,6 +34,7 @@ const parsedQueueEvent = JSON.parse(storedQueue);

enqueueEvent(queueEventPayload) {
const sessionStorage = this.getSessionStorage();
queueEventPayload.eventData.page = queueEventPayload.eventData.page ?? (0, core_1.pageName)();
queueEventPayload.eventData.language = queueEventPayload.eventData.language ?? (0, core_1.language)();
new events_1.CustomEvent({
eventApiClient: this.eventApiClient,
sendEvent: sendEvent_1.sendEvent,
...queueEventPayload,

@@ -48,3 +44,3 @@ });

eventQueue.push(queueEventPayload);
this.storage.setItem(this.key, JSON.stringify(eventQueue));
sessionStorage.setItem(this.key, JSON.stringify(eventQueue));
}

@@ -58,6 +54,6 @@ /**

await new events_1.CustomEvent({
eventApiClient: this.eventApiClient,
eventData: queueEventPayload.eventData,
extensionData: queueEventPayload.extensionData,
id: queueEventPayload.id,
sendEvent: sendEvent_1.sendEvent,
settings: queueEventPayload.settings,

@@ -73,5 +69,9 @@ type: queueEventPayload.type,

clearQueue() {
this.storage.removeItem(this.key);
const sessionStorage = this.getSessionStorage();
sessionStorage.removeItem(this.key);
}
getSessionStorage() {
return sessionStorage;
}
}
exports.EventQueue = EventQueue;
exports.eventQueue = new EventQueue();

@@ -5,2 +5,2 @@ /**

*/
export declare function processEventQueue(): void;
export declare function processEventQueue(): Promise<void>;

@@ -6,2 +6,3 @@ "use strict";

const initializer_1 = require("../initializer/browser/initializer");
const eventStorage_1 = require("./eventStorage");
/**

@@ -11,6 +12,6 @@ * A function that sends all queue events to SitecoreCloud API.

*/
function processEventQueue() {
const { eventQueue } = (0, initializer_1.getDependencies)();
eventQueue.sendAllEvents();
async function processEventQueue() {
await (0, initializer_1.awaitInit)();
eventStorage_1.eventQueue.sendAllEvents();
}
exports.processEventQueue = processEventQueue;
"use strict";
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGuestId = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const initializer_1 = require("../initializer/browser/initializer");
const core_1 = require("@sitecore-cloudsdk/core");
const initializer_1 = require("../initializer/browser/initializer");
/**

@@ -12,6 +12,8 @@ * A function that returns the guest id.

*/
function getGuestId() {
const { id, settings } = (0, initializer_1.getDependencies)();
async function getGuestId() {
await (0, initializer_1.awaitInit)();
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
return (0, core_1.getGuestId)(id, settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl);
}
exports.getGuestId = getGuestId;

@@ -1,21 +0,4 @@

import { EventApiClient } from '../../ep/EventApiClient';
import { Settings, SettingsParamsBrowser } from '@sitecore-cloudsdk/core';
import { EventQueue } from '../../eventStorage/eventStorage';
export declare function setDependencies(settings: BrowserEventsSettings | null): void;
import { SettingsParamsBrowser } from '@sitecore-cloudsdk/core';
export declare let initPromise: Promise<void> | null;
/**
* Retrieves the browser event settings object.
*
* This function ensures that the browser event settings have been initialized and contain essential properties like `settings`, `eventQueue`, and `eventApiClient`.
*
* @returns The browser event settings object.
* @throws Error if the event settings haven't been initialized with the required properties.
*/
export declare function getDependencies(): BrowserEventsSettings;
export interface BrowserEventsSettings {
id: string;
settings: Settings;
eventQueue: EventQueue;
eventApiClient: EventApiClient;
}
/**
* Initiates the Events library using the global settings added by the developer

@@ -26,1 +9,5 @@ * @param settingsInput - Global settings added by the developer

export declare function init(settingsInput: SettingsParamsBrowser): Promise<void>;
/**
* A function that handles the async browser init logic. Throws an error or awaits the promise.
*/
export declare function awaitInit(): Promise<void>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.awaitInit = exports.init = exports.initPromise = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = exports.getDependencies = exports.setDependencies = void 0;
const EventApiClient_1 = require("../../ep/EventApiClient");
const core_1 = require("@sitecore-cloudsdk/core");
const consts_1 = require("../../consts");
const eventStorage_1 = require("../../eventStorage/eventStorage");
let dependencies = null;
function setDependencies(settings) {
dependencies = settings;
}
exports.setDependencies = setDependencies;
exports.initPromise = null;
/**
* Retrieves the browser event settings object.
*
* This function ensures that the browser event settings have been initialized and contain essential properties like `settings`, `eventQueue`, and `eventApiClient`.
*
* @returns The browser event settings object.
* @throws Error if the event settings haven't been initialized with the required properties.
*/
function getDependencies() {
if (!dependencies) {
throw Error(`[IE-0004] You must first initialize the "events/browser" module. Run the "init" function.`);
}
return dependencies;
}
exports.getDependencies = getDependencies;
/**
* Initiates the Events library using the global settings added by the developer

@@ -35,18 +14,12 @@ * @param settingsInput - Global settings added by the developer

async function init(settingsInput) {
if (typeof window === 'undefined') {
throw new Error(
// eslint-disable-next-line max-len
`[IE-0001] The "window" object is not available on the server side. Use the "window" object only on the client side, and in the correct execution context.`);
if (typeof window === 'undefined')
throw new Error(consts_1.ErrorMessages.IE_0001);
try {
exports.initPromise = (0, core_1.initCore)(settingsInput);
await exports.initPromise;
}
await (0, core_1.initCore)(settingsInput);
const settings = (0, core_1.getSettings)();
const id = (0, core_1.getBrowserId)();
const eventApiClient = new EventApiClient_1.EventApiClient(settings.sitecoreEdgeUrl, settingsInput.sitecoreEdgeContextId, settingsInput.siteName);
const eventQueue = new eventStorage_1.EventQueue(sessionStorage, eventApiClient);
setDependencies({
eventApiClient,
eventQueue,
id,
settings,
});
catch (error) {
exports.initPromise = null;
throw new Error(error);
}
window.Engage = {

@@ -62,1 +35,10 @@ ...window.Engage,

exports.init = init;
/**
* A function that handles the async browser init logic. Throws an error or awaits the promise.
*/
async function awaitInit() {
if (exports.initPromise === null)
throw new Error(consts_1.ErrorMessages.IE_0004);
await exports.initPromise;
}
exports.awaitInit = awaitInit;

@@ -1,15 +0,4 @@

import { Settings, SettingsParamsServer } from '@sitecore-cloudsdk/core';
import { EventApiClient } from '../../ep/EventApiClient';
import { SettingsParamsServer } from '@sitecore-cloudsdk/core';
import { HttpResponse, MiddlewareNextResponse, Request } from '@sitecore-cloudsdk/utils';
export declare function setServerDependencies(settings: ServerEventsSettings | null): void;
/**
* Retrieves the server event settings object.
*
* This function ensures that the server event settings have been initialized and contain essential properties like `settings` and `eventApiClient`.
*
* @returns The server event settings object.
* @throws Error if the server event settings haven't been initialized with the required properties.
*/
export declare function getServerDependencies(): ServerEventsSettings;
/**
* Initiates the server Events library using the global settings added by the developer

@@ -20,5 +9,1 @@ * @param settings - Global settings added by the developer

export declare function initServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: SettingsParamsServer, request: Request, response: Response): Promise<void>;
export interface ServerEventsSettings {
settings: Settings;
eventApiClient: EventApiClient;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initServer = exports.getServerDependencies = exports.setServerDependencies = void 0;
exports.initServer = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const core_1 = require("@sitecore-cloudsdk/core");
const EventApiClient_1 = require("../../ep/EventApiClient");
let serverDependencies = null;
function setServerDependencies(settings) {
serverDependencies = settings;
}
exports.setServerDependencies = setServerDependencies;
/**
* Retrieves the server event settings object.
*
* This function ensures that the server event settings have been initialized and contain essential properties like `settings` and `eventApiClient`.
*
* @returns The server event settings object.
* @throws Error if the server event settings haven't been initialized with the required properties.
*/
function getServerDependencies() {
if (!serverDependencies) {
throw Error(`[IE-0005] You must first initialize the "events/server" module. Run the "init" function.`);
}
return serverDependencies;
}
exports.getServerDependencies = getServerDependencies;
/**
* Initiates the server Events library using the global settings added by the developer

@@ -34,9 +13,3 @@ * @param settings - Global settings added by the developer

await (0, core_1.initCoreServer)(settingsInput, request, response);
const settings = (0, core_1.getSettingsServer)();
const eventApiClient = new EventApiClient_1.EventApiClient(settings.sitecoreEdgeUrl, settings.sitecoreEdgeContextId, settings.siteName);
setServerDependencies({
eventApiClient,
settings,
});
}
exports.initServer = initServer;

@@ -7,2 +7,1 @@ export { initServer as init } from './lib/initializer/server/initializer';

export type { PageViewEventInput, CustomEventInput, IdentityEventAttributesInput } from './lib/events';
export type { ServerEventsSettings as EventsSettings } from './lib/initializer/server/initializer';
{
"name": "@sitecore-cloudsdk/events",
"version": "0.1.3",
"version": "0.2.0",
"license": "Apache-2.0",

@@ -35,3 +35,7 @@ "exports": {

"server.d.ts"
]
],
"engines": {
"node": ">=18",
"npm": ">=9"
}
}

@@ -5,1 +5,11 @@ /**

export declare const LIBRARY_VERSION: string;
export declare enum ErrorMessages {
IE_0001 = "[IE-0001] The \"window\" object is not available on the server side. Use the \"window\" object only on the client side, and in the correct execution context.",
IE_0004 = "[IE-0004] You must first initialize the \"events/browser\" module. Run the \"init\" function.",
IE_0005 = "[IE-0005] You must first initialize the \"events/server\" module. Run the \"init\" function.",
IV_0002 = "[IV-0002] Incorrect value for \"dob\". Format the value according to ISO 8601.",
IV_0003 = "[IV-0003] Incorrect value for \"email\". Set the value to a valid email address.",
IV_0004 = "[IV-0004] Incorrect value for \"expiryDate\". Format the value according to ISO 8601.",
IV_0005 = "[IV-0005] This event supports maximum 50 attributes. Reduce the number of attributes.",
MV_0003 = "[MV-0003] \"identifiers\" is required."
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
/* eslint-disable @typescript-eslint/naming-convention */
import packageJson from '../../package.json';
import { MAX_EXT_ATTRIBUTES } from './events/consts';
/**

@@ -8,1 +9,12 @@ * Returns the version of the library.

export const LIBRARY_VERSION = packageJson.version;
export var ErrorMessages;
(function (ErrorMessages) {
ErrorMessages["IE_0001"] = "[IE-0001] The \"window\" object is not available on the server side. Use the \"window\" object only on the client side, and in the correct execution context.";
ErrorMessages["IE_0004"] = "[IE-0004] You must first initialize the \"events/browser\" module. Run the \"init\" function.";
ErrorMessages["IE_0005"] = "[IE-0005] You must first initialize the \"events/server\" module. Run the \"init\" function.";
ErrorMessages["IV_0002"] = "[IV-0002] Incorrect value for \"dob\". Format the value according to ISO 8601.";
ErrorMessages["IV_0003"] = "[IV-0003] Incorrect value for \"email\". Set the value to a valid email address.";
ErrorMessages["IV_0004"] = "[IV-0004] Incorrect value for \"expiryDate\". Format the value according to ISO 8601.";
ErrorMessages["IV_0005"] = "[IV-0005] This event supports maximum 50 attributes. Reduce the number of attributes.";
ErrorMessages["MV_0003"] = "[MV-0003] \"identifiers\" is required.";
})(ErrorMessages || (ErrorMessages = {}));

@@ -1,6 +0,4 @@

import type { Settings } from '@sitecore-cloudsdk/core';
import { EventAttributesInput } from './common-interfaces';
export declare class BaseEvent {
private baseEventData;
protected settings: Settings;
private readonly browserId;

@@ -16,3 +14,3 @@ private readonly language;

*/
constructor(baseEventData: BaseEventData, settings: Settings, id: string);
constructor(baseEventData: BaseEventData, id: string);
/**

@@ -19,0 +17,0 @@ * A function that returns the properties for sending events to Sitecore EP

@@ -11,5 +11,4 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

*/
constructor(baseEventData, settings, id) {
constructor(baseEventData, id) {
this.baseEventData = baseEventData;
this.settings = settings;
this.browserId = id;

@@ -16,0 +15,0 @@ this.language = this.baseEventData.language ?? language();

import { BaseEvent } from '../base-event';
import { EventAttributesInput } from '../common-interfaces';
import { EventApiClient } from '../../ep/EventApiClient';
import { SendEvent } from '../send-event/sendEvent';
import { EPResponse, Settings } from '@sitecore-cloudsdk/core';

@@ -8,4 +8,5 @@ import { BasicTypes, NestedObject } from '@sitecore-cloudsdk/utils';

customEventPayload: CustomEventPayload;
private eventApiClient;
private sendEvent;
private extensionData;
private settings;
/**

@@ -26,3 +27,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface CustomEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: CustomEventData;

@@ -29,0 +30,0 @@ id: string;

@@ -5,2 +5,3 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

import { MAX_EXT_ATTRIBUTES } from '../consts';
import { ErrorMessages } from '../../consts';
export class CustomEvent extends BaseEvent {

@@ -13,5 +14,6 @@ /**

const { channel, currency, language, page, ...rest } = args.eventData;
super({ channel, currency, language, page }, args.settings, args.id);
super({ channel, currency, language, page }, args.id);
this.extensionData = {};
this.eventApiClient = args.eventApiClient;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
this.customEventPayload = {

@@ -25,3 +27,3 @@ type: args.type,

if (numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
throw new Error(ErrorMessages.IV_0005);
if (numberOfExtensionDataProperties > 0)

@@ -37,4 +39,4 @@ this.customEventPayload.ext = this.extensionData;

const fetchBody = Object.assign({}, this.customEventPayload, baseAttr);
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserId, getSettings } from '@sitecore-cloudsdk/core';
import { CustomEvent } from './custom-event';
import { getDependencies } from '../../initializer/browser/initializer';
import { awaitInit } from '../../initializer/browser/initializer';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -12,9 +14,11 @@ * A function that sends an event to SitecoreCloud API with the specified type

*/
export function event(type, eventData, extensionData) {
const { eventApiClient, id, settings } = getDependencies();
export async function event(type, eventData, extensionData) {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
return new CustomEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent,
settings,

@@ -21,0 +25,0 @@ type,

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserIdFromRequest } from '@sitecore-cloudsdk/core';
import { getBrowserIdFromRequest, getSettingsServer } from '@sitecore-cloudsdk/core';
import { CustomEvent } from './custom-event';
import { getServerDependencies } from '../../initializer/server/initializer';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -14,12 +14,12 @@ * A function that sends an event to SitecoreCloud API with the specified type

export function eventServer(type, eventData, request, extensionData) {
const { eventApiClient, settings } = getServerDependencies();
const settings = getSettingsServer();
const id = getBrowserIdFromRequest(request, settings.cookieSettings.cookieName);
return new CustomEvent({
eventApiClient,
eventData,
extensionData,
id,
settings: settings,
sendEvent,
settings,
type,
}).send();
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getDependencies } from '../../initializer/browser/initializer';
import { getBrowserId, getSettings } from '@sitecore-cloudsdk/core';
import { CustomEvent } from './custom-event';
import { sendEvent } from '../send-event/sendEvent';
import { awaitInit } from '../../initializer/browser/initializer';
/**

@@ -11,6 +13,7 @@ * A function that sends a form event to SitecoreCloud API

*/
export function form(formId, interactionType) {
const { eventApiClient, id, settings } = getDependencies();
export async function form(formId, interactionType) {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
const formEvent = new CustomEvent({
eventApiClient,
eventData: {},

@@ -22,2 +25,3 @@ extensionData: {

id,
sendEvent,
settings,

@@ -24,0 +28,0 @@ type: 'FORM',

import { BaseEvent } from '../base-event';
import { ExtensionData, EventAttributesInput } from '../common-interfaces';
import { EventApiClient } from '../../ep/EventApiClient';
import { FlattenedObject } from '@sitecore-cloudsdk/utils';
import { EPResponse, Infer, Settings } from '@sitecore-cloudsdk/core';
import { SendEvent } from '../send-event/sendEvent';
export declare class IdentityEvent extends BaseEvent {
private eventData;
private eventApiClient;
private sendEvent;
private extensionData;
private numberOfExtensionDataProperties;
private settings;
/**

@@ -92,3 +93,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface IdentityEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: IdentityEventAttributesInput;

@@ -95,0 +96,0 @@ extensionData?: ExtensionData;

@@ -5,2 +5,3 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

import { isShortISODateString, isValidEmail, flattenObject } from '@sitecore-cloudsdk/utils';
import { ErrorMessages } from '../../consts';
export class IdentityEvent extends BaseEvent {

@@ -13,3 +14,3 @@ /**

const { channel, currency, language, page } = args.eventData;
super({ channel, currency, language, page }, args.settings, args.id);
super({ channel, currency, language, page }, args.id);
this.extensionData = {};

@@ -19,3 +20,4 @@ this.numberOfExtensionDataProperties = 0;

this.eventData = args.eventData;
this.eventApiClient = args.eventApiClient;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
if (args.extensionData)

@@ -25,3 +27,3 @@ this.extensionData = flattenObject({ object: args.extensionData });

if (this.numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
throw new Error(ErrorMessages.IV_0005);
}

@@ -34,11 +36,11 @@ /**

if (eventData.identifiers.length === 0)
throw new Error(`[MV-0003] "identifiers" is required.`);
throw new Error(ErrorMessages.MV_0003);
if (eventData.dob !== undefined && !isShortISODateString(eventData.dob))
throw new Error(`[IV-0002] Incorrect value for "dob". Format the value according to ISO 8601.`);
throw new Error(ErrorMessages.IV_0002);
eventData.identifiers.forEach((identifier) => {
if (identifier.expiryDate && !isShortISODateString(identifier.expiryDate))
throw new Error(`[IV-0004] Incorrect value for "expiryDate". Format the value according to ISO 8601.`);
throw new Error(ErrorMessages.IV_0004);
});
if (eventData.email && !isValidEmail(eventData.email))
throw new Error(`[IV-0003] Incorrect value for "email". Set the value to a valid email address.`);
throw new Error(ErrorMessages.IV_0003);
}

@@ -87,4 +89,4 @@ /**

const fetchBody = Object.assign({}, eventAttrs, baseAttr);
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}
}

@@ -1,4 +0,5 @@

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getDependencies } from '../../initializer/browser/initializer';
import { getBrowserId, getSettings } from '@sitecore-cloudsdk/core';
import { IdentityEvent } from './identity-event';
import { awaitInit } from '../../initializer/browser/initializer';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -11,11 +12,13 @@ * A function that sends an IDENTITY event to SitecoreCloud API

*/
export function identity(eventData, extensionData) {
const { eventApiClient, id, settings } = getDependencies();
export async function identity(eventData, extensionData) {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
return new IdentityEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent,
settings,
}).send();
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserIdFromRequest } from '@sitecore-cloudsdk/core';
import { getServerDependencies } from '../../initializer/server/initializer';
import { getBrowserIdFromRequest, getSettingsServer } from '@sitecore-cloudsdk/core';
import { IdentityEvent } from './identity-event';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -14,11 +14,11 @@ * A function that sends an IDENTITY event to SitecoreCloud API

export function identityServer(eventData, request, extensionData) {
const { eventApiClient, settings } = getServerDependencies();
const settings = getSettingsServer();
const id = getBrowserIdFromRequest(request, settings.cookieSettings.cookieName);
return new IdentityEvent({
eventApiClient,
eventData,
extensionData,
id,
sendEvent,
settings: settings,
}).send();
}
import { EPResponse, Infer, Settings } from '@sitecore-cloudsdk/core';
import { FlattenedObject, NestedObject } from '@sitecore-cloudsdk/utils';
import { BaseEvent } from '../base-event';
import { EventApiClient } from '../../ep/EventApiClient';
import { EventAttributesInput } from '../common-interfaces';
import { SendEvent } from '../send-event/sendEvent';
export declare class PageViewEvent extends BaseEvent {
static isFirstPageView: boolean;
private eventApiClient;
private sendEvent;
private eventData;

@@ -13,2 +13,3 @@ private extensionData;

private includeUTMParameters;
settings: Settings;
/**

@@ -51,3 +52,3 @@ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event

export interface PageViewEventArguments {
eventApiClient: EventApiClient;
sendEvent: SendEvent;
eventData: PageViewEventInput;

@@ -54,0 +55,0 @@ id: string;

@@ -1,5 +0,5 @@

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { flattenObject } from '@sitecore-cloudsdk/utils';
import { BaseEvent } from '../base-event';
import { MAX_EXT_ATTRIBUTES, UTM_PREFIX } from '../consts';
import { ErrorMessages } from '../../consts';
export class PageViewEvent extends BaseEvent {

@@ -17,5 +17,7 @@ /**

page,
}, args.settings, args.id);
}, args.id);
this.extensionData = {};
this.eventData = args.eventData;
this.sendEvent = args.sendEvent;
this.settings = args.settings;
this.urlSearchParams = new URLSearchParams(decodeURI(args.searchParams));

@@ -26,4 +28,3 @@ if (args.extensionData)

if (numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
throw new Error(`[IV-0005] This event supports maximum ${MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
this.eventApiClient = args.eventApiClient;
throw new Error(ErrorMessages.IV_0005);
this.includeUTMParameters =

@@ -95,3 +96,3 @@ args.eventData.includeUTMParameters === undefined ? true : args.eventData.includeUTMParameters;

PageViewEvent.isFirstPageView = false;
return await this.eventApiClient.send(fetchBody);
return await this.sendEvent(fetchBody, this.settings);
}

@@ -98,0 +99,0 @@ /**

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserIdFromRequest } from '@sitecore-cloudsdk/core';
import { getServerDependencies } from '../../initializer/server/initializer';
import { getBrowserIdFromRequest, getSettingsServer } from '@sitecore-cloudsdk/core';
import { PageViewEvent } from './page-view-event';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -14,3 +14,3 @@ * A function that sends a VIEW event to SitecoreCloud API

export function pageViewServer(eventData, request, extensionData) {
const { eventApiClient, settings } = getServerDependencies();
const settings = getSettingsServer();
const id = getBrowserIdFromRequest(request, settings.cookieSettings.cookieName);

@@ -20,3 +20,2 @@ // Host is irrelevant but necessary to support relative URL

return new PageViewEvent({
eventApiClient,
eventData,

@@ -26,4 +25,5 @@ extensionData,

searchParams: requestUrl.search,
sendEvent,
settings,
}).send();
}

@@ -1,3 +0,6 @@

import { getDependencies } from '../../initializer/browser/initializer';
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserId, getSettings } from '@sitecore-cloudsdk/core';
import { PageViewEvent } from './page-view-event';
import { awaitInit } from '../../initializer/browser/initializer';
import { sendEvent } from '../send-event/sendEvent';
/**

@@ -10,6 +13,7 @@ * A function that sends a VIEW event to SitecoreCloud API

*/
export function pageView(eventData, extensionData) {
const { eventApiClient, id, settings } = getDependencies();
export async function pageView(eventData, extensionData) {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
return new PageViewEvent({
eventApiClient,
eventData,

@@ -19,4 +23,5 @@ extensionData,

searchParams: window.location.search,
sendEvent,
settings,
}).send();
}

@@ -9,2 +9,2 @@ import { CustomEventInput, ExtensionData } from '../events';

*/
export declare function addToEventQueue(type: string, eventData: CustomEventInput, extensionData?: ExtensionData): void;
export declare function addToEventQueue(type: string, eventData: CustomEventInput, extensionData?: ExtensionData): Promise<void>;

@@ -1,2 +0,5 @@

import { getDependencies } from '../initializer/browser/initializer';
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { awaitInit } from '../initializer/browser/initializer';
import { eventQueue } from './eventStorage';
import { getBrowserId, getSettings } from '@sitecore-cloudsdk/core';
/**

@@ -9,4 +12,6 @@ * A function that adds event to the queue

*/
export function addToEventQueue(type, eventData, extensionData) {
const { id, settings, eventQueue } = getDependencies();
export async function addToEventQueue(type, eventData, extensionData) {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
const queueEventPayload = {

@@ -13,0 +18,0 @@ eventData,

/**
* Deletes the queue from session.
*/
export declare function clearEventQueue(): void;
export declare function clearEventQueue(): Promise<void>;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getDependencies } from '../initializer/browser/initializer';
import { awaitInit } from '../initializer/browser/initializer';
import { eventQueue } from './eventStorage';
/**
* Deletes the queue from session.
*/
export function clearEventQueue() {
const { eventQueue } = getDependencies();
export async function clearEventQueue() {
await awaitInit();
eventQueue.clearQueue();
}

@@ -1,14 +0,7 @@

import { EventApiClient } from '../ep/EventApiClient';
import { CustomEventArguments } from '../events';
export declare class EventQueue {
private storage;
private eventApiClient;
declare class EventQueue {
/**
* Initialize the Event Storage
* @param storage - Interface that describes the storage functionality
* @param eventApiClient - The API client which sends events to EP
* @param infer - The instance of the infer class
*/
private key;
constructor(storage: Storage, eventApiClient: EventApiClient);
/** Returns the stored array of data with type QueueEventPayload, or empty array if the given key does not exist. */

@@ -30,2 +23,3 @@ private getEventQueue;

clearQueue(): void;
private getSessionStorage;
}

@@ -41,1 +35,3 @@ /**

export type QueueEventPayload = Pick<CustomEventArguments, 'eventData' | 'extensionData' | 'type' | 'settings' | 'id'>;
export declare const eventQueue: EventQueue;
export {};
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { pageName, language } from '@sitecore-cloudsdk/core';
import { CustomEvent } from '../events';
export class EventQueue {
constructor(storage, eventApiClient) {
this.storage = storage;
this.eventApiClient = eventApiClient;
import { sendEvent } from '../events/send-event/sendEvent';
class EventQueue {
constructor() {
/**
* Initialize the Event Storage
* @param storage - Interface that describes the storage functionality
* @param eventApiClient - The API client which sends events to EP
* @param infer - The instance of the infer class
*/

@@ -18,5 +14,4 @@ this.key = 'EventQueue';

getEventQueue() {
const storedQueue = this.storage.getItem(this.key);
if (!storedQueue)
return [];
const sessionStorage = this.getSessionStorage();
const storedQueue = sessionStorage.getItem(this.key) ?? '""';
try {

@@ -36,6 +31,7 @@ const parsedQueueEvent = JSON.parse(storedQueue);

enqueueEvent(queueEventPayload) {
const sessionStorage = this.getSessionStorage();
queueEventPayload.eventData.page = queueEventPayload.eventData.page ?? pageName();
queueEventPayload.eventData.language = queueEventPayload.eventData.language ?? language();
new CustomEvent({
eventApiClient: this.eventApiClient,
sendEvent,
...queueEventPayload,

@@ -45,3 +41,3 @@ });

eventQueue.push(queueEventPayload);
this.storage.setItem(this.key, JSON.stringify(eventQueue));
sessionStorage.setItem(this.key, JSON.stringify(eventQueue));
}

@@ -55,6 +51,6 @@ /**

await new CustomEvent({
eventApiClient: this.eventApiClient,
eventData: queueEventPayload.eventData,
extensionData: queueEventPayload.extensionData,
id: queueEventPayload.id,
sendEvent,
settings: queueEventPayload.settings,

@@ -70,4 +66,9 @@ type: queueEventPayload.type,

clearQueue() {
this.storage.removeItem(this.key);
const sessionStorage = this.getSessionStorage();
sessionStorage.removeItem(this.key);
}
getSessionStorage() {
return sessionStorage;
}
}
export const eventQueue = new EventQueue();

@@ -5,2 +5,2 @@ /**

*/
export declare function processEventQueue(): void;
export declare function processEventQueue(): Promise<void>;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getDependencies } from '../initializer/browser/initializer';
import { awaitInit } from '../initializer/browser/initializer';
import { eventQueue } from './eventStorage';
/**

@@ -7,5 +8,5 @@ * A function that sends all queue events to SitecoreCloud API.

*/
export function processEventQueue() {
const { eventQueue } = getDependencies();
export async function processEventQueue() {
await awaitInit();
eventQueue.sendAllEvents();
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getGuestId as getGuestIdFromCore } from '@sitecore-cloudsdk/core';
import { getDependencies } from '../initializer/browser/initializer';
import { awaitInit } from '../initializer/browser/initializer';
import { getBrowserId, getGuestId as getGuestIdFromCore, getSettings } from '@sitecore-cloudsdk/core';
/**

@@ -9,5 +9,7 @@ * A function that returns the guest id.

*/
export function getGuestId() {
const { id, settings } = getDependencies();
export async function getGuestId() {
await awaitInit();
const settings = getSettings();
const id = getBrowserId();
return getGuestIdFromCore(id, settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl);
}

@@ -1,21 +0,4 @@

import { EventApiClient } from '../../ep/EventApiClient';
import { Settings, SettingsParamsBrowser } from '@sitecore-cloudsdk/core';
import { EventQueue } from '../../eventStorage/eventStorage';
export declare function setDependencies(settings: BrowserEventsSettings | null): void;
import { SettingsParamsBrowser } from '@sitecore-cloudsdk/core';
export declare let initPromise: Promise<void> | null;
/**
* Retrieves the browser event settings object.
*
* This function ensures that the browser event settings have been initialized and contain essential properties like `settings`, `eventQueue`, and `eventApiClient`.
*
* @returns The browser event settings object.
* @throws Error if the event settings haven't been initialized with the required properties.
*/
export declare function getDependencies(): BrowserEventsSettings;
export interface BrowserEventsSettings {
id: string;
settings: Settings;
eventQueue: EventQueue;
eventApiClient: EventApiClient;
}
/**
* Initiates the Events library using the global settings added by the developer

@@ -26,1 +9,5 @@ * @param settingsInput - Global settings added by the developer

export declare function init(settingsInput: SettingsParamsBrowser): Promise<void>;
/**
* A function that handles the async browser init logic. Throws an error or awaits the promise.
*/
export declare function awaitInit(): Promise<void>;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { EventApiClient } from '../../ep/EventApiClient';
import { getBrowserId, getSettings, initCore } from '@sitecore-cloudsdk/core';
import { LIBRARY_VERSION } from '../../consts';
import { EventQueue } from '../../eventStorage/eventStorage';
let dependencies = null;
export function setDependencies(settings) {
dependencies = settings;
}
import { getBrowserId, initCore } from '@sitecore-cloudsdk/core';
import { ErrorMessages, LIBRARY_VERSION } from '../../consts';
export let initPromise = null;
/**
* Retrieves the browser event settings object.
*
* This function ensures that the browser event settings have been initialized and contain essential properties like `settings`, `eventQueue`, and `eventApiClient`.
*
* @returns The browser event settings object.
* @throws Error if the event settings haven't been initialized with the required properties.
*/
export function getDependencies() {
if (!dependencies) {
throw Error(`[IE-0004] You must first initialize the "events/browser" module. Run the "init" function.`);
}
return dependencies;
}
/**
* Initiates the Events library using the global settings added by the developer

@@ -30,18 +11,12 @@ * @param settingsInput - Global settings added by the developer

export async function init(settingsInput) {
if (typeof window === 'undefined') {
throw new Error(
// eslint-disable-next-line max-len
`[IE-0001] The "window" object is not available on the server side. Use the "window" object only on the client side, and in the correct execution context.`);
if (typeof window === 'undefined')
throw new Error(ErrorMessages.IE_0001);
try {
initPromise = initCore(settingsInput);
await initPromise;
}
await initCore(settingsInput);
const settings = getSettings();
const id = getBrowserId();
const eventApiClient = new EventApiClient(settings.sitecoreEdgeUrl, settingsInput.sitecoreEdgeContextId, settingsInput.siteName);
const eventQueue = new EventQueue(sessionStorage, eventApiClient);
setDependencies({
eventApiClient,
eventQueue,
id,
settings,
});
catch (error) {
initPromise = null;
throw new Error(error);
}
window.Engage = {

@@ -56,1 +31,9 @@ ...window.Engage,

}
/**
* A function that handles the async browser init logic. Throws an error or awaits the promise.
*/
export async function awaitInit() {
if (initPromise === null)
throw new Error(ErrorMessages.IE_0004);
await initPromise;
}

@@ -1,15 +0,4 @@

import { Settings, SettingsParamsServer } from '@sitecore-cloudsdk/core';
import { EventApiClient } from '../../ep/EventApiClient';
import { SettingsParamsServer } from '@sitecore-cloudsdk/core';
import { HttpResponse, MiddlewareNextResponse, Request } from '@sitecore-cloudsdk/utils';
export declare function setServerDependencies(settings: ServerEventsSettings | null): void;
/**
* Retrieves the server event settings object.
*
* This function ensures that the server event settings have been initialized and contain essential properties like `settings` and `eventApiClient`.
*
* @returns The server event settings object.
* @throws Error if the server event settings haven't been initialized with the required properties.
*/
export declare function getServerDependencies(): ServerEventsSettings;
/**
* Initiates the server Events library using the global settings added by the developer

@@ -20,5 +9,1 @@ * @param settings - Global settings added by the developer

export declare function initServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: SettingsParamsServer, request: Request, response: Response): Promise<void>;
export interface ServerEventsSettings {
settings: Settings;
eventApiClient: EventApiClient;
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getSettingsServer, initCoreServer } from '@sitecore-cloudsdk/core';
import { EventApiClient } from '../../ep/EventApiClient';
let serverDependencies = null;
export function setServerDependencies(settings) {
serverDependencies = settings;
}
import { initCoreServer } from '@sitecore-cloudsdk/core';
/**
* Retrieves the server event settings object.
*
* This function ensures that the server event settings have been initialized and contain essential properties like `settings` and `eventApiClient`.
*
* @returns The server event settings object.
* @throws Error if the server event settings haven't been initialized with the required properties.
*/
export function getServerDependencies() {
if (!serverDependencies) {
throw Error(`[IE-0005] You must first initialize the "events/server" module. Run the "init" function.`);
}
return serverDependencies;
}
/**
* Initiates the server Events library using the global settings added by the developer

@@ -29,8 +10,2 @@ * @param settings - Global settings added by the developer

await initCoreServer(settingsInput, request, response);
const settings = getSettingsServer();
const eventApiClient = new EventApiClient(settings.sitecoreEdgeUrl, settings.sitecoreEdgeContextId, settings.siteName);
setServerDependencies({
eventApiClient,
settings,
});
}

@@ -7,2 +7,1 @@ export { initServer as init } from './lib/initializer/server/initializer';

export type { PageViewEventInput, CustomEventInput, IdentityEventAttributesInput } from './lib/events';
export type { ServerEventsSettings as EventsSettings } from './lib/initializer/server/initializer';
{
"name": "@sitecore-cloudsdk/events",
"version": "0.1.3",
"version": "0.2.0",
"license": "Apache-2.0",

@@ -35,3 +35,7 @@ "exports": {

"server.d.ts"
]
],
"engines": {
"node": ">=18",
"npm": ">=9"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc