@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
@@ -0,0 +1,13 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ import { ExtensionData } from '../common-interfaces';
3
+ import { ICustomEventInput } from './custom-event';
4
+ import { TRequest } from '@sitecore-cloudsdk/utils';
5
+ /**
6
+ * A function that sends an event to SitecoreCloud API with the specified type
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 request - Interface with constraint for extending request
10
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
11
+ * @returns The response object that Sitecore CDP returns
12
+ */
13
+ export declare function eventServer<T extends TRequest>(type: string, eventData: ICustomEventInput, request: T, extensionData?: ExtensionData): Promise<ICdpResponse | null>;
@@ -0,0 +1,28 @@
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.eventServer = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const custom_event_1 = require("./custom-event");
7
+ const initializer_1 = require("../../initializer/server/initializer");
8
+ /**
9
+ * A function that sends an event to SitecoreCloud API with the specified type
10
+ * @param type - The required type of the event
11
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
12
+ * @param request - Interface with constraint for extending request
13
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
14
+ * @returns The response object that Sitecore CDP returns
15
+ */
16
+ function eventServer(type, eventData, request, extensionData) {
17
+ const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
18
+ const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
19
+ return new custom_event_1.CustomEvent({
20
+ eventApiClient,
21
+ eventData,
22
+ extensionData,
23
+ id,
24
+ settings: settings,
25
+ type,
26
+ }).send();
27
+ }
28
+ exports.eventServer = eventServer;
@@ -0,0 +1,9 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ /**
3
+ * A function that sends a form event to SitecoreCloud API
4
+ * @param formId - The required form ID string
5
+ * @param interactionType - The required interaction type string. Possible values: "VIEWED", "SUBMITTED"
6
+ * settings object, you must specify it here
7
+ * @returns The response object that Sitecore CDP returns or null
8
+ */
9
+ export declare function form(formId: string, interactionType: 'VIEWED' | 'SUBMITTED'): Promise<ICdpResponse | null>;
@@ -0,0 +1,30 @@
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.form = void 0;
5
+ const initializer_1 = require("../../initializer/browser/initializer");
6
+ const custom_event_1 = require("./custom-event");
7
+ /**
8
+ * A function that sends a form event to SitecoreCloud API
9
+ * @param formId - The required form ID string
10
+ * @param interactionType - The required interaction type string. Possible values: "VIEWED", "SUBMITTED"
11
+ * settings object, you must specify it here
12
+ * @returns The response object that Sitecore CDP returns or null
13
+ */
14
+ function form(formId, interactionType) {
15
+ const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
16
+ const formEvent = new custom_event_1.CustomEvent({
17
+ eventApiClient,
18
+ eventData: {},
19
+ extensionData: {
20
+ formId,
21
+ interactionType: interactionType.toUpperCase(),
22
+ },
23
+ id,
24
+ settings,
25
+ type: 'FORM',
26
+ });
27
+ formEvent.page = undefined;
28
+ return formEvent.send();
29
+ }
30
+ exports.form = form;
@@ -0,0 +1,99 @@
1
+ import { BaseEvent } from '../base-event';
2
+ import { ExtensionData, IEventAttributesInput } from '../common-interfaces';
3
+ import { IEventApiClient } from '../../cdp/EventApiClient';
4
+ import { IFlattenedObject } from '@sitecore-cloudsdk/utils';
5
+ import { ICdpResponse, IInfer, ISettings } from '@sitecore-cloudsdk/core';
6
+ export declare class IdentityEvent extends BaseEvent {
7
+ private eventData;
8
+ private eventApiClient;
9
+ private extensionData;
10
+ private numberOfExtensionDataProperties;
11
+ /**
12
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
13
+ * @param args - Unified object containing the required properties
14
+ */
15
+ constructor(args: IIdentityEventArguments);
16
+ /**
17
+ * Function that validates the identifiers object, email and date attributes for CDN users
18
+ * * @param eventData - The data to be validated
19
+ */
20
+ private validateAttributes;
21
+ /**
22
+ * A function that maps the identity event input data with the payload sent to the API
23
+ * @returns - The payload object
24
+ */
25
+ private mapAttributes;
26
+ /**
27
+ * Sends the event to Sitecore CDP
28
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
29
+ */
30
+ send(): Promise<ICdpResponse | null>;
31
+ }
32
+ /**
33
+ * The JSON array of objects that contain the identity identifiers
34
+ */
35
+ interface ICDPIdentifier {
36
+ expiry_date?: string;
37
+ id: string;
38
+ provider: string;
39
+ }
40
+ /**
41
+ * Interface with the necessary attributes for the input for sending Identity events
42
+ */
43
+ export interface IIdentityEventAttributesInput extends IEventAttributesInput {
44
+ city?: string;
45
+ country?: string;
46
+ dob?: string;
47
+ email?: string;
48
+ firstName?: string;
49
+ gender?: string;
50
+ identifiers: IIdentifier[];
51
+ lastName?: string;
52
+ mobile?: string;
53
+ phone?: string;
54
+ postalCode?: string;
55
+ state?: string;
56
+ street?: string[];
57
+ title?: string;
58
+ }
59
+ /**
60
+ * The JSON array of objects that contain the identity identifiers
61
+ */
62
+ export interface IIdentifier {
63
+ expiryDate?: string;
64
+ id: string;
65
+ provider: string;
66
+ }
67
+ /**
68
+ * An interface describing the identity event specific payload to be sent to the API
69
+ */
70
+ export interface IIdentityEventPayload {
71
+ city?: string;
72
+ country?: string;
73
+ dob?: string;
74
+ email?: string;
75
+ firstname?: string;
76
+ gender?: string;
77
+ identifiers: ICDPIdentifier[];
78
+ lastname?: string;
79
+ mobile?: string;
80
+ phone?: string;
81
+ postal_code?: string;
82
+ state?: string;
83
+ street?: string[];
84
+ title?: string;
85
+ type: 'IDENTITY';
86
+ ext?: IFlattenedObject;
87
+ }
88
+ /**
89
+ * Interface of the unified arguments object for identity event
90
+ */
91
+ export interface IIdentityEventArguments {
92
+ eventApiClient: IEventApiClient;
93
+ eventData: IIdentityEventAttributesInput;
94
+ extensionData?: ExtensionData;
95
+ id: string;
96
+ settings: ISettings;
97
+ infer?: IInfer;
98
+ }
99
+ export {};
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityEvent = 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 consts_1 = require("../consts");
7
+ const utils_1 = require("@sitecore-cloudsdk/utils");
8
+ class IdentityEvent 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 } = args.eventData;
15
+ super({ channel, currency, language, page }, args.settings, args.id);
16
+ this.extensionData = {};
17
+ this.numberOfExtensionDataProperties = 0;
18
+ this.validateAttributes(args.eventData);
19
+ this.eventData = args.eventData;
20
+ this.eventApiClient = args.eventApiClient;
21
+ if (args.extensionData)
22
+ this.extensionData = (0, utils_1.flattenObject)({ object: args.extensionData });
23
+ this.numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
24
+ if (this.numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
25
+ throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
26
+ }
27
+ /**
28
+ * Function that validates the identifiers object, email and date attributes for CDN users
29
+ * * @param eventData - The data to be validated
30
+ */
31
+ validateAttributes(eventData) {
32
+ if (eventData.identifiers.length === 0)
33
+ throw new Error(`[MV-0004] "identifiers" is required.`);
34
+ if (eventData.dob !== undefined && !(0, utils_1.isShortISODateString)(eventData.dob))
35
+ throw new Error(`[IV-0002] Incorrect value for "dob". Format the value according to ISO 8601.`);
36
+ eventData.identifiers.forEach((identifier) => {
37
+ if (identifier.expiryDate && !(0, utils_1.isShortISODateString)(identifier.expiryDate))
38
+ throw new Error(`[IV-0004] Incorrect value for "expiryDate". Format the value according to ISO 8601.`);
39
+ });
40
+ if (eventData.email && !(0, utils_1.isValidEmail)(eventData.email))
41
+ throw new Error(`[IV-0003] Incorrect value for "email". Set the value to a valid email address.`);
42
+ }
43
+ /**
44
+ * A function that maps the identity event input data with the payload sent to the API
45
+ * @returns - The payload object
46
+ */
47
+ mapAttributes() {
48
+ const identityPayload = {
49
+ city: this.eventData.city,
50
+ country: this.eventData.country,
51
+ dob: this.eventData.dob,
52
+ email: this.eventData.email,
53
+ firstname: this.eventData.firstName,
54
+ gender: this.eventData.gender,
55
+ identifiers: this.eventData.identifiers.map((value) => {
56
+ return {
57
+ // eslint-disable-next-line @typescript-eslint/naming-convention
58
+ expiry_date: value.expiryDate,
59
+ id: value.id,
60
+ provider: value.provider,
61
+ };
62
+ }),
63
+ lastname: this.eventData.lastName,
64
+ mobile: this.eventData.mobile,
65
+ phone: this.eventData.phone,
66
+ // eslint-disable-next-line @typescript-eslint/naming-convention
67
+ postal_code: this.eventData.postalCode,
68
+ state: this.eventData.state,
69
+ street: this.eventData.street,
70
+ title: this.eventData.title,
71
+ type: 'IDENTITY',
72
+ };
73
+ if (this.numberOfExtensionDataProperties > 0)
74
+ identityPayload.ext = this.extensionData;
75
+ return identityPayload;
76
+ }
77
+ /**
78
+ * Sends the event to Sitecore CDP
79
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
80
+ */
81
+ async send() {
82
+ const baseAttr = this.mapBaseEventPayload();
83
+ const eventAttrs = this.mapAttributes();
84
+ const fetchBody = Object.assign({}, eventAttrs, baseAttr);
85
+ return await this.eventApiClient.send(fetchBody);
86
+ }
87
+ }
88
+ exports.IdentityEvent = IdentityEvent;
@@ -0,0 +1,11 @@
1
+ import { ExtensionData } from '../common-interfaces';
2
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
3
+ import { IIdentityEventAttributesInput } from './identity-event';
4
+ /**
5
+ * A function that sends an IDENTITY event to SitecoreCloud API
6
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
7
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
8
+ * This object will be flattened and sent in the ext object of the payload
9
+ * @returns The response object that Sitecore CDP returns
10
+ */
11
+ export declare function identity(eventData: IIdentityEventAttributesInput, extensionData?: ExtensionData): Promise<ICdpResponse | null>;
@@ -0,0 +1,24 @@
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.identity = void 0;
5
+ const initializer_1 = require("../../initializer/browser/initializer");
6
+ const identity_event_1 = require("./identity-event");
7
+ /**
8
+ * A function that sends an IDENTITY event to SitecoreCloud API
9
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
10
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
11
+ * This object will be flattened and sent in the ext object of the payload
12
+ * @returns The response object that Sitecore CDP returns
13
+ */
14
+ function identity(eventData, extensionData) {
15
+ const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
16
+ return new identity_event_1.IdentityEvent({
17
+ eventApiClient,
18
+ eventData,
19
+ extensionData,
20
+ id,
21
+ settings,
22
+ }).send();
23
+ }
24
+ exports.identity = identity;
@@ -0,0 +1,13 @@
1
+ import { ExtensionData } from '../common-interfaces';
2
+ import { TRequest } from '@sitecore-cloudsdk/utils';
3
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
4
+ import { IIdentityEventAttributesInput } from './identity-event';
5
+ /**
6
+ * A function that sends an IDENTITY event to SitecoreCloud API
7
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
8
+ * @param request - Interface with constraint for extending request
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
+ * @returns The response object that Sitecore CDP returns
12
+ */
13
+ export declare function identityServer(eventData: IIdentityEventAttributesInput, request: TRequest, extensionData?: ExtensionData): Promise<ICdpResponse | null>;
@@ -0,0 +1,27 @@
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.identityServer = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const initializer_1 = require("../../initializer/server/initializer");
7
+ const identity_event_1 = require("./identity-event");
8
+ /**
9
+ * A function that sends an IDENTITY event to SitecoreCloud API
10
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
11
+ * @param request - Interface with constraint for extending request
12
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
13
+ * This object will be flattened and sent in the ext object of the payload
14
+ * @returns The response object that Sitecore CDP returns
15
+ */
16
+ function identityServer(eventData, request, extensionData) {
17
+ const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
18
+ const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
19
+ return new identity_event_1.IdentityEvent({
20
+ eventApiClient,
21
+ eventData,
22
+ extensionData,
23
+ id,
24
+ settings: settings,
25
+ }).send();
26
+ }
27
+ exports.identityServer = identityServer;
@@ -0,0 +1,9 @@
1
+ export { BaseEvent } from './base-event';
2
+ export type { IBasePayload } from './base-event';
3
+ export { PageViewEvent } from './page-view/page-view-event';
4
+ export type { IPageViewEventInput, IPageViewEventPayload } from './page-view/page-view-event';
5
+ export { CustomEvent } from './custom-event/custom-event';
6
+ export type { ICustomEventInput, ICustomEventPayload, ICustomEventArguments } from './custom-event/custom-event';
7
+ export { IdentityEvent } from './identity/identity-event';
8
+ export type { IIdentityEventAttributesInput, IIdentityEventPayload } from './identity/identity-event';
9
+ export type { ExtensionData } from './common-interfaces';
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityEvent = exports.CustomEvent = exports.PageViewEvent = exports.BaseEvent = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ var base_event_1 = require("./base-event");
6
+ Object.defineProperty(exports, "BaseEvent", { enumerable: true, get: function () { return base_event_1.BaseEvent; } });
7
+ var page_view_event_1 = require("./page-view/page-view-event");
8
+ Object.defineProperty(exports, "PageViewEvent", { enumerable: true, get: function () { return page_view_event_1.PageViewEvent; } });
9
+ var custom_event_1 = require("./custom-event/custom-event");
10
+ Object.defineProperty(exports, "CustomEvent", { enumerable: true, get: function () { return custom_event_1.CustomEvent; } });
11
+ var identity_event_1 = require("./identity/identity-event");
12
+ Object.defineProperty(exports, "IdentityEvent", { enumerable: true, get: function () { return identity_event_1.IdentityEvent; } });
@@ -0,0 +1,75 @@
1
+ import { ICdpResponse, IInfer, ISettings } from '@sitecore-cloudsdk/core';
2
+ import { IFlattenedObject, INestedObject } from '@sitecore-cloudsdk/utils';
3
+ import { BaseEvent } from '../base-event';
4
+ import { IEventApiClient } from '../../cdp/EventApiClient';
5
+ import { IEventAttributesInput } from '../common-interfaces';
6
+ export declare class PageViewEvent extends BaseEvent {
7
+ static isFirstPageView: boolean;
8
+ private eventApiClient;
9
+ private eventData;
10
+ private extensionData;
11
+ private urlSearchParams;
12
+ /**
13
+ * A class that extends from {@link BaseEvent} and has all the required functionality to send a VIEW event
14
+ * @param args - Unified object containing the required properties
15
+ */
16
+ constructor(args: IPageViewEventArguments);
17
+ /**
18
+ * Gets the variant ID from the url if not passed by the developer
19
+ * Gets the variant ID from the extension data if not found from the url
20
+ * @returns - variant ID or null
21
+ */
22
+ private getPageVariantId;
23
+ /**
24
+ * Returns the referrer if exists on page view event else null if we are on server and no referrer is on event, else
25
+ * returns the href if on client side and the document referrer is different from the window location hostname
26
+ * @returns - the referrer
27
+ */
28
+ private getReferrer;
29
+ /**
30
+ * Maps parameters given as input to corresponding attributes send to the API
31
+ * @returns the mapped object to be sent as payload
32
+ */
33
+ private mapAttributes;
34
+ /**
35
+ * Sends the event to Sitecore CDP
36
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
37
+ */
38
+ send(): Promise<ICdpResponse | null>;
39
+ }
40
+ /**
41
+ * Interface of the unified arguments object for page view event
42
+ */
43
+ export interface IPageViewEventArguments {
44
+ eventApiClient: IEventApiClient;
45
+ eventData: IPageViewEventInput;
46
+ id: string;
47
+ settings: ISettings;
48
+ infer?: IInfer;
49
+ extensionData?: INestedObject;
50
+ searchParams: string;
51
+ }
52
+ /**
53
+ * Type with the required/optional attributes in order to send a view event to SitecoreCloud API
54
+ */
55
+ export interface IPageViewEventInput extends IEventAttributesInput {
56
+ pageVariantId?: string;
57
+ referrer?: string;
58
+ }
59
+ /**
60
+ * Interface with the utm_ parameters
61
+ */
62
+ interface IUtmParameters {
63
+ [key: `utm_${string}`]: string;
64
+ }
65
+ /**
66
+ * An interface describing the page view event specific payload to be sent * to the API
67
+ */
68
+ export interface IPageViewEventPayload extends IUtmParameters {
69
+ type: 'VIEW';
70
+ referrer?: string;
71
+ ext?: {
72
+ pageVariantId?: string;
73
+ } & IFlattenedObject;
74
+ }
75
+ export {};
@@ -0,0 +1,94 @@
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.PageViewEvent = void 0;
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const base_event_1 = require("../base-event");
7
+ const consts_1 = require("../consts");
8
+ class PageViewEvent 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 } = args.eventData;
15
+ super({
16
+ channel,
17
+ currency,
18
+ language,
19
+ page,
20
+ }, args.settings, args.id);
21
+ this.extensionData = {};
22
+ this.eventData = args.eventData;
23
+ this.urlSearchParams = new URLSearchParams(decodeURI(args.searchParams));
24
+ if (args.extensionData)
25
+ this.extensionData = (0, utils_1.flattenObject)({ object: args.extensionData });
26
+ const numberOfExtensionDataProperties = Object.entries(this.extensionData).length;
27
+ if (numberOfExtensionDataProperties > consts_1.MAX_EXT_ATTRIBUTES)
28
+ throw new Error(`[IV-0005] This event supports maximum ${consts_1.MAX_EXT_ATTRIBUTES} attributes. Reduce the number of attributes.`);
29
+ this.eventApiClient = args.eventApiClient;
30
+ }
31
+ /**
32
+ * Gets the variant ID from the url if not passed by the developer
33
+ * Gets the variant ID from the extension data if not found from the url
34
+ * @returns - variant ID or null
35
+ */
36
+ getPageVariantId(pageVariantIdFromEventData, pageVariantIdFromExt) {
37
+ if (pageVariantIdFromEventData)
38
+ return pageVariantIdFromEventData;
39
+ const pageVariantIdFromURL = this.urlSearchParams.get('variantid');
40
+ if (pageVariantIdFromURL)
41
+ return pageVariantIdFromURL;
42
+ if (pageVariantIdFromExt)
43
+ return pageVariantIdFromExt;
44
+ return null;
45
+ }
46
+ /**
47
+ * Returns the referrer if exists on page view event else null if we are on server and no referrer is on event, else
48
+ * returns the href if on client side and the document referrer is different from the window location hostname
49
+ * @returns - the referrer
50
+ */
51
+ getReferrer() {
52
+ if (this.eventData.referrer)
53
+ return this.eventData.referrer;
54
+ if (typeof window === 'undefined')
55
+ return null;
56
+ if (!PageViewEvent.isFirstPageView || !document.referrer)
57
+ return null;
58
+ const { hostname, href } = new URL(document.referrer);
59
+ return window.location.hostname !== hostname ? href : null;
60
+ }
61
+ /**
62
+ * Maps parameters given as input to corresponding attributes send to the API
63
+ * @returns the mapped object to be sent as payload
64
+ */
65
+ mapAttributes() {
66
+ let viewPayload = {
67
+ type: 'VIEW',
68
+ };
69
+ const pageVariantId = this.getPageVariantId(this.eventData.pageVariantId, this.extensionData['pageVariantId']);
70
+ if (pageVariantId !== null)
71
+ viewPayload.ext = { ...viewPayload.ext, pageVariantId };
72
+ if (Object.keys(this.extensionData).length > 0) {
73
+ delete this.extensionData['pageVariantId'];
74
+ viewPayload.ext = { ...viewPayload.ext, ...this.extensionData };
75
+ }
76
+ const referrer = this.getReferrer();
77
+ if (referrer !== null)
78
+ viewPayload = { ...viewPayload, referrer };
79
+ return viewPayload;
80
+ }
81
+ /**
82
+ * Sends the event to Sitecore CDP
83
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
84
+ */
85
+ async send() {
86
+ const baseAttr = this.mapBaseEventPayload();
87
+ const eventAttrs = this.mapAttributes();
88
+ const fetchBody = Object.assign({}, eventAttrs, baseAttr);
89
+ PageViewEvent.isFirstPageView = false;
90
+ return await this.eventApiClient.send(fetchBody);
91
+ }
92
+ }
93
+ exports.PageViewEvent = PageViewEvent;
94
+ PageViewEvent.isFirstPageView = true;
@@ -0,0 +1,12 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ import { INestedObject, TRequest } from '@sitecore-cloudsdk/utils';
3
+ import { IPageViewEventInput } from './page-view-event';
4
+ /**
5
+ * A function that sends a VIEW event to SitecoreCloud API
6
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
7
+ * @param request - Interface with constraint for extending request
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 pageViewServer<T extends TRequest>(eventData: IPageViewEventInput, request: T, extensionData?: INestedObject): Promise<ICdpResponse | null>;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pageViewServer = 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 initializer_1 = require("../../initializer/server/initializer");
7
+ const page_view_event_1 = require("./page-view-event");
8
+ /**
9
+ * A function that sends a VIEW event to SitecoreCloud API
10
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
11
+ * @param request - Interface with constraint for extending request
12
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
13
+ * This object will be flattened and sent in the ext object of the payload
14
+ * @returns The response object that Sitecore CDP returns
15
+ */
16
+ function pageViewServer(eventData, request, extensionData) {
17
+ const { eventApiClient, settings } = (0, initializer_1.getServerDependencies)();
18
+ const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
19
+ // Host is irrelevant but necessary to support relative URL
20
+ const requestUrl = new URL(request.url, `https://localhost`);
21
+ return new page_view_event_1.PageViewEvent({
22
+ eventApiClient,
23
+ eventData,
24
+ extensionData,
25
+ id,
26
+ searchParams: requestUrl.search,
27
+ settings,
28
+ }).send();
29
+ }
30
+ exports.pageViewServer = pageViewServer;
@@ -0,0 +1,11 @@
1
+ import { ICdpResponse } from '@sitecore-cloudsdk/core';
2
+ import { INestedObject } from '@sitecore-cloudsdk/utils';
3
+ import { IPageViewEventInput } from './page-view-event';
4
+ /**
5
+ * A function that sends a VIEW event to SitecoreCloud API
6
+ * @param eventData - The required/optional attributes in order to be send to SitecoreCloud API
7
+ * @param extensionData - The optional extensionData attributes that will be sent to SitecoreCloud API.
8
+ * This object will be flattened and sent in the ext object of the payload
9
+ * @returns The response object that Sitecore CDP returns
10
+ */
11
+ export declare function pageView(eventData: IPageViewEventInput, extensionData?: INestedObject): Promise<ICdpResponse | null>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pageView = void 0;
4
+ const initializer_1 = require("../../initializer/browser/initializer");
5
+ const page_view_event_1 = require("./page-view-event");
6
+ /**
7
+ * A function that sends a VIEW event to SitecoreCloud API
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
+ * @returns The response object that Sitecore CDP returns
12
+ */
13
+ function pageView(eventData, extensionData) {
14
+ const { eventApiClient, id, settings } = (0, initializer_1.getDependencies)();
15
+ return new page_view_event_1.PageViewEvent({
16
+ eventApiClient,
17
+ eventData,
18
+ extensionData,
19
+ id,
20
+ searchParams: window.location.search,
21
+ settings,
22
+ }).send();
23
+ }
24
+ exports.pageView = pageView;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A function that returns the guest id.
3
+ * @returns - A promise that resolves with the guest id
4
+ * @throws - Will throw an error if the clientKey/browser id is invalid
5
+ */
6
+ export declare function getGuestId(): Promise<string>;
@@ -0,0 +1,16 @@
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.getGuestId = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const initializer_1 = require("../initializer/browser/initializer");
7
+ /**
8
+ * A function that returns the guest id.
9
+ * @returns - A promise that resolves with the guest id
10
+ * @throws - Will throw an error if the clientKey/browser id is invalid
11
+ */
12
+ function getGuestId() {
13
+ const { id, settings } = (0, initializer_1.getDependencies)();
14
+ return (0, core_1.getGuestId)(id, settings.sitecoreEdgeContextId);
15
+ }
16
+ exports.getGuestId = getGuestId;