@sitecore-content-sdk/events 2.0.0-canary.12

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 (103) hide show
  1. package/LICENSE.MD +202 -0
  2. package/README.md +91 -0
  3. package/browser.d.ts +2 -0
  4. package/dist/cjs/package.json +80 -0
  5. package/dist/cjs/src/browser.js +9 -0
  6. package/dist/cjs/src/consts.js +21 -0
  7. package/dist/cjs/src/debug.js +12 -0
  8. package/dist/cjs/src/eventStorage/addToEventQueue.js +25 -0
  9. package/dist/cjs/src/eventStorage/clearEventQueue.js +15 -0
  10. package/dist/cjs/src/eventStorage/eventStorage.js +67 -0
  11. package/dist/cjs/src/eventStorage/processEventQueue.js +16 -0
  12. package/dist/cjs/src/events/base-event.js +35 -0
  13. package/dist/cjs/src/events/common-interfaces.js +2 -0
  14. package/dist/cjs/src/events/consts.js +5 -0
  15. package/dist/cjs/src/events/custom-event/custom-event.js +62 -0
  16. package/dist/cjs/src/events/custom-event/event.js +27 -0
  17. package/dist/cjs/src/events/custom-event/form.js +38 -0
  18. package/dist/cjs/src/events/identity/identity-event.js +93 -0
  19. package/dist/cjs/src/events/identity/identity.js +27 -0
  20. package/dist/cjs/src/events/index.js +11 -0
  21. package/dist/cjs/src/events/page-view/page-view-event.js +131 -0
  22. package/dist/cjs/src/events/page-view/page-view.js +30 -0
  23. package/dist/cjs/src/events/send-event/sendEvent.js +36 -0
  24. package/dist/cjs/src/index.js +13 -0
  25. package/dist/cjs/src/initialization/const.js +8 -0
  26. package/dist/cjs/src/initialization/plugin.js +56 -0
  27. package/dist/cjs/src/initialization/types.js +2 -0
  28. package/dist/cjs/src/internal.js +8 -0
  29. package/dist/esm/package.json +80 -0
  30. package/dist/esm/src/browser.js +3 -0
  31. package/dist/esm/src/consts.js +15 -0
  32. package/dist/esm/src/debug.js +9 -0
  33. package/dist/esm/src/eventStorage/addToEventQueue.js +22 -0
  34. package/dist/esm/src/eventStorage/clearEventQueue.js +12 -0
  35. package/dist/esm/src/eventStorage/eventStorage.js +64 -0
  36. package/dist/esm/src/eventStorage/processEventQueue.js +13 -0
  37. package/dist/esm/src/events/base-event.js +31 -0
  38. package/dist/esm/src/events/common-interfaces.js +1 -0
  39. package/dist/esm/src/events/consts.js +2 -0
  40. package/dist/esm/src/events/custom-event/custom-event.js +58 -0
  41. package/dist/esm/src/events/custom-event/event.js +24 -0
  42. package/dist/esm/src/events/custom-event/form.js +35 -0
  43. package/dist/esm/src/events/identity/identity-event.js +89 -0
  44. package/dist/esm/src/events/identity/identity.js +24 -0
  45. package/dist/esm/src/events/index.js +4 -0
  46. package/dist/esm/src/events/page-view/page-view-event.js +127 -0
  47. package/dist/esm/src/events/page-view/page-view.js +27 -0
  48. package/dist/esm/src/events/send-event/sendEvent.js +33 -0
  49. package/dist/esm/src/index.js +5 -0
  50. package/dist/esm/src/initialization/const.js +5 -0
  51. package/dist/esm/src/initialization/plugin.js +52 -0
  52. package/dist/esm/src/initialization/types.js +1 -0
  53. package/dist/esm/src/internal.js +2 -0
  54. package/internal.d.ts +2 -0
  55. package/package.json +80 -0
  56. package/types/src/browser.d.ts +4 -0
  57. package/types/src/browser.d.ts.map +1 -0
  58. package/types/src/consts.d.ts +15 -0
  59. package/types/src/consts.d.ts.map +1 -0
  60. package/types/src/debug.d.ts +9 -0
  61. package/types/src/debug.d.ts.map +1 -0
  62. package/types/src/eventStorage/addToEventQueue.d.ts +8 -0
  63. package/types/src/eventStorage/addToEventQueue.d.ts.map +1 -0
  64. package/types/src/eventStorage/clearEventQueue.d.ts +6 -0
  65. package/types/src/eventStorage/clearEventQueue.d.ts.map +1 -0
  66. package/types/src/eventStorage/eventStorage.d.ts +31 -0
  67. package/types/src/eventStorage/eventStorage.d.ts.map +1 -0
  68. package/types/src/eventStorage/processEventQueue.d.ts +7 -0
  69. package/types/src/eventStorage/processEventQueue.d.ts.map +1 -0
  70. package/types/src/events/base-event.d.ts +35 -0
  71. package/types/src/events/base-event.d.ts.map +1 -0
  72. package/types/src/events/common-interfaces.d.ts +53 -0
  73. package/types/src/events/common-interfaces.d.ts.map +1 -0
  74. package/types/src/events/consts.d.ts +3 -0
  75. package/types/src/events/consts.d.ts.map +1 -0
  76. package/types/src/events/custom-event/custom-event.d.ts +79 -0
  77. package/types/src/events/custom-event/custom-event.d.ts.map +1 -0
  78. package/types/src/events/custom-event/event.d.ts +10 -0
  79. package/types/src/events/custom-event/event.d.ts.map +1 -0
  80. package/types/src/events/custom-event/form.d.ts +11 -0
  81. package/types/src/events/custom-event/form.d.ts.map +1 -0
  82. package/types/src/events/identity/identity-event.d.ts +184 -0
  83. package/types/src/events/identity/identity-event.d.ts.map +1 -0
  84. package/types/src/events/identity/identity.d.ts +10 -0
  85. package/types/src/events/identity/identity.d.ts.map +1 -0
  86. package/types/src/events/index.d.ts +10 -0
  87. package/types/src/events/index.d.ts.map +1 -0
  88. package/types/src/events/page-view/page-view-event.d.ts +125 -0
  89. package/types/src/events/page-view/page-view-event.d.ts.map +1 -0
  90. package/types/src/events/page-view/page-view.d.ts +11 -0
  91. package/types/src/events/page-view/page-view.d.ts.map +1 -0
  92. package/types/src/events/send-event/sendEvent.d.ts +22 -0
  93. package/types/src/events/send-event/sendEvent.d.ts.map +1 -0
  94. package/types/src/index.d.ts +10 -0
  95. package/types/src/index.d.ts.map +1 -0
  96. package/types/src/initialization/const.d.ts +6 -0
  97. package/types/src/initialization/const.d.ts.map +1 -0
  98. package/types/src/initialization/plugin.d.ts +35 -0
  99. package/types/src/initialization/plugin.d.ts.map +1 -0
  100. package/types/src/initialization/types.d.ts +22 -0
  101. package/types/src/initialization/types.d.ts.map +1 -0
  102. package/types/src/internal.d.ts +3 -0
  103. package/types/src/internal.d.ts.map +1 -0
@@ -0,0 +1,64 @@
1
+ import { language, pageName } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { CustomEvent } from '../events';
3
+ import { sendEvent } from '../events/send-event/sendEvent';
4
+ class EventQueue {
5
+ constructor() {
6
+ /**
7
+ * Initialize the Event Storage
8
+ */
9
+ this.key = 'EventQueue';
10
+ }
11
+ /**
12
+ * Adds the required event data to the queue and stores it in the storage.
13
+ * @param {QueueEventPayload} queueEventPayload - The required event data for the creation of a `CustomEvent`.
14
+ * Validates by creating a new `CustomEvent`.
15
+ */
16
+ enqueueEvent(queueEventPayload) {
17
+ var _a, _b;
18
+ const sessionStorage = this.getSessionStorage();
19
+ queueEventPayload.eventData.page = (_a = queueEventPayload.eventData.page) !== null && _a !== void 0 ? _a : pageName();
20
+ queueEventPayload.eventData.language = (_b = queueEventPayload.eventData.language) !== null && _b !== void 0 ? _b : language();
21
+ new CustomEvent(Object.assign({ sendEvent }, queueEventPayload));
22
+ const eventQueue = this.getEventQueue();
23
+ eventQueue.push(queueEventPayload);
24
+ sessionStorage.setItem(this.key, JSON.stringify(eventQueue));
25
+ }
26
+ /**
27
+ * Iterates the queue and sends sequentially the custom events to Sitecore Edge Proxy.
28
+ */
29
+ async sendAllEvents() {
30
+ const eventQueue = this.getEventQueue();
31
+ for (const queueEventPayload of eventQueue)
32
+ await new CustomEvent({
33
+ eventData: queueEventPayload.eventData,
34
+ id: queueEventPayload.id,
35
+ sendEvent,
36
+ config: queueEventPayload.config,
37
+ }).send();
38
+ this.clearQueue();
39
+ }
40
+ /**
41
+ * Clears the queue from storage.
42
+ */
43
+ clearQueue() {
44
+ const sessionStorage = this.getSessionStorage();
45
+ sessionStorage.removeItem(this.key);
46
+ }
47
+ /** Returns the stored array of data with type `QueueEventPayload`, or empty array if the given key does not exist. */
48
+ getEventQueue() {
49
+ var _a;
50
+ const sessionStorage = this.getSessionStorage();
51
+ const storedQueue = (_a = sessionStorage.getItem(this.key)) !== null && _a !== void 0 ? _a : '""';
52
+ try {
53
+ const parsedQueueEvent = JSON.parse(storedQueue);
54
+ return Array.isArray(parsedQueueEvent) ? parsedQueueEvent : [];
55
+ }
56
+ catch (_b) {
57
+ return [];
58
+ }
59
+ }
60
+ getSessionStorage() {
61
+ return sessionStorage;
62
+ }
63
+ }
64
+ export const eventQueue = new EventQueue();
@@ -0,0 +1,13 @@
1
+ import { getCoreContext } from '@sitecore-content-sdk/core';
2
+ import { eventQueue } from './eventStorage';
3
+ import { getEventsPlugin } from '../initialization/plugin';
4
+ /**
5
+ * A function that sends all queue events to the SitecoreCloud API.
6
+ * Clears the queue upon completion.
7
+ * @public
8
+ */
9
+ export async function processEventQueue() {
10
+ await getCoreContext().readyPromise;
11
+ getEventsPlugin();
12
+ eventQueue.sendAllEvents();
13
+ }
@@ -0,0 +1,31 @@
1
+ import { language, pageName } from '@sitecore-content-sdk/analytics-core/internal';
2
+ export class BaseEvent {
3
+ /**
4
+ * The base event class that has all the shared functions between Events
5
+ * @param {BaseEventData} baseEventData - The event data to send
6
+ * @param {string} id - The client id
7
+ */
8
+ constructor(baseEventData, id) {
9
+ var _a, _b;
10
+ this.baseEventData = baseEventData;
11
+ this.clientId = id;
12
+ this.language = (_a = this.baseEventData.language) !== null && _a !== void 0 ? _a : language();
13
+ this.page = (_b = this.baseEventData.page) !== null && _b !== void 0 ? _b : pageName();
14
+ }
15
+ /**
16
+ * A function that returns the properties for sending events to the Sitecore Edge Proxy.
17
+ * @returns an object that is required
18
+ */
19
+ mapBaseEventPayload() {
20
+ return {
21
+ browser_id: this.clientId,
22
+ channel: this.baseEventData.channel,
23
+ client_key: '',
24
+ currency: this.baseEventData.currency,
25
+ language: this.language,
26
+ page: this.page,
27
+ pos: '',
28
+ requested_at: new Date().toISOString(),
29
+ };
30
+ }
31
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export const MAX_EXT_ATTRIBUTES = 50;
2
+ export const UTM_PREFIX = 'utm_';
@@ -0,0 +1,58 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { flattenObject } from '@sitecore-content-sdk/analytics-core/utils';
13
+ import { BaseEvent } from '../base-event';
14
+ import { MAX_EXT_ATTRIBUTES } from '../consts';
15
+ import { constants } from '@sitecore-content-sdk/core';
16
+ const { ERROR_MESSAGES } = constants;
17
+ /**
18
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a custom event
19
+ * @internal
20
+ */
21
+ export class CustomEvent extends BaseEvent {
22
+ /**
23
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a custom event
24
+ * @param {CustomEventArguments} args - Unified object containing the required properties
25
+ */
26
+ constructor(args) {
27
+ const _a = args.eventData, { channel, currency, language, page, type, extensionData, searchData } = _a, rest = __rest(_a, ["channel", "currency", "language", "page", "type", "extensionData", "searchData"]);
28
+ super({ channel, currency, language, page }, args.id);
29
+ this.extensionData = {};
30
+ this.sendEvent = args.sendEvent;
31
+ this.config = args.config;
32
+ this.customEventPayload = Object.assign({ type }, rest);
33
+ if (extensionData)
34
+ this.extensionData = flattenObject({ object: extensionData });
35
+ const numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
36
+ if (numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
37
+ throw new Error(ERROR_MESSAGES.IV_006(MAX_EXT_ATTRIBUTES));
38
+ if (numberOfExtensionDataProperties > 0)
39
+ this.customEventPayload.ext = this.extensionData;
40
+ if (searchData)
41
+ this.customEventPayload.sc_search = {
42
+ data: searchData,
43
+ metadata: {
44
+ // eslint-disable-next-line @typescript-eslint/naming-convention
45
+ ut_api_version: '1.0',
46
+ },
47
+ };
48
+ }
49
+ /**
50
+ * Sends the event to Sitecore Edge Proxy
51
+ * @returns - A promise that resolves with either the Sitecore Edge Proxy response object or null
52
+ */
53
+ async send() {
54
+ const baseAttr = this.mapBaseEventPayload();
55
+ const fetchBody = Object.assign({}, this.customEventPayload, baseAttr);
56
+ return await this.sendEvent(fetchBody, this.config);
57
+ }
58
+ }
@@ -0,0 +1,24 @@
1
+ import { getAnalyticsPlugin } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { sendEvent } from '../send-event/sendEvent';
3
+ import { CustomEvent } from './custom-event';
4
+ import { getCoreContext } from '@sitecore-content-sdk/core';
5
+ import { getEventsPlugin } from '../../initialization/plugin';
6
+ /**
7
+ * A function that sends an event to the SitecoreCloud API with the specified type
8
+ * @param {EventData} eventData - The required/optional attributes to be sent to the SitecoreCloud API
9
+ * @returns The response object that Sitecore Edge Proxy returns
10
+ * @public
11
+ */
12
+ export async function event(eventData) {
13
+ const coreContext = getCoreContext();
14
+ await coreContext.readyPromise;
15
+ getEventsPlugin();
16
+ const { options, adapter } = getAnalyticsPlugin();
17
+ const id = adapter.getClientId() || '';
18
+ return new CustomEvent({
19
+ eventData,
20
+ id,
21
+ sendEvent,
22
+ config: Object.assign(Object.assign({}, coreContext.config), options),
23
+ }).send();
24
+ }
@@ -0,0 +1,35 @@
1
+ import { getAnalyticsPlugin } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { sendEvent } from '../send-event/sendEvent';
3
+ import { CustomEvent } from './custom-event';
4
+ import { getCoreContext } from '@sitecore-content-sdk/core';
5
+ import { getEventsPlugin } from '../../initialization/plugin';
6
+ /**
7
+ * A function that sends a form event to the SitecoreCloud API
8
+ * @param {string} formId - The required form ID string
9
+ * @param {'VIEWED' | 'SUBMITTED'} interactionType - The required interaction type string. Possible values: `VIEWED`, `SUBMITTED`
10
+ * @param {string} componentInstanceId - The required component instance ID string
11
+ * @returns The response object that Sitecore Edge Proxy returns or null
12
+ * @public
13
+ */
14
+ export async function form(formId, interactionType, componentInstanceId) {
15
+ const coreContext = getCoreContext();
16
+ await coreContext.readyPromise;
17
+ getEventsPlugin();
18
+ const { options, adapter } = getAnalyticsPlugin();
19
+ const id = adapter.getClientId() || '';
20
+ const formEvent = new CustomEvent({
21
+ eventData: {
22
+ extensionData: {
23
+ componentInstanceId,
24
+ formId,
25
+ interactionType: interactionType.toUpperCase(),
26
+ },
27
+ type: 'FORM',
28
+ },
29
+ id,
30
+ sendEvent,
31
+ config: Object.assign(Object.assign({}, coreContext.config), options),
32
+ });
33
+ formEvent.page = undefined;
34
+ return formEvent.send();
35
+ }
@@ -0,0 +1,89 @@
1
+ import { flattenObject, isShortISODateString, isValidEmail, } from '@sitecore-content-sdk/analytics-core/utils';
2
+ import { BaseEvent } from '../base-event';
3
+ import { MAX_EXT_ATTRIBUTES } from '../consts';
4
+ import { constants } from '@sitecore-content-sdk/core';
5
+ const { ERROR_MESSAGES } = constants;
6
+ /**
7
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send an IDENTITY event
8
+ */
9
+ export class IdentityEvent extends BaseEvent {
10
+ /**
11
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send an IDENTITY event
12
+ * @param {IdentityEventArguments} args - Unified object containing the required properties
13
+ */
14
+ constructor(args) {
15
+ const { channel, currency, language, page, extensionData } = args.identityData;
16
+ super({ channel, currency, language, page }, args.id);
17
+ this.extensionData = {};
18
+ this.numberOfExtensionDataProperties = 0;
19
+ this.validateAttributes(args.identityData);
20
+ this.identityData = args.identityData;
21
+ this.sendEvent = args.sendEvent;
22
+ this.config = args.config;
23
+ if (extensionData)
24
+ this.extensionData = flattenObject({ object: extensionData });
25
+ this.numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
26
+ if (this.numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
27
+ throw new Error(ERROR_MESSAGES.IV_006(MAX_EXT_ATTRIBUTES));
28
+ }
29
+ /**
30
+ * Sends the event to Sitecore Edge Proxy
31
+ * @returns - A promise that resolves with either the Sitecore Edge Proxy response object or null
32
+ */
33
+ async send() {
34
+ const baseAttr = this.mapBaseEventPayload();
35
+ const eventAttrs = this.mapAttributes();
36
+ const fetchBody = Object.assign({}, eventAttrs, baseAttr);
37
+ return await this.sendEvent(fetchBody, this.config);
38
+ }
39
+ /**
40
+ * Function that validates the identifiers object, email, and date attributes for CDN users
41
+ * @param {IdentityData} identityData - The data to be validated
42
+ */
43
+ validateAttributes(identityData) {
44
+ if (identityData.identifiers.length === 0)
45
+ throw new Error(ERROR_MESSAGES.MV_003);
46
+ if (identityData.dob !== undefined && !isShortISODateString(identityData.dob))
47
+ throw new Error(ERROR_MESSAGES.IV_003);
48
+ identityData.identifiers.forEach((identifier) => {
49
+ if (identifier.expiryDate && !isShortISODateString(identifier.expiryDate))
50
+ throw new Error(ERROR_MESSAGES.IV_005);
51
+ });
52
+ if (identityData.email && !isValidEmail(identityData.email))
53
+ throw new Error(ERROR_MESSAGES.IV_004);
54
+ }
55
+ /**
56
+ * A function that maps the identity event input data with the payload sent to the API
57
+ * @returns - The payload object
58
+ */
59
+ mapAttributes() {
60
+ const identityPayload = {
61
+ city: this.identityData.city,
62
+ country: this.identityData.country,
63
+ dob: this.identityData.dob,
64
+ email: this.identityData.email,
65
+ firstname: this.identityData.firstName,
66
+ gender: this.identityData.gender,
67
+ identifiers: this.identityData.identifiers.map((value) => {
68
+ return {
69
+ // eslint-disable-next-line @typescript-eslint/naming-convention
70
+ expiry_date: value.expiryDate,
71
+ id: value.id,
72
+ provider: value.provider,
73
+ };
74
+ }),
75
+ lastname: this.identityData.lastName,
76
+ mobile: this.identityData.mobile,
77
+ phone: this.identityData.phone,
78
+ // eslint-disable-next-line @typescript-eslint/naming-convention
79
+ postal_code: this.identityData.postalCode,
80
+ state: this.identityData.state,
81
+ street: this.identityData.street,
82
+ title: this.identityData.title,
83
+ type: 'IDENTITY',
84
+ };
85
+ if (this.numberOfExtensionDataProperties > 0)
86
+ identityPayload.ext = this.extensionData;
87
+ return identityPayload;
88
+ }
89
+ }
@@ -0,0 +1,24 @@
1
+ import { getAnalyticsPlugin } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { sendEvent } from '../send-event/sendEvent';
3
+ import { IdentityEvent } from './identity-event';
4
+ import { getCoreContext } from '@sitecore-content-sdk/core';
5
+ import { getEventsPlugin } from '../../initialization/plugin';
6
+ /**
7
+ * A function that sends an IDENTITY event to the SitecoreCloud API
8
+ * @param {IdentityData} identityData - The required/optional attributes to be sent to the SitecoreCloud API
9
+ * @returns The response object that Sitecore Edge Proxy returns
10
+ * @public
11
+ */
12
+ export async function identity(identityData) {
13
+ const coreContext = getCoreContext();
14
+ await coreContext.readyPromise;
15
+ getEventsPlugin();
16
+ const { options, adapter } = getAnalyticsPlugin();
17
+ const id = adapter.getClientId() || '';
18
+ return new IdentityEvent({
19
+ id,
20
+ identityData,
21
+ sendEvent,
22
+ config: Object.assign(Object.assign({}, coreContext.config), options),
23
+ }).send();
24
+ }
@@ -0,0 +1,4 @@
1
+ export { BaseEvent } from './base-event';
2
+ export { PageViewEvent } from './page-view/page-view-event';
3
+ export { CustomEvent } from './custom-event/custom-event';
4
+ export { IdentityEvent } from './identity/identity-event';
@@ -0,0 +1,127 @@
1
+ import { MAX_EXT_ATTRIBUTES, UTM_PREFIX } from '../consts';
2
+ import { BaseEvent } from '../base-event';
3
+ import { flattenObject } from '@sitecore-content-sdk/analytics-core/utils';
4
+ import { constants } from '@sitecore-content-sdk/core';
5
+ const { ERROR_MESSAGES } = constants;
6
+ /**
7
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
8
+ */
9
+ export class PageViewEvent extends BaseEvent {
10
+ /**
11
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
12
+ * @param {PageViewEventArguments} args - Unified object containing the required properties
13
+ */
14
+ constructor(args) {
15
+ var _a;
16
+ const { channel, currency, language, page, extensionData } = Object.assign({}, args.pageViewData);
17
+ super({
18
+ channel,
19
+ currency,
20
+ language,
21
+ page,
22
+ }, args.id);
23
+ this.extensionData = {};
24
+ this.pageViewData = args.pageViewData;
25
+ this.sendEvent = args.sendEvent;
26
+ this.config = args.config;
27
+ this.urlSearchParams = new URLSearchParams(decodeURI(args.searchParams));
28
+ if (extensionData)
29
+ this.extensionData = flattenObject({ object: extensionData });
30
+ const numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
31
+ if (numberOfExtensionDataProperties > MAX_EXT_ATTRIBUTES)
32
+ throw new Error(ERROR_MESSAGES.IV_006(MAX_EXT_ATTRIBUTES));
33
+ this.includeUTMParameters =
34
+ (_a = (args.pageViewData && args.pageViewData.includeUTMParameters)) !== null && _a !== void 0 ? _a : true;
35
+ }
36
+ /**
37
+ * Sends the event to Sitecore Edge Proxy
38
+ * @returns - A promise that resolves with either the Sitecore Edge Proxy response object or null
39
+ */
40
+ async send() {
41
+ const baseAttr = this.mapBaseEventPayload();
42
+ const eventAttrs = this.mapAttributes();
43
+ PageViewEvent.isFirstPageView = false;
44
+ return await this.sendEvent(Object.assign(Object.assign({}, baseAttr), eventAttrs), this.config);
45
+ }
46
+ /**
47
+ * Gets the variant ID from the url if not passed by the developer
48
+ * Gets the variant ID from the extension data if not found from the url
49
+ * @param {string} [pageVariantIdFromPageViewData] - The variant ID from the page view data
50
+ * @param {string} [pageVariantIdFromExt] - The variant ID from extension data
51
+ * @returns - variant ID or null
52
+ */
53
+ getPageVariantId(pageVariantIdFromPageViewData, pageVariantIdFromExt) {
54
+ if (pageVariantIdFromPageViewData)
55
+ return pageVariantIdFromPageViewData;
56
+ const pageVariantIdFromURL = this.urlSearchParams.get('variantid');
57
+ if (pageVariantIdFromURL)
58
+ return pageVariantIdFromURL;
59
+ if (pageVariantIdFromExt)
60
+ return pageVariantIdFromExt;
61
+ return null;
62
+ }
63
+ /**
64
+ * Returns the referrer if exists on page view event else null if on server and no referrer is on event, else
65
+ * returns the href if on client side and the document referrer is different from the window location hostname
66
+ * @returns - the referrer
67
+ */
68
+ getReferrer() {
69
+ var _a;
70
+ if ((_a = this.pageViewData) === null || _a === void 0 ? void 0 : _a.referrer)
71
+ return this.pageViewData.referrer;
72
+ if (typeof window === 'undefined')
73
+ return null;
74
+ if (!PageViewEvent.isFirstPageView || !document.referrer)
75
+ return null;
76
+ const { hostname, href } = new URL(document.referrer);
77
+ return window.location.hostname !== hostname ? href : null;
78
+ }
79
+ /**
80
+ * Maps parameters given as input to corresponding attributes send to the API
81
+ * @returns the mapped object to be sent as payload
82
+ */
83
+ mapAttributes() {
84
+ var _a;
85
+ let pageViewPayload = {
86
+ type: 'VIEW',
87
+ };
88
+ const pageVariantId = this.pageViewData &&
89
+ this.getPageVariantId(this.pageViewData.pageVariantId, this.extensionData.pageVariantId);
90
+ if (pageVariantId !== null)
91
+ pageViewPayload.ext = Object.assign(Object.assign({}, pageViewPayload.ext), { pageVariantId });
92
+ if (Object.keys(this.extensionData).length > 0) {
93
+ delete this.extensionData.pageVariantId;
94
+ pageViewPayload.ext = Object.assign(Object.assign({}, pageViewPayload.ext), this.extensionData);
95
+ }
96
+ if (this.includeUTMParameters) {
97
+ const utmParameters = this.getUTMParameters();
98
+ pageViewPayload = Object.assign(Object.assign({}, pageViewPayload), utmParameters);
99
+ }
100
+ const referrer = this.getReferrer();
101
+ if (referrer !== null)
102
+ pageViewPayload = Object.assign(Object.assign({}, pageViewPayload), { referrer });
103
+ if ((_a = this.pageViewData) === null || _a === void 0 ? void 0 : _a.searchData)
104
+ pageViewPayload.sc_search = {
105
+ data: this.pageViewData.searchData,
106
+ metadata: {
107
+ // eslint-disable-next-line @typescript-eslint/naming-convention
108
+ ut_api_version: '1.0',
109
+ },
110
+ };
111
+ return pageViewPayload;
112
+ }
113
+ /**
114
+ * Retrieves UTM parameters from the url query string
115
+ * @returns - an object containing the UTM parameters if they exist
116
+ */
117
+ getUTMParameters() {
118
+ const utmParameters = {};
119
+ this.urlSearchParams.forEach((value, key) => {
120
+ const param = key.toLowerCase();
121
+ if (param.indexOf(UTM_PREFIX) === 0)
122
+ utmParameters[param] = value;
123
+ });
124
+ return utmParameters;
125
+ }
126
+ }
127
+ PageViewEvent.isFirstPageView = true;
@@ -0,0 +1,27 @@
1
+ import { getAnalyticsPlugin } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { sendEvent } from '../send-event/sendEvent';
3
+ import { PageViewEvent } from './page-view-event';
4
+ import { getCoreContext } from '@sitecore-content-sdk/core';
5
+ import { getEventsPlugin } from '../../initialization/plugin';
6
+ /**
7
+ * A function that sends a VIEW event to the SitecoreCloud API
8
+ * @param {PageViewData} [pageViewData] - The optional attributes to be sent to the SitecoreCloud API
9
+ * This object will be flattened and sent in the ext object of the payload
10
+ * @returns The response object that Sitecore Edge Proxy returns
11
+ * @public
12
+ */
13
+ export async function pageView(pageViewData) {
14
+ const coreContext = getCoreContext();
15
+ await coreContext.readyPromise;
16
+ getEventsPlugin();
17
+ const { options, adapter } = getAnalyticsPlugin();
18
+ const id = adapter.getClientId() || '';
19
+ const searchParams = adapter.location.getSearchParams();
20
+ return new PageViewEvent({
21
+ id,
22
+ pageViewData,
23
+ searchParams,
24
+ sendEvent,
25
+ config: Object.assign(Object.assign({}, coreContext.config), options),
26
+ }).send();
27
+ }
@@ -0,0 +1,33 @@
1
+ import { API_VERSION } from '@sitecore-content-sdk/analytics-core/internal';
2
+ import { PACKAGE_VERSION, X_CLIENT_SOFTWARE_ID } from '../../consts';
3
+ import { NativeDataFetcher } from '@sitecore-content-sdk/core';
4
+ import { debug } from '../../debug';
5
+ /**
6
+ * This function sends an event to Sitecore Edge Proxy
7
+ * @param {EPFetchBody & BasePayload} body - The event data to send
8
+ * @param {CoreContext['config']} config - The global configuration
9
+ * @internal
10
+ */
11
+ export async function sendEvent(body, config) {
12
+ // eslint-disable-next-line max-len
13
+ const eventUrl = `${config.edgeUrl}/v1/events/${API_VERSION}/events?siteId=${config.siteName}`;
14
+ const fetchOptions = {
15
+ body: JSON.stringify(body),
16
+ headers: {
17
+ 'Content-Type': 'application/json',
18
+ 'X-Client-Software-ID': X_CLIENT_SOFTWARE_ID,
19
+ 'X-Library-Version': PACKAGE_VERSION,
20
+ 'x-sitecore-contextid': config.contextId,
21
+ },
22
+ method: 'POST',
23
+ };
24
+ const fetcher = new NativeDataFetcher({ debugger: debug.events });
25
+ return await fetcher
26
+ .fetch(eventUrl, fetchOptions)
27
+ .then(async (response) => {
28
+ return response.data;
29
+ })
30
+ .catch(() => {
31
+ return null;
32
+ });
33
+ }
@@ -0,0 +1,5 @@
1
+ export { form } from './events/custom-event/form';
2
+ export { event } from './events/custom-event/event';
3
+ export { identity } from './events/identity/identity';
4
+ export { pageView } from './events/page-view/page-view';
5
+ export { eventsPlugin } from './initialization/plugin';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The name of the Events plugin.
3
+ * @public
4
+ */
5
+ export const EVENTS_PLUGIN_NAME = 'EventsPlugin';
@@ -0,0 +1,52 @@
1
+ import { getCoreContext, constants } from '@sitecore-content-sdk/core';
2
+ import { EVENTS_PLUGIN_NAME } from './const';
3
+ import { PACKAGE_VERSION } from '../consts';
4
+ import { event } from '../events/custom-event/event';
5
+ import { form } from '../events/custom-event/form';
6
+ import { identity } from '../events/identity/identity';
7
+ import { pageView } from '../events/page-view/page-view';
8
+ import { addToEventQueue } from '../eventStorage/addToEventQueue';
9
+ import { clearEventQueue } from '../eventStorage/clearEventQueue';
10
+ import { processEventQueue } from '../eventStorage/processEventQueue';
11
+ import { ANALYTICS_PLUGIN_NAME } from '@sitecore-content-sdk/analytics-core/internal';
12
+ const { ERROR_MESSAGES } = constants;
13
+ /**
14
+ * Initializes the events plugin with the provided options.
15
+ * @internal
16
+ */
17
+ async function init() {
18
+ if (typeof window !== 'undefined')
19
+ window.scContentSDK = Object.assign(Object.assign({}, window.scContentSDK), { events: {
20
+ addToEventQueue,
21
+ clearEventQueue,
22
+ event,
23
+ form,
24
+ identity,
25
+ pageView,
26
+ processEventQueue,
27
+ version: PACKAGE_VERSION,
28
+ } });
29
+ }
30
+ /**
31
+ * Creates an events plugin with the provided options.
32
+ * @returns {EventsPlugin} The events plugin instance.
33
+ * @public
34
+ */
35
+ export function eventsPlugin() {
36
+ return {
37
+ name: EVENTS_PLUGIN_NAME,
38
+ init,
39
+ dependencies: [ANALYTICS_PLUGIN_NAME],
40
+ };
41
+ }
42
+ /**
43
+ * Retrieves the events plugin instance from the core context.
44
+ * @returns {EventsPlugin} The events plugin instance.
45
+ * @internal
46
+ */
47
+ export function getEventsPlugin() {
48
+ const plugin = getCoreContext().plugins.get(EVENTS_PLUGIN_NAME);
49
+ if (!plugin)
50
+ throw new Error(ERROR_MESSAGES.IE_004(EVENTS_PLUGIN_NAME));
51
+ return plugin;
52
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { EVENTS_PLUGIN_NAME } from './initialization/const';
2
+ export { PACKAGE_VERSION, PACKAGE_NAME } from './consts';
package/internal.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './types/src/internal';
2
+