@sitecore-cloudsdk/events 0.1.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/package.json +31 -0
  3. package/dist/cjs/src/browser.d.ts +10 -0
  4. package/dist/cjs/src/browser.js +22 -0
  5. package/dist/cjs/src/index.d.ts +4 -0
  6. package/dist/cjs/src/index.js +24 -0
  7. package/dist/cjs/src/lib/cdp/EventApiClient.d.ts +24 -0
  8. package/dist/cjs/src/lib/cdp/EventApiClient.js +34 -0
  9. package/dist/cjs/src/lib/consts.d.ts +4 -0
  10. package/dist/cjs/src/lib/consts.js +13 -0
  11. package/dist/cjs/src/lib/eventStorage/addToEventQueue.d.ts +9 -0
  12. package/dist/cjs/src/lib/eventStorage/addToEventQueue.js +23 -0
  13. package/dist/cjs/src/lib/eventStorage/clearEventQueue.d.ts +4 -0
  14. package/dist/cjs/src/lib/eventStorage/clearEventQueue.js +13 -0
  15. package/dist/cjs/src/lib/eventStorage/eventStorage.d.ts +39 -0
  16. package/dist/cjs/src/lib/eventStorage/eventStorage.js +72 -0
  17. package/dist/cjs/src/lib/eventStorage/processEventQueue.d.ts +5 -0
  18. package/dist/cjs/src/lib/eventStorage/processEventQueue.js +14 -0
  19. package/dist/cjs/src/lib/events/base-event.d.ts +36 -0
  20. package/dist/cjs/src/lib/events/base-event.js +39 -0
  21. package/dist/cjs/src/lib/events/common-interfaces.d.ts +39 -0
  22. package/dist/cjs/src/lib/events/common-interfaces.js +3 -0
  23. package/dist/cjs/src/lib/events/consts.d.ts +2 -0
  24. package/dist/cjs/src/lib/events/consts.js +7 -0
  25. package/dist/cjs/src/lib/events/custom-event/custom-event.d.ts +50 -0
  26. package/dist/cjs/src/lib/events/custom-event/custom-event.js +40 -0
  27. package/dist/cjs/src/lib/events/custom-event/event.d.ts +12 -0
  28. package/dist/cjs/src/lib/events/custom-event/event.js +26 -0
  29. package/dist/cjs/src/lib/events/custom-event/eventServer.d.ts +13 -0
  30. package/dist/cjs/src/lib/events/custom-event/eventServer.js +28 -0
  31. package/dist/cjs/src/lib/events/custom-event/form.d.ts +9 -0
  32. package/dist/cjs/src/lib/events/custom-event/form.js +30 -0
  33. package/dist/cjs/src/lib/events/identity/identity-event.d.ts +99 -0
  34. package/dist/cjs/src/lib/events/identity/identity-event.js +88 -0
  35. package/dist/cjs/src/lib/events/identity/identity.d.ts +11 -0
  36. package/dist/cjs/src/lib/events/identity/identity.js +24 -0
  37. package/dist/cjs/src/lib/events/identity/identityServer.d.ts +13 -0
  38. package/dist/cjs/src/lib/events/identity/identityServer.js +27 -0
  39. package/dist/cjs/src/lib/events/index.d.ts +9 -0
  40. package/dist/cjs/src/lib/events/index.js +12 -0
  41. package/dist/cjs/src/lib/events/page-view/page-view-event.d.ts +75 -0
  42. package/dist/cjs/src/lib/events/page-view/page-view-event.js +94 -0
  43. package/dist/cjs/src/lib/events/page-view/page-view-server.d.ts +12 -0
  44. package/dist/cjs/src/lib/events/page-view/page-view-server.js +30 -0
  45. package/dist/cjs/src/lib/events/page-view/page-view.d.ts +11 -0
  46. package/dist/cjs/src/lib/events/page-view/page-view.js +24 -0
  47. package/dist/cjs/src/lib/getGuestId/getGuestId.d.ts +6 -0
  48. package/dist/cjs/src/lib/getGuestId/getGuestId.js +16 -0
  49. package/dist/cjs/src/lib/initializer/browser/initializer.d.ts +25 -0
  50. package/dist/cjs/src/lib/initializer/browser/initializer.js +60 -0
  51. package/dist/cjs/src/lib/initializer/server/initializer.d.ts +23 -0
  52. package/dist/cjs/src/lib/initializer/server/initializer.js +41 -0
  53. package/dist/cjs/src/server.d.ts +5 -0
  54. package/dist/cjs/src/server.js +12 -0
  55. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  56. package/dist/esm/package.json +31 -0
  57. package/dist/esm/src/browser.d.ts +10 -0
  58. package/dist/esm/src/browser.js +10 -0
  59. package/dist/esm/src/index.d.ts +4 -0
  60. package/dist/esm/src/index.js +5 -0
  61. package/dist/esm/src/lib/cdp/EventApiClient.d.ts +24 -0
  62. package/dist/esm/src/lib/cdp/EventApiClient.js +30 -0
  63. package/dist/esm/src/lib/consts.d.ts +4 -0
  64. package/dist/esm/src/lib/consts.js +7 -0
  65. package/dist/esm/src/lib/eventStorage/addToEventQueue.d.ts +9 -0
  66. package/dist/esm/src/lib/eventStorage/addToEventQueue.js +19 -0
  67. package/dist/esm/src/lib/eventStorage/clearEventQueue.d.ts +4 -0
  68. package/dist/esm/src/lib/eventStorage/clearEventQueue.js +9 -0
  69. package/dist/esm/src/lib/eventStorage/eventStorage.d.ts +39 -0
  70. package/dist/esm/src/lib/eventStorage/eventStorage.js +68 -0
  71. package/dist/esm/src/lib/eventStorage/processEventQueue.d.ts +5 -0
  72. package/dist/esm/src/lib/eventStorage/processEventQueue.js +10 -0
  73. package/dist/esm/src/lib/events/base-event.d.ts +36 -0
  74. package/dist/esm/src/lib/events/base-event.js +35 -0
  75. package/dist/esm/src/lib/events/common-interfaces.d.ts +39 -0
  76. package/dist/esm/src/lib/events/common-interfaces.js +2 -0
  77. package/dist/esm/src/lib/events/consts.d.ts +2 -0
  78. package/dist/esm/src/lib/events/consts.js +4 -0
  79. package/dist/esm/src/lib/events/custom-event/custom-event.d.ts +50 -0
  80. package/dist/esm/src/lib/events/custom-event/custom-event.js +36 -0
  81. package/dist/esm/src/lib/events/custom-event/event.d.ts +12 -0
  82. package/dist/esm/src/lib/events/custom-event/event.js +22 -0
  83. package/dist/esm/src/lib/events/custom-event/eventServer.d.ts +13 -0
  84. package/dist/esm/src/lib/events/custom-event/eventServer.js +24 -0
  85. package/dist/esm/src/lib/events/custom-event/form.d.ts +9 -0
  86. package/dist/esm/src/lib/events/custom-event/form.js +26 -0
  87. package/dist/esm/src/lib/events/identity/identity-event.d.ts +99 -0
  88. package/dist/esm/src/lib/events/identity/identity-event.js +84 -0
  89. package/dist/esm/src/lib/events/identity/identity.d.ts +11 -0
  90. package/dist/esm/src/lib/events/identity/identity.js +20 -0
  91. package/dist/esm/src/lib/events/identity/identityServer.d.ts +13 -0
  92. package/dist/esm/src/lib/events/identity/identityServer.js +23 -0
  93. package/dist/esm/src/lib/events/index.d.ts +9 -0
  94. package/dist/esm/src/lib/events/index.js +5 -0
  95. package/dist/esm/src/lib/events/page-view/page-view-event.d.ts +75 -0
  96. package/dist/esm/src/lib/events/page-view/page-view-event.js +90 -0
  97. package/dist/esm/src/lib/events/page-view/page-view-server.d.ts +12 -0
  98. package/dist/esm/src/lib/events/page-view/page-view-server.js +26 -0
  99. package/dist/esm/src/lib/events/page-view/page-view.d.ts +11 -0
  100. package/dist/esm/src/lib/events/page-view/page-view.js +20 -0
  101. package/dist/esm/src/lib/getGuestId/getGuestId.d.ts +6 -0
  102. package/dist/esm/src/lib/getGuestId/getGuestId.js +12 -0
  103. package/dist/esm/src/lib/initializer/browser/initializer.d.ts +25 -0
  104. package/dist/esm/src/lib/initializer/browser/initializer.js +54 -0
  105. package/dist/esm/src/lib/initializer/server/initializer.d.ts +23 -0
  106. package/dist/esm/src/lib/initializer/server/initializer.js +35 -0
  107. package/dist/esm/src/server.d.ts +5 -0
  108. package/dist/esm/src/server.js +5 -0
  109. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  110. package/package.json +31 -0
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # events
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build events` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test events` to execute the unit tests via [Jest](https://jestjs.io).
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@sitecore-cloudsdk/events",
3
+ "version": "0.1.0-rc.0",
4
+ "license": "Apache-2.0",
5
+ "main": "dist/cjs/src/index.js",
6
+ "module": "dist/esm/src/index.js",
7
+ "types": "dist/esm/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": {
11
+ "require": "./dist/cjs/src/index.d.ts",
12
+ "default": "./dist/esm/src/index.d.ts"
13
+ },
14
+ "import": "./dist/esm/src/index.js",
15
+ "require": "./dist/cjs/src/index.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@sitecore-cloudsdk/core": "0.x",
20
+ "@sitecore-cloudsdk/utils": "0.x"
21
+ },
22
+ "scripts": {
23
+ "build": "npm run build:cjs && npm run build:es",
24
+ "build:cjs": "tsc -b tsconfig.cjs.json",
25
+ "build:es": "tsc -b ."
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ]
31
+ }
@@ -0,0 +1,10 @@
1
+ export { getBrowserId } from 'packages/core/src/lib/init/get-browser-id';
2
+ export { init } from './lib/initializer/browser/initializer';
3
+ export type { IPageViewEventInput, ICustomEventInput, IIdentityEventAttributesInput } from './lib/events';
4
+ export { addToEventQueue } from './lib/eventStorage/addToEventQueue';
5
+ export { processEventQueue } from './lib/eventStorage/processEventQueue';
6
+ export { clearEventQueue } from './lib/eventStorage/clearEventQueue';
7
+ export { form } from './lib/events/custom-event/form';
8
+ export { event } from './lib/events/custom-event/event';
9
+ export { identity } from './lib/events/identity/identity';
10
+ export { pageView } from './lib/events/page-view/page-view';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pageView = exports.identity = exports.event = exports.form = exports.clearEventQueue = exports.processEventQueue = exports.addToEventQueue = exports.init = exports.getBrowserId = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ var get_browser_id_1 = require("packages/core/src/lib/init/get-browser-id");
6
+ Object.defineProperty(exports, "getBrowserId", { enumerable: true, get: function () { return get_browser_id_1.getBrowserId; } });
7
+ var initializer_1 = require("./lib/initializer/browser/initializer");
8
+ Object.defineProperty(exports, "init", { enumerable: true, get: function () { return initializer_1.init; } });
9
+ var addToEventQueue_1 = require("./lib/eventStorage/addToEventQueue");
10
+ Object.defineProperty(exports, "addToEventQueue", { enumerable: true, get: function () { return addToEventQueue_1.addToEventQueue; } });
11
+ var processEventQueue_1 = require("./lib/eventStorage/processEventQueue");
12
+ Object.defineProperty(exports, "processEventQueue", { enumerable: true, get: function () { return processEventQueue_1.processEventQueue; } });
13
+ var clearEventQueue_1 = require("./lib/eventStorage/clearEventQueue");
14
+ Object.defineProperty(exports, "clearEventQueue", { enumerable: true, get: function () { return clearEventQueue_1.clearEventQueue; } });
15
+ var form_1 = require("./lib/events/custom-event/form");
16
+ Object.defineProperty(exports, "form", { enumerable: true, get: function () { return form_1.form; } });
17
+ var event_1 = require("./lib/events/custom-event/event");
18
+ Object.defineProperty(exports, "event", { enumerable: true, get: function () { return event_1.event; } });
19
+ var identity_1 = require("./lib/events/identity/identity");
20
+ Object.defineProperty(exports, "identity", { enumerable: true, get: function () { return identity_1.identity; } });
21
+ var page_view_1 = require("./lib/events/page-view/page-view");
22
+ Object.defineProperty(exports, "pageView", { enumerable: true, get: function () { return page_view_1.pageView; } });
@@ -0,0 +1,4 @@
1
+ export * from './browser';
2
+ export * from './server';
3
+ export { getGuestId } from './lib/getGuestId/getGuestId';
4
+ export { LIBRARY_VERSION } from './lib/consts';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.LIBRARY_VERSION = exports.getGuestId = void 0;
18
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
19
+ __exportStar(require("./browser"), exports);
20
+ __exportStar(require("./server"), exports);
21
+ var getGuestId_1 = require("./lib/getGuestId/getGuestId");
22
+ Object.defineProperty(exports, "getGuestId", { enumerable: true, get: function () { return getGuestId_1.getGuestId; } });
23
+ var consts_1 = require("./lib/consts");
24
+ Object.defineProperty(exports, "LIBRARY_VERSION", { enumerable: true, get: function () { return consts_1.LIBRARY_VERSION; } });
@@ -0,0 +1,24 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ import type { IBasePayload, IPageViewEventPayload, IIdentityEventPayload, ICustomEventPayload } from '../events';
3
+ export declare class EventApiClient implements IEventApiClient {
4
+ private targetURL;
5
+ private readonly eventUrl;
6
+ constructor(targetURL: string, sitecoreEdgeContextId: string, siteName: string);
7
+ /**
8
+ * A function that sends the payload to Sitecore CDP
9
+ * @param body - The Request body for the Sitecore CDP
10
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
11
+ */
12
+ send(body: TCdpFetchBody & IBasePayload): Promise<ICdpResponse | null>;
13
+ }
14
+ /**
15
+ * The interface of EventApiClient class
16
+ */
17
+ export interface IEventApiClient {
18
+ send(body: TCdpFetchBody & IBasePayload): Promise<ICdpResponse | null>;
19
+ }
20
+ /**
21
+ * The type describing all possible event payloads
22
+ */
23
+ type TCdpFetchBody = IPageViewEventPayload | IIdentityEventPayload | ICustomEventPayload;
24
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventApiClient = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const consts_1 = require("../consts");
7
+ class EventApiClient {
8
+ constructor(targetURL, sitecoreEdgeContextId, siteName) {
9
+ this.targetURL = targetURL;
10
+ this.eventUrl = `${this.targetURL}/events/${core_1.API_VERSION}/events?sitecoreContextId=${sitecoreEdgeContextId}&siteId=${siteName}`;
11
+ }
12
+ /**
13
+ * A function that sends the payload to Sitecore CDP
14
+ * @param body - The Request body for the Sitecore CDP
15
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
16
+ */
17
+ async send(body) {
18
+ const fetchOptions = {
19
+ body: JSON.stringify(body),
20
+ headers: {
21
+ // eslint-disable-next-line @typescript-eslint/naming-convention
22
+ 'Content-Type': 'application/json',
23
+ // eslint-disable-next-line @typescript-eslint/naming-convention
24
+ 'X-Library-Version': consts_1.LIBRARY_VERSION,
25
+ },
26
+ method: 'POST',
27
+ };
28
+ return await fetch(this.eventUrl, fetchOptions)
29
+ .then((response) => response.json())
30
+ .then((data) => data)
31
+ .catch(() => null);
32
+ }
33
+ }
34
+ exports.EventApiClient = EventApiClient;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Returns the version of the library.
3
+ */
4
+ export declare const LIBRARY_VERSION: string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LIBRARY_VERSION = void 0;
7
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
8
+ /* eslint-disable @typescript-eslint/naming-convention */
9
+ const package_json_1 = __importDefault(require("../../package.json"));
10
+ /**
11
+ * Returns the version of the library.
12
+ */
13
+ exports.LIBRARY_VERSION = package_json_1.default.version;
@@ -0,0 +1,9 @@
1
+ import { ICustomEventInput, ExtensionData } from '../events';
2
+ /**
3
+ * A function that adds event to the queue
4
+ * @param type - The required type of the event
5
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
6
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
7
+ * This object will be flattened and sent in the ext object of the payload
8
+ */
9
+ export declare function addToEventQueue(type: string, eventData: ICustomEventInput, extensionData?: ExtensionData): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addToEventQueue = void 0;
4
+ const initializer_1 = require("../initializer/browser/initializer");
5
+ /**
6
+ * A function that adds event to the queue
7
+ * @param type - The required type of the event
8
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
9
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
10
+ * This object will be flattened and sent in the ext object of the payload
11
+ */
12
+ function addToEventQueue(type, eventData, extensionData) {
13
+ const { id, settings, eventQueue } = (0, initializer_1.getDependencies)();
14
+ const queueEventPayload = {
15
+ eventData,
16
+ extensionData,
17
+ id,
18
+ settings,
19
+ type,
20
+ };
21
+ eventQueue.enqueueEvent(queueEventPayload);
22
+ }
23
+ exports.addToEventQueue = addToEventQueue;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Deletes the queue from session.
3
+ */
4
+ export declare function clearEventQueue(): void;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearEventQueue = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const initializer_1 = require("../initializer/browser/initializer");
6
+ /**
7
+ * Deletes the queue from session.
8
+ */
9
+ function clearEventQueue() {
10
+ const { eventQueue } = (0, initializer_1.getDependencies)();
11
+ eventQueue.clearQueue();
12
+ }
13
+ exports.clearEventQueue = clearEventQueue;
@@ -0,0 +1,39 @@
1
+ import { EventApiClient } from '../cdp/EventApiClient';
2
+ import { ICustomEventArguments } from '../events';
3
+ export declare class EventQueue {
4
+ private storage;
5
+ private eventApiClient;
6
+ /**
7
+ * Initialize the Event Storage
8
+ * @param storage - Interface that describes the storage functionality
9
+ * @param eventApiClient - The API client which sends events to CDP
10
+ * @param infer - The instance of the infer class
11
+ */
12
+ private key;
13
+ constructor(storage: IStorage, eventApiClient: EventApiClient);
14
+ /** Returns the stored array of data with type QueueEventPayload, or empty array if the given key does not exist. */
15
+ private getEventQueue;
16
+ /**
17
+ * Adds the required event data to the queue and stores it in the storage.
18
+ * @param queueEventPayload - The required event data for the creation of a CustomEvent.
19
+ * Performs validation by creating a new CustomEvent.
20
+ */
21
+ enqueueEvent(queueEventPayload: QueueEventPayload): void;
22
+ /**
23
+ * Iterates the queue, and sends sequently the custom events to Sitecore CDP.
24
+ */
25
+ sendAllEvents(): Promise<void>;
26
+ /**
27
+ * Clears the queue from storage.
28
+ */
29
+ clearQueue(): void;
30
+ }
31
+ /**
32
+ * This Storage interface represents the required storage functionality.
33
+ */
34
+ export interface IStorage {
35
+ getItem(key: string): string | null;
36
+ setItem(key: string, value: string): void;
37
+ removeItem(key: string): void;
38
+ }
39
+ export type QueueEventPayload = Pick<ICustomEventArguments, 'eventData' | 'extensionData' | 'type' | 'settings' | 'id'>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventQueue = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const events_1 = require("../events");
7
+ class EventQueue {
8
+ constructor(storage, eventApiClient) {
9
+ this.storage = storage;
10
+ this.eventApiClient = eventApiClient;
11
+ /**
12
+ * Initialize the Event Storage
13
+ * @param storage - Interface that describes the storage functionality
14
+ * @param eventApiClient - The API client which sends events to CDP
15
+ * @param infer - The instance of the infer class
16
+ */
17
+ this.key = 'EventQueue';
18
+ }
19
+ /** Returns the stored array of data with type QueueEventPayload, or empty array if the given key does not exist. */
20
+ getEventQueue() {
21
+ const storedQueue = this.storage.getItem(this.key);
22
+ if (!storedQueue)
23
+ return [];
24
+ try {
25
+ const parsedQueueEvent = JSON.parse(storedQueue);
26
+ return Array.isArray(parsedQueueEvent) ? parsedQueueEvent : [];
27
+ }
28
+ catch {
29
+ return [];
30
+ }
31
+ }
32
+ /**
33
+ * Adds the required event data to the queue and stores it in the storage.
34
+ * @param queueEventPayload - The required event data for the creation of a CustomEvent.
35
+ * Performs validation by creating a new CustomEvent.
36
+ */
37
+ enqueueEvent(queueEventPayload) {
38
+ queueEventPayload.eventData.page = queueEventPayload.eventData.page ?? (0, core_1.pageName)();
39
+ queueEventPayload.eventData.language = queueEventPayload.eventData.language ?? (0, core_1.language)();
40
+ new events_1.CustomEvent({
41
+ eventApiClient: this.eventApiClient,
42
+ ...queueEventPayload,
43
+ });
44
+ const eventQueue = this.getEventQueue();
45
+ eventQueue.push(queueEventPayload);
46
+ this.storage.setItem(this.key, JSON.stringify(eventQueue));
47
+ }
48
+ /**
49
+ * Iterates the queue, and sends sequently the custom events to Sitecore CDP.
50
+ */
51
+ async sendAllEvents() {
52
+ const eventQueue = this.getEventQueue();
53
+ for (const queueEventPayload of eventQueue) {
54
+ await new events_1.CustomEvent({
55
+ eventApiClient: this.eventApiClient,
56
+ eventData: queueEventPayload.eventData,
57
+ extensionData: queueEventPayload.extensionData,
58
+ id: queueEventPayload.id,
59
+ settings: queueEventPayload.settings,
60
+ type: queueEventPayload.type,
61
+ }).send();
62
+ }
63
+ this.clearQueue();
64
+ }
65
+ /**
66
+ * Clears the queue from storage.
67
+ */
68
+ clearQueue() {
69
+ this.storage.removeItem(this.key);
70
+ }
71
+ }
72
+ exports.EventQueue = EventQueue;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A function that sends all queue events to SitecoreCloud API.
3
+ * Clears the queue upon completion.
4
+ */
5
+ export declare function processEventQueue(): void;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processEventQueue = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const initializer_1 = require("../initializer/browser/initializer");
6
+ /**
7
+ * A function that sends all queue events to SitecoreCloud API.
8
+ * Clears the queue upon completion.
9
+ */
10
+ function processEventQueue() {
11
+ const { eventQueue } = (0, initializer_1.getDependencies)();
12
+ eventQueue.sendAllEvents();
13
+ }
14
+ exports.processEventQueue = processEventQueue;
@@ -0,0 +1,36 @@
1
+ import type { ISettings } from '@sitecore-cloudsdk/core';
2
+ import { IEventAttributesInput } from './common-interfaces';
3
+ export declare class BaseEvent {
4
+ private baseEventData;
5
+ protected settings: ISettings;
6
+ private readonly browserId;
7
+ private readonly language;
8
+ page: string;
9
+ /**
10
+ * The base event class that has all the shared functions between Events
11
+ * @param baseEventData - The event data to send
12
+ * @param settings - The global settings
13
+ * @param id - The browser id
14
+ * @param infer - The source of methods to estimate language and page parameters
15
+ */
16
+ constructor(baseEventData: IBaseEventData, settings: ISettings, id: string);
17
+ /**
18
+ * A function that returns the properties for sending events to Sitecore CDP
19
+ * @returns an object that is required
20
+ */
21
+ protected mapBaseEventPayload(): IBasePayload;
22
+ }
23
+ /**
24
+ * An interface describing the basic payload to be sent to the API
25
+ */
26
+ export interface IBasePayload {
27
+ browser_id: string;
28
+ channel?: string;
29
+ client_key: string;
30
+ currency?: string;
31
+ language?: string;
32
+ page?: string;
33
+ pos: string;
34
+ }
35
+ type IBaseEventData = Partial<IEventAttributesInput>;
36
+ export {};
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseEvent = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ class BaseEvent {
7
+ /**
8
+ * The base event class that has all the shared functions between Events
9
+ * @param baseEventData - The event data to send
10
+ * @param settings - The global settings
11
+ * @param id - The browser id
12
+ * @param infer - The source of methods to estimate language and page parameters
13
+ */
14
+ constructor(baseEventData, settings, id) {
15
+ this.baseEventData = baseEventData;
16
+ this.settings = settings;
17
+ this.browserId = id;
18
+ this.language = this.baseEventData.language ?? (0, core_1.language)();
19
+ this.page = this.baseEventData.page ?? (0, core_1.pageName)();
20
+ }
21
+ /**
22
+ * A function that returns the properties for sending events to Sitecore CDP
23
+ * @returns an object that is required
24
+ */
25
+ mapBaseEventPayload() {
26
+ return {
27
+ // eslint-disable-next-line @typescript-eslint/naming-convention
28
+ browser_id: this.browserId,
29
+ channel: this.baseEventData.channel,
30
+ /* eslint-disable @typescript-eslint/naming-convention */
31
+ client_key: '',
32
+ currency: this.baseEventData.currency,
33
+ language: this.language,
34
+ page: this.page,
35
+ pos: '',
36
+ };
37
+ }
38
+ }
39
+ exports.BaseEvent = BaseEvent;
@@ -0,0 +1,39 @@
1
+ import { BasicTypes, INestedObject } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Event data received as input to be sent to Sitecore CDP
4
+ */
5
+ interface IMandatoryInput {
6
+ channel: string;
7
+ currency: string;
8
+ }
9
+ type IMandatoryEventInput = IMandatoryInput;
10
+ /**
11
+ * Event data that is sent to Sitecore CDP
12
+ */
13
+ interface IInferrableInput {
14
+ /**
15
+ * To be restored back to IMandatoryEventInput Interface
16
+ * as mandatory input parameter in version 1.0.0
17
+ */
18
+ pointOfSale?: string;
19
+ language?: string;
20
+ page?: string;
21
+ }
22
+ type IInferrableEventInput = IInferrableInput;
23
+ /**
24
+ * Interface to hold the base event attributes
25
+ */
26
+ export interface IEventAttributesInput extends IMandatoryEventInput, IInferrableEventInput {
27
+ }
28
+ /**
29
+ * Interface to hold the "ext" data
30
+ */
31
+ export interface IExtensionData {
32
+ pageVariantId?: string;
33
+ [key: string]: BasicTypes;
34
+ }
35
+ /**
36
+ * Type of the extension data that the developer can pass to events
37
+ */
38
+ export type ExtensionData = INestedObject;
39
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export declare const MAX_EXT_ATTRIBUTES = 50;
2
+ export declare const UTM_PREFIX = "utm_";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ /* eslint-disable @typescript-eslint/naming-convention */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.UTM_PREFIX = exports.MAX_EXT_ATTRIBUTES = void 0;
6
+ exports.MAX_EXT_ATTRIBUTES = 50;
7
+ exports.UTM_PREFIX = 'utm_';
@@ -0,0 +1,50 @@
1
+ import { BaseEvent } from '../base-event';
2
+ import { IEventAttributesInput } from '../common-interfaces';
3
+ import { IEventApiClient } from '../../cdp/EventApiClient';
4
+ import { ICdpResponse, ISettings } from '@sitecore-cloudsdk/core';
5
+ import { BasicTypes, INestedObject } from '@sitecore-cloudsdk/utils';
6
+ export declare class CustomEvent extends BaseEvent {
7
+ customEventPayload: ICustomEventPayload;
8
+ private eventApiClient;
9
+ private extensionData;
10
+ /**
11
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
12
+ * @param args - Unified object containing the required properties
13
+ */
14
+ constructor(args: ICustomEventArguments);
15
+ /**
16
+ * Sends the event to Sitecore CDP
17
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
18
+ */
19
+ send(): Promise<ICdpResponse | null>;
20
+ }
21
+ /**
22
+ * Interface of the unified arguments object for custom event
23
+ */
24
+ export interface ICustomEventArguments {
25
+ eventApiClient: IEventApiClient;
26
+ eventData: ICustomEventData;
27
+ id: string;
28
+ extensionData?: INestedObject;
29
+ settings: ISettings;
30
+ type: string;
31
+ }
32
+ /**
33
+ * Interface with the required/optional attributes in order to send a custom event to SitecoreCloud API
34
+ */
35
+ export interface ICustomEventPayload extends INestedObject {
36
+ ext?: {
37
+ [key: string]: BasicTypes;
38
+ };
39
+ }
40
+ /**
41
+ * Interface with the required/optional attributes in order to send a custom event to SitecoreCloud API
42
+ */
43
+ export interface ICustomEventInput extends IEventAttributesInput, INestedObject {
44
+ }
45
+ /**
46
+ * Internal interface with the required/optional attributes in order to send a custom event to SitecoreCloud API
47
+ */
48
+ interface ICustomEventData extends Partial<IEventAttributesInput>, INestedObject {
49
+ }
50
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomEvent = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const base_event_1 = require("../base-event");
6
+ const utils_1 = require("@sitecore-cloudsdk/utils");
7
+ const consts_1 = require("../consts");
8
+ class CustomEvent extends base_event_1.BaseEvent {
9
+ /**
10
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
11
+ * @param args - Unified object containing the required properties
12
+ */
13
+ constructor(args) {
14
+ const { channel, currency, language, page, ...rest } = args.eventData;
15
+ super({ channel, currency, language, page }, args.settings, args.id);
16
+ this.extensionData = {};
17
+ this.eventApiClient = args.eventApiClient;
18
+ this.customEventPayload = {
19
+ type: args.type,
20
+ ...rest,
21
+ };
22
+ if (args.extensionData)
23
+ this.extensionData = (0, utils_1.flattenObject)({ object: args.extensionData });
24
+ const numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
25
+ if (numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
26
+ throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
27
+ if (numberOfExtensionDataProperties > 0)
28
+ this.customEventPayload.ext = this.extensionData;
29
+ }
30
+ /**
31
+ * Sends the event to Sitecore CDP
32
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
33
+ */
34
+ async send() {
35
+ const baseAttr = this.mapBaseEventPayload();
36
+ const fetchBody = Object.assign({}, this.customEventPayload, baseAttr);
37
+ return await this.eventApiClient.send(fetchBody);
38
+ }
39
+ }
40
+ exports.CustomEvent = CustomEvent;
@@ -0,0 +1,12 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ import { ExtensionData } from '../common-interfaces';
3
+ import { ICustomEventInput } from './custom-event';
4
+ /**
5
+ * A function that sends an event to SitecoreCloud API with the specified type
6
+ * @param type - The required type of the event
7
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
8
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
9
+ * This object will be flattened and sent in the ext object of the payload
10
+ * @returns The response object that Sitecore CDP returns
11
+ */
12
+ export declare function event(type: string, eventData: ICustomEventInput, extensionData?: ExtensionData): Promise<ICdpResponse | null>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.event = void 0;
5
+ const custom_event_1 = require("./custom-event");
6
+ const initializer_1 = require("../../initializer/browser/initializer");
7
+ /**
8
+ * A function that sends an event to SitecoreCloud API with the specified type
9
+ * @param type - The required type of the event
10
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
11
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
12
+ * This object will be flattened and sent in the ext object of the payload
13
+ * @returns The response object that Sitecore CDP returns
14
+ */
15
+ function event(type, eventData, extensionData) {
16
+ const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
17
+ return new custom_event_1.CustomEvent({
18
+ eventApiClient,
19
+ eventData,
20
+ extensionData,
21
+ id,
22
+ settings,
23
+ type,
24
+ }).send();
25
+ }
26
+ exports.event = event;