@sitecore-content-sdk/analytics-core 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 (135) hide show
  1. package/LICENSE.txt +202 -0
  2. package/README.md +99 -0
  3. package/dist/cjs/package.json +78 -0
  4. package/dist/cjs/src/client-id/fetch-client-id-from-edge-proxy.js +41 -0
  5. package/dist/cjs/src/client-id/get-client-id.js +12 -0
  6. package/dist/cjs/src/client-id/resolve-get-client-id-url.js +13 -0
  7. package/dist/cjs/src/consts.js +42 -0
  8. package/dist/cjs/src/cookie/get-default-cookie-attributes.js +20 -0
  9. package/dist/cjs/src/correlation-id/generate-correlation-id.js +12 -0
  10. package/dist/cjs/src/index.js +11 -0
  11. package/dist/cjs/src/infer/infer.js +28 -0
  12. package/dist/cjs/src/initialization/browser-adapter.js +42 -0
  13. package/dist/cjs/src/initialization/const.js +8 -0
  14. package/dist/cjs/src/initialization/plugin.js +75 -0
  15. package/dist/cjs/src/initialization/server-adapter.js +91 -0
  16. package/dist/cjs/src/initialization/types.js +2 -0
  17. package/dist/cjs/src/interfaces.js +2 -0
  18. package/dist/cjs/src/internal.js +24 -0
  19. package/dist/cjs/src/utils/browser/appendScriptWithAttributes.js +15 -0
  20. package/dist/cjs/src/utils/converters/base-64-converter.js +18 -0
  21. package/dist/cjs/src/utils/converters/flatten-object.js +35 -0
  22. package/dist/cjs/src/utils/cookies/consts.js +4 -0
  23. package/dist/cjs/src/utils/cookies/cookie-exists-in-server-side.js +14 -0
  24. package/dist/cjs/src/utils/cookies/cookie-exists.js +13 -0
  25. package/dist/cjs/src/utils/cookies/create-cookie-string.js +20 -0
  26. package/dist/cjs/src/utils/cookies/delete-cookie.js +11 -0
  27. package/dist/cjs/src/utils/cookies/get-cookie-server-side.js +14 -0
  28. package/dist/cjs/src/utils/cookies/get-cookie-value-client-side.js +15 -0
  29. package/dist/cjs/src/utils/cookies/get-cookie.js +20 -0
  30. package/dist/cjs/src/utils/cookies/interfaces.js +2 -0
  31. package/dist/cjs/src/utils/generators/generate-v4-uuid.js +11 -0
  32. package/dist/cjs/src/utils/interfaces.js +2 -0
  33. package/dist/cjs/src/utils/validators/is-short-iso-date-string.js +20 -0
  34. package/dist/cjs/src/utils/validators/is-valid-email.js +16 -0
  35. package/dist/cjs/src/utils.js +23 -0
  36. package/dist/esm/package.json +78 -0
  37. package/dist/esm/src/client-id/fetch-client-id-from-edge-proxy.js +38 -0
  38. package/dist/esm/src/client-id/get-client-id.js +9 -0
  39. package/dist/esm/src/client-id/resolve-get-client-id-url.js +10 -0
  40. package/dist/esm/src/consts.js +36 -0
  41. package/dist/esm/src/cookie/get-default-cookie-attributes.js +17 -0
  42. package/dist/esm/src/correlation-id/generate-correlation-id.js +9 -0
  43. package/dist/esm/src/index.js +4 -0
  44. package/dist/esm/src/infer/infer.js +24 -0
  45. package/dist/esm/src/initialization/browser-adapter.js +39 -0
  46. package/dist/esm/src/initialization/const.js +5 -0
  47. package/dist/esm/src/initialization/plugin.js +71 -0
  48. package/dist/esm/src/initialization/server-adapter.js +88 -0
  49. package/dist/esm/src/initialization/types.js +1 -0
  50. package/dist/esm/src/interfaces.js +1 -0
  51. package/dist/esm/src/internal.js +7 -0
  52. package/dist/esm/src/utils/browser/appendScriptWithAttributes.js +12 -0
  53. package/dist/esm/src/utils/converters/base-64-converter.js +15 -0
  54. package/dist/esm/src/utils/converters/flatten-object.js +32 -0
  55. package/dist/esm/src/utils/cookies/consts.js +1 -0
  56. package/dist/esm/src/utils/cookies/cookie-exists-in-server-side.js +11 -0
  57. package/dist/esm/src/utils/cookies/cookie-exists.js +10 -0
  58. package/dist/esm/src/utils/cookies/create-cookie-string.js +17 -0
  59. package/dist/esm/src/utils/cookies/delete-cookie.js +8 -0
  60. package/dist/esm/src/utils/cookies/get-cookie-server-side.js +11 -0
  61. package/dist/esm/src/utils/cookies/get-cookie-value-client-side.js +12 -0
  62. package/dist/esm/src/utils/cookies/get-cookie.js +17 -0
  63. package/dist/esm/src/utils/cookies/interfaces.js +1 -0
  64. package/dist/esm/src/utils/generators/generate-v4-uuid.js +8 -0
  65. package/dist/esm/src/utils/interfaces.js +1 -0
  66. package/dist/esm/src/utils/validators/is-short-iso-date-string.js +17 -0
  67. package/dist/esm/src/utils/validators/is-valid-email.js +13 -0
  68. package/dist/esm/src/utils.js +10 -0
  69. package/internal.d.ts +2 -0
  70. package/package.json +78 -0
  71. package/types/src/client-id/fetch-client-id-from-edge-proxy.d.ts +11 -0
  72. package/types/src/client-id/fetch-client-id-from-edge-proxy.d.ts.map +1 -0
  73. package/types/src/client-id/get-client-id.d.ts +7 -0
  74. package/types/src/client-id/get-client-id.d.ts.map +1 -0
  75. package/types/src/client-id/resolve-get-client-id-url.d.ts +8 -0
  76. package/types/src/client-id/resolve-get-client-id-url.d.ts.map +1 -0
  77. package/types/src/consts.d.ts +36 -0
  78. package/types/src/consts.d.ts.map +1 -0
  79. package/types/src/cookie/get-default-cookie-attributes.d.ts +10 -0
  80. package/types/src/cookie/get-default-cookie-attributes.d.ts.map +1 -0
  81. package/types/src/correlation-id/generate-correlation-id.d.ts +7 -0
  82. package/types/src/correlation-id/generate-correlation-id.d.ts.map +1 -0
  83. package/types/src/index.d.ts +9 -0
  84. package/types/src/index.d.ts.map +1 -0
  85. package/types/src/infer/infer.d.ts +15 -0
  86. package/types/src/infer/infer.d.ts.map +1 -0
  87. package/types/src/initialization/browser-adapter.d.ts +20 -0
  88. package/types/src/initialization/browser-adapter.d.ts.map +1 -0
  89. package/types/src/initialization/const.d.ts +6 -0
  90. package/types/src/initialization/const.d.ts.map +1 -0
  91. package/types/src/initialization/plugin.d.ts +65 -0
  92. package/types/src/initialization/plugin.d.ts.map +1 -0
  93. package/types/src/initialization/server-adapter.d.ts +25 -0
  94. package/types/src/initialization/server-adapter.d.ts.map +1 -0
  95. package/types/src/initialization/types.d.ts +92 -0
  96. package/types/src/initialization/types.d.ts.map +1 -0
  97. package/types/src/interfaces.d.ts +34 -0
  98. package/types/src/interfaces.d.ts.map +1 -0
  99. package/types/src/internal.d.ts +12 -0
  100. package/types/src/internal.d.ts.map +1 -0
  101. package/types/src/utils/browser/appendScriptWithAttributes.d.ts +21 -0
  102. package/types/src/utils/browser/appendScriptWithAttributes.d.ts.map +1 -0
  103. package/types/src/utils/converters/base-64-converter.d.ts +10 -0
  104. package/types/src/utils/converters/base-64-converter.d.ts.map +1 -0
  105. package/types/src/utils/converters/flatten-object.d.ts +41 -0
  106. package/types/src/utils/converters/flatten-object.d.ts.map +1 -0
  107. package/types/src/utils/cookies/consts.d.ts +2 -0
  108. package/types/src/utils/cookies/consts.d.ts.map +1 -0
  109. package/types/src/utils/cookies/cookie-exists-in-server-side.d.ts +9 -0
  110. package/types/src/utils/cookies/cookie-exists-in-server-side.d.ts.map +1 -0
  111. package/types/src/utils/cookies/cookie-exists.d.ts +9 -0
  112. package/types/src/utils/cookies/cookie-exists.d.ts.map +1 -0
  113. package/types/src/utils/cookies/create-cookie-string.d.ts +11 -0
  114. package/types/src/utils/cookies/create-cookie-string.d.ts.map +1 -0
  115. package/types/src/utils/cookies/delete-cookie.d.ts +7 -0
  116. package/types/src/utils/cookies/delete-cookie.d.ts.map +1 -0
  117. package/types/src/utils/cookies/get-cookie-server-side.d.ts +12 -0
  118. package/types/src/utils/cookies/get-cookie-server-side.d.ts.map +1 -0
  119. package/types/src/utils/cookies/get-cookie-value-client-side.d.ts +8 -0
  120. package/types/src/utils/cookies/get-cookie-value-client-side.d.ts.map +1 -0
  121. package/types/src/utils/cookies/get-cookie.d.ts +12 -0
  122. package/types/src/utils/cookies/get-cookie.d.ts.map +1 -0
  123. package/types/src/utils/cookies/interfaces.d.ts +35 -0
  124. package/types/src/utils/cookies/interfaces.d.ts.map +1 -0
  125. package/types/src/utils/generators/generate-v4-uuid.d.ts +7 -0
  126. package/types/src/utils/generators/generate-v4-uuid.d.ts.map +1 -0
  127. package/types/src/utils/interfaces.d.ts +8 -0
  128. package/types/src/utils/interfaces.d.ts.map +1 -0
  129. package/types/src/utils/validators/is-short-iso-date-string.d.ts +8 -0
  130. package/types/src/utils/validators/is-short-iso-date-string.d.ts.map +1 -0
  131. package/types/src/utils/validators/is-valid-email.d.ts +8 -0
  132. package/types/src/utils/validators/is-valid-email.d.ts.map +1 -0
  133. package/types/src/utils.d.ts +14 -0
  134. package/types/src/utils.d.ts.map +1 -0
  135. package/utils.d.ts +1 -0
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyticsPlugin = analyticsPlugin;
4
+ exports.getAnalyticsPlugin = getAnalyticsPlugin;
5
+ const consts_1 = require("../consts");
6
+ const const_1 = require("./const");
7
+ const core_1 = require("@sitecore-content-sdk/core");
8
+ const get_client_id_1 = require("../client-id/get-client-id");
9
+ const debugInit = core_1.debug.init;
10
+ const { ERROR_MESSAGES } = core_1.constants;
11
+ /**
12
+ * Creates an analytics plugin with the provided options.
13
+ * @param {AnalyticsPluginParams} params - The parameters for the analytics plugin.
14
+ * @returns {AnalyticsPlugin} The analytics plugin instance.
15
+ * @public
16
+ */
17
+ function analyticsPlugin(params) {
18
+ var _a;
19
+ const { options, adapter } = params;
20
+ const resolvedOptions = {
21
+ cookies: {
22
+ domain: options === null || options === void 0 ? void 0 : options.cookieDomain,
23
+ enabled: (_a = options === null || options === void 0 ? void 0 : options.enableCookie) !== null && _a !== void 0 ? _a : false,
24
+ expiryDays: (options === null || options === void 0 ? void 0 : options.cookieExpiryDays) || consts_1.DEFAULT_COOKIE_EXPIRY_DAYS,
25
+ name: `${consts_1.COOKIE_NAME_PREFIX}${consts_1.CLIENT_ID_COOKIE_NAME}`,
26
+ path: (options === null || options === void 0 ? void 0 : options.cookiePath) || '/',
27
+ },
28
+ timeout: options === null || options === void 0 ? void 0 : options.timeout,
29
+ };
30
+ return {
31
+ name: const_1.ANALYTICS_PLUGIN_NAME,
32
+ init,
33
+ options: resolvedOptions,
34
+ adapter,
35
+ };
36
+ }
37
+ /**
38
+ * Initializes the analytics plugin with the provided options.
39
+ * @internal
40
+ */
41
+ async function init() {
42
+ debugInit(`Initializing ${const_1.ANALYTICS_PLUGIN_NAME}`);
43
+ const coreContext = (0, core_1.getCoreContext)();
44
+ const analyticsPlugin = getAnalyticsPlugin();
45
+ if (!analyticsPlugin.options.cookies.enabled) {
46
+ debugInit(`Cookies are disabled for ${const_1.ANALYTICS_PLUGIN_NAME}. If this was not intentional, set "enableCookie" to "true".`);
47
+ return;
48
+ }
49
+ const adapter = analyticsPlugin.adapter;
50
+ if (!adapter.getClientId() || analyticsPlugin.adapter.type !== 'browser') {
51
+ await adapter.setClientId();
52
+ debugInit(`Cookie set for ${const_1.ANALYTICS_PLUGIN_NAME}`);
53
+ }
54
+ if (analyticsPlugin.adapter.type === 'browser')
55
+ window.scContentSDK = Object.assign(Object.assign({}, window.scContentSDK), { analytics_core: {
56
+ getClientId: get_client_id_1.getClientId,
57
+ options: {
58
+ siteName: coreContext.config.siteName,
59
+ contextId: coreContext.config.contextId,
60
+ edgeUrl: coreContext.config.edgeUrl,
61
+ },
62
+ version: consts_1.LIBRARY_VERSION,
63
+ } });
64
+ }
65
+ /**
66
+ * Retrieves the analytics plugin instance from the core context.
67
+ * @returns {AnalyticsPlugin} The analytics plugin instance.
68
+ * @internal
69
+ */
70
+ function getAnalyticsPlugin() {
71
+ const plugin = (0, core_1.getCoreContext)().plugins.get(const_1.ANALYTICS_PLUGIN_NAME);
72
+ if (!plugin)
73
+ throw new Error(ERROR_MESSAGES.IE_004(const_1.ANALYTICS_PLUGIN_NAME));
74
+ return plugin;
75
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyticsServerAdapter = analyticsServerAdapter;
4
+ const internal_1 = require("../internal");
5
+ const utils_1 = require("../utils");
6
+ const plugin_1 = require("./plugin");
7
+ const core_1 = require("@sitecore-content-sdk/core");
8
+ /**
9
+ * Creates a server-based analytics adapter that reads and writes the visitor ID
10
+ * using cookies and can resolve a new client ID from the Edge proxy when needed.
11
+ * The adapter also provides access to the current URL search parameters.
12
+ * @template Request - The HTTP request type extending `IncomingMessage`.
13
+ * @template Response - The HTTP response type extending `OutgoingMessage`.
14
+ * @param {Request} request - The HTTP request object.
15
+ * @param {Response} response - The HTTP response object.
16
+ * @returns {AnalyticsServerAdapter} The analytics server adapter.
17
+ * @public
18
+ */
19
+ function analyticsServerAdapter(request, response) {
20
+ return {
21
+ type: 'server',
22
+ getClientId: () => {
23
+ return getClientId(request);
24
+ },
25
+ setClientId: async () => {
26
+ var _a;
27
+ const coreConfig = (0, core_1.getCoreContext)().config;
28
+ const analyticsOptions = (0, plugin_1.getAnalyticsPlugin)().options;
29
+ const cookieOptions = analyticsOptions.cookies;
30
+ const clientIdName = cookieOptions.name;
31
+ const legacyClientIdName = `${internal_1.COOKIE_NAME_PREFIX}${coreConfig.contextId}`;
32
+ const defaultCookieAttributes = (0, internal_1.getDefaultCookieAttributes)(cookieOptions.expiryDays, cookieOptions.domain);
33
+ const legacyClientIdCookie = (0, utils_1.getCookieServerSide)(request.headers.cookie, legacyClientIdName);
34
+ if (legacyClientIdCookie) {
35
+ request.headers.cookie = (_a = request.headers.cookie) === null || _a === void 0 ? void 0 : _a.replace(legacyClientIdCookie.name, clientIdName);
36
+ response.setHeader('Set-Cookie', [
37
+ (0, utils_1.createCookieString)(clientIdName, legacyClientIdCookie.value, defaultCookieAttributes),
38
+ (0, utils_1.createCookieString)(legacyClientIdName, '', Object.assign(Object.assign({}, defaultCookieAttributes), { maxAge: 0 })),
39
+ ]);
40
+ return;
41
+ }
42
+ const clientIdCookie = getClientId(request);
43
+ let clientIdCookieValue;
44
+ if (!clientIdCookie) {
45
+ const cookieValues = await (0, internal_1.fetchClientIdFromEdgeProxy)(coreConfig.edgeUrl, coreConfig.contextId, analyticsOptions.timeout);
46
+ clientIdCookieValue = cookieValues.clientId;
47
+ analyticsOptions.visitorIds = cookieValues;
48
+ }
49
+ else
50
+ clientIdCookieValue = clientIdCookie;
51
+ const clientIdCookieString = (0, utils_1.createCookieString)(clientIdName, clientIdCookieValue, defaultCookieAttributes);
52
+ if (!clientIdCookie) {
53
+ request.headers.cookie = request.headers.cookie
54
+ ? request.headers.cookie + '; ' + clientIdCookieString
55
+ : clientIdCookieString;
56
+ }
57
+ let cookieHeader;
58
+ const currentSetCookieHeader = response.getHeader('Set-Cookie');
59
+ if (currentSetCookieHeader) {
60
+ if (Array.isArray(currentSetCookieHeader)) {
61
+ cookieHeader = [...currentSetCookieHeader, clientIdCookieString];
62
+ }
63
+ else {
64
+ cookieHeader = `${currentSetCookieHeader}; ${clientIdCookieString}`;
65
+ }
66
+ }
67
+ else {
68
+ cookieHeader = clientIdCookieString;
69
+ }
70
+ response.setHeader('Set-Cookie', cookieHeader);
71
+ },
72
+ location: {
73
+ getSearchParams: () => {
74
+ // Host is irrelevant but necessary to support relative URL
75
+ const requestUrl = new URL(request.url, `https://localhost`);
76
+ return requestUrl.search;
77
+ },
78
+ },
79
+ };
80
+ }
81
+ /**
82
+ * Retrieves the client ID from the request cookies.
83
+ * @template Request - The HTTP request type extending `IncomingMessage`.
84
+ * @param {Request} request
85
+ * @returns {string | null} The client ID or null if not found.
86
+ * @internal
87
+ */
88
+ function getClientId(request) {
89
+ var _a, _b;
90
+ return ((_b = (_a = (0, utils_1.getCookieServerSide)(request.headers.cookie, (0, plugin_1.getAnalyticsPlugin)().options.cookies.name)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null);
91
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ANALYTICS_PLUGIN_NAME = exports.getAnalyticsPlugin = exports.fetchClientIdFromEdgeProxy = exports.CLIENT_ID_COOKIE_NAME = exports.CORRELATION_ID_HEADER = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.COOKIE_NAME_PREFIX = exports.API_VERSION = exports.generateCorrelationId = exports.pageName = exports.language = exports.getDefaultCookieAttributes = void 0;
4
+ var get_default_cookie_attributes_1 = require("./cookie/get-default-cookie-attributes");
5
+ Object.defineProperty(exports, "getDefaultCookieAttributes", { enumerable: true, get: function () { return get_default_cookie_attributes_1.getDefaultCookieAttributes; } });
6
+ var infer_1 = require("./infer/infer");
7
+ Object.defineProperty(exports, "language", { enumerable: true, get: function () { return infer_1.language; } });
8
+ Object.defineProperty(exports, "pageName", { enumerable: true, get: function () { return infer_1.pageName; } });
9
+ var generate_correlation_id_1 = require("./correlation-id/generate-correlation-id");
10
+ Object.defineProperty(exports, "generateCorrelationId", { enumerable: true, get: function () { return generate_correlation_id_1.generateCorrelationId; } });
11
+ var consts_1 = require("./consts");
12
+ Object.defineProperty(exports, "API_VERSION", { enumerable: true, get: function () { return consts_1.API_VERSION; } });
13
+ Object.defineProperty(exports, "COOKIE_NAME_PREFIX", { enumerable: true, get: function () { return consts_1.COOKIE_NAME_PREFIX; } });
14
+ Object.defineProperty(exports, "DAILY_SECONDS", { enumerable: true, get: function () { return consts_1.DAILY_SECONDS; } });
15
+ Object.defineProperty(exports, "DEFAULT_COOKIE_EXPIRY_DAYS", { enumerable: true, get: function () { return consts_1.DEFAULT_COOKIE_EXPIRY_DAYS; } });
16
+ Object.defineProperty(exports, "LIBRARY_VERSION", { enumerable: true, get: function () { return consts_1.LIBRARY_VERSION; } });
17
+ Object.defineProperty(exports, "CORRELATION_ID_HEADER", { enumerable: true, get: function () { return consts_1.CORRELATION_ID_HEADER; } });
18
+ Object.defineProperty(exports, "CLIENT_ID_COOKIE_NAME", { enumerable: true, get: function () { return consts_1.CLIENT_ID_COOKIE_NAME; } });
19
+ var fetch_client_id_from_edge_proxy_1 = require("./client-id/fetch-client-id-from-edge-proxy");
20
+ Object.defineProperty(exports, "fetchClientIdFromEdgeProxy", { enumerable: true, get: function () { return fetch_client_id_from_edge_proxy_1.fetchClientIdFromEdgeProxy; } });
21
+ var plugin_1 = require("./initialization/plugin");
22
+ Object.defineProperty(exports, "getAnalyticsPlugin", { enumerable: true, get: function () { return plugin_1.getAnalyticsPlugin; } });
23
+ var const_1 = require("./initialization/const");
24
+ Object.defineProperty(exports, "ANALYTICS_PLUGIN_NAME", { enumerable: true, get: function () { return const_1.ANALYTICS_PLUGIN_NAME; } });
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appendScriptWithAttributes = appendScriptWithAttributes;
4
+ /**
5
+ * Appends a script element to the document head with the specified attributes.
6
+ * @param {ScriptAttributes} attributes - The attributes to set on the script element.
7
+ * @internal
8
+ */
9
+ function appendScriptWithAttributes(attributes) {
10
+ const sdkScriptElement = document.createElement('script');
11
+ sdkScriptElement.type = 'text/javascript';
12
+ sdkScriptElement.src = attributes.src;
13
+ sdkScriptElement.async = attributes.async;
14
+ document.head.appendChild(sdkScriptElement);
15
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToBase64 = convertToBase64;
4
+ /**
5
+ * Converts a string or plain object to a base64 string representation.
6
+ * @param {string | Record<string, unknown>} input The value to convert.
7
+ * @returns {string} The base64-encoded string.
8
+ * @internal
9
+ */
10
+ function convertToBase64(input) {
11
+ const data = typeof input === 'string' ? input : JSON.stringify(input);
12
+ const stringFormat = 'base64';
13
+ if (typeof Buffer === 'function')
14
+ return Buffer.from(data).toString(stringFormat);
15
+ if (typeof (window === null || window === void 0 ? void 0 : window.btoa) === 'function')
16
+ return window.btoa(data);
17
+ return data;
18
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flattenObject = flattenObject;
4
+ /**
5
+ * Flattens a nested object by concatenating keys with an underscore.
6
+ * @param {FlattenObjectDataParameters} data Parameters describing the flatten operation.
7
+ * @returns {FlattenedObject} A new flattened object.
8
+ * @example
9
+ * ```ts
10
+ * const object = { order: { amount: 1, delivered: false } };
11
+ * const flattenedObject = flattenObject(object);
12
+ * // flattenedObject is { order_amount: 1, order_delivered: false }
13
+ * ```
14
+ * @internal
15
+ */
16
+ function flattenObject(data) {
17
+ var _a;
18
+ const { currentKey, object } = data;
19
+ const newObject = (_a = data.newObject) !== null && _a !== void 0 ? _a : {};
20
+ // eslint-disable-next-line guard-for-in
21
+ for (const key in object) {
22
+ const value = object[key];
23
+ if (value === undefined)
24
+ continue;
25
+ if (typeof value === 'object' && !Array.isArray(value))
26
+ flattenObject({
27
+ currentKey: `${currentKey ? `${currentKey}_${key}` : key}`,
28
+ newObject,
29
+ object: value,
30
+ });
31
+ else
32
+ newObject[currentKey ? `${currentKey}_${key}` : key] = value;
33
+ }
34
+ return newObject;
35
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DAILY_SECONDS = void 0;
4
+ exports.DAILY_SECONDS = 86400;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cookieExistsInServerSide = cookieExistsInServerSide;
4
+ const get_cookie_server_side_1 = require("./get-cookie-server-side");
5
+ /**
6
+ * Checks whether a cookie exists in a server-side request header string.
7
+ * @param {string} cookiesHeader Raw `cookie` header contents.
8
+ * @param {string} cookieName The cookie name to search for.
9
+ * @returns {boolean} True when the cookie is present.
10
+ * @internal
11
+ */
12
+ function cookieExistsInServerSide(cookiesHeader, cookieName) {
13
+ return (0, get_cookie_server_side_1.getCookieServerSide)(cookiesHeader, cookieName) !== undefined;
14
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cookieExists = cookieExists;
4
+ /**
5
+ * Checks whether the cookie exists within a cookie string.
6
+ * @param {string} cookieStr Cookie string containing individual cookies.
7
+ * @param {string} cookieName The cookie name to search for.
8
+ * @returns {boolean} True when the cookie exists in the string.
9
+ * @internal
10
+ */
11
+ function cookieExists(cookieStr, cookieName) {
12
+ return cookieStr.split('; ').some((cookie) => cookie.split('=')[0] === cookieName);
13
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCookieString = createCookieString;
4
+ /**
5
+ * Creates a cookie string with the provided attributes.
6
+ * @param {string} name Cookie name.
7
+ * @param {string} value Cookie value.
8
+ * @param {CookieProperties} attributes Supported cookie attributes.
9
+ * @returns {string} Serialized cookie ready to be assigned to `document.cookie`.
10
+ * @internal
11
+ */
12
+ function createCookieString(name, value, attributes) {
13
+ let cookieString = `${name}=${value};`;
14
+ cookieString += ` Max-Age=${attributes.maxAge}; SameSite=${attributes.sameSite};`;
15
+ cookieString += attributes.secure ? ' Secure;' : '';
16
+ cookieString += attributes.path ? ` Path=${attributes.path};` : '';
17
+ cookieString += attributes.domain ? ` Domain=${attributes.domain};` : '';
18
+ cookieString = cookieString.substring(0, cookieString.length - 1);
19
+ return cookieString;
20
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteCookie = deleteCookie;
4
+ /**
5
+ * Deletes a cookie from the document (client-side only).
6
+ * @param {string} cookieName The cookie to be deleted.
7
+ * @internal
8
+ */
9
+ function deleteCookie(cookieName) {
10
+ document.cookie = cookieName + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
11
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookieServerSide = getCookieServerSide;
4
+ const get_cookie_1 = require("./get-cookie");
5
+ /**
6
+ * Retrieves a cookie from the server-side request header string.
7
+ * @param {string | undefined} cookiesHeader Raw `cookie` header contents.
8
+ * @param {string} cookieName The cookie name to look up.
9
+ * @returns {{ name: string; value: string } | undefined} The resolved cookie information when found.
10
+ * @internal
11
+ */
12
+ function getCookieServerSide(cookiesHeader, cookieName) {
13
+ return (0, get_cookie_1.getCookie)(cookiesHeader, cookieName);
14
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookieValueClientSide = getCookieValueClientSide;
4
+ const get_cookie_1 = require("./get-cookie");
5
+ /**
6
+ * Retrieves the value of a cookie in the browser context.
7
+ * @param {string} cookieName The name of the cookie to read.
8
+ * @returns {string} The cookie value or an empty string when not found.
9
+ * @internal
10
+ */
11
+ function getCookieValueClientSide(cookieName) {
12
+ var _a;
13
+ const cookie = (0, get_cookie_1.getCookie)(document.cookie, cookieName);
14
+ return (_a = cookie === null || cookie === void 0 ? void 0 : cookie.value) !== null && _a !== void 0 ? _a : '';
15
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookie = getCookie;
4
+ /**
5
+ * Retrieves a cookie by name from a cookie string.
6
+ * @param {string | undefined} cookieStr Cookie string containing serialized cookies.
7
+ * @param {string} cookieName The cookie name to locate.
8
+ * @returns {{ name: string; value: string } | undefined} The cookie name/value pair when found.
9
+ * @internal
10
+ */
11
+ function getCookie(cookieStr, cookieName) {
12
+ if (!cookieStr)
13
+ return undefined;
14
+ const found = cookieStr.split('; ').find((cookie) => {
15
+ return cookie.indexOf('=') > 0 && cookie.split('=')[0] === cookieName;
16
+ });
17
+ return found !== undefined
18
+ ? { name: found.split('=')[0], value: found.split('=')[1] }
19
+ : undefined;
20
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateV4UUID = generateV4UUID;
4
+ /**
5
+ * Generates a v4 UUID string using the global crypto API.
6
+ * @returns {string} A v4 UUID string.
7
+ * @internal
8
+ */
9
+ function generateV4UUID() {
10
+ return globalThis.crypto.randomUUID();
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isShortISODateString = isShortISODateString;
4
+ /**
5
+ * Checks if the provided string matches the shortened ISO 8601 format (`YYYY-MM-DDThh:mm`).
6
+ * @param {string} date The date string to validate.
7
+ * @returns {boolean} True when the value conforms to the shortened ISO format.
8
+ * @internal
9
+ */
10
+ function isShortISODateString(date) {
11
+ try {
12
+ const dateString = date + 'Z';
13
+ const convertedDate = new Date(dateString).toISOString().substring(0, 16);
14
+ return convertedDate === date;
15
+ // eslint-disable-next-line no-unused-vars
16
+ }
17
+ catch (error) {
18
+ return false;
19
+ }
20
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidEmail = isValidEmail;
4
+ /**
5
+ * Validates whether the provided value matches a basic email pattern.
6
+ * @param {string} email The email address to validate.
7
+ * @returns {boolean} True when the email matches the allowed pattern.
8
+ * @internal
9
+ */
10
+ function isValidEmail(email) {
11
+ const regx = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
12
+ if (!email || email.length > 320) {
13
+ return false;
14
+ }
15
+ return regx.test(email);
16
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appendScriptWithAttributes = exports.generateV4UUID = exports.isValidEmail = exports.isShortISODateString = exports.flattenObject = exports.cookieExists = exports.getCookieServerSide = exports.getCookie = exports.getCookieValueClientSide = exports.createCookieString = void 0;
4
+ var create_cookie_string_1 = require("./utils/cookies/create-cookie-string");
5
+ Object.defineProperty(exports, "createCookieString", { enumerable: true, get: function () { return create_cookie_string_1.createCookieString; } });
6
+ var get_cookie_value_client_side_1 = require("./utils/cookies/get-cookie-value-client-side");
7
+ Object.defineProperty(exports, "getCookieValueClientSide", { enumerable: true, get: function () { return get_cookie_value_client_side_1.getCookieValueClientSide; } });
8
+ var get_cookie_1 = require("./utils/cookies/get-cookie");
9
+ Object.defineProperty(exports, "getCookie", { enumerable: true, get: function () { return get_cookie_1.getCookie; } });
10
+ var get_cookie_server_side_1 = require("./utils/cookies/get-cookie-server-side");
11
+ Object.defineProperty(exports, "getCookieServerSide", { enumerable: true, get: function () { return get_cookie_server_side_1.getCookieServerSide; } });
12
+ var cookie_exists_1 = require("./utils/cookies/cookie-exists");
13
+ Object.defineProperty(exports, "cookieExists", { enumerable: true, get: function () { return cookie_exists_1.cookieExists; } });
14
+ var flatten_object_1 = require("./utils/converters/flatten-object");
15
+ Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return flatten_object_1.flattenObject; } });
16
+ var is_short_iso_date_string_1 = require("./utils/validators/is-short-iso-date-string");
17
+ Object.defineProperty(exports, "isShortISODateString", { enumerable: true, get: function () { return is_short_iso_date_string_1.isShortISODateString; } });
18
+ var is_valid_email_1 = require("./utils/validators/is-valid-email");
19
+ Object.defineProperty(exports, "isValidEmail", { enumerable: true, get: function () { return is_valid_email_1.isValidEmail; } });
20
+ var generate_v4_uuid_1 = require("./utils/generators/generate-v4-uuid");
21
+ Object.defineProperty(exports, "generateV4UUID", { enumerable: true, get: function () { return generate_v4_uuid_1.generateV4UUID; } });
22
+ var appendScriptWithAttributes_1 = require("./utils/browser/appendScriptWithAttributes");
23
+ Object.defineProperty(exports, "appendScriptWithAttributes", { enumerable: true, get: function () { return appendScriptWithAttributes_1.appendScriptWithAttributes; } });
@@ -0,0 +1,78 @@
1
+ {
2
+ "author": {
3
+ "name": "Sitecore Corporation",
4
+ "url": "https://doc.sitecore.com/xmc/en/developers/content-sdk/index.html"
5
+ },
6
+ "bugs": {
7
+ "url": "https://github.com/sitecore/content-sdk/issues"
8
+ },
9
+ "dependencies": {
10
+ "@sitecore-content-sdk/core": "2.0.0-canary.12",
11
+ "debug": "^4.4.3"
12
+ },
13
+ "description": "Provides shared logic and runtime initialization. Required for the Content SDK 'events' and 'personalize' packages to function.",
14
+ "devDependencies": {
15
+ "@jest/types": "^29.6.3",
16
+ "@stylistic/eslint-plugin": "^5.2.2",
17
+ "@types/debug": "^4.1.12",
18
+ "@types/jest": "^29.5.12",
19
+ "@typescript-eslint/eslint-plugin": "8.39.0",
20
+ "@typescript-eslint/parser": "8.39.0",
21
+ "del-cli": "^6.0.0",
22
+ "eslint": "^9.32.0",
23
+ "eslint-config-prettier": "^10.1.8",
24
+ "eslint-plugin-import": "2.32.0",
25
+ "eslint-plugin-jsdoc": "52.0.3",
26
+ "eslint-plugin-prettier": "^4.0.0",
27
+ "jest": "^29.7.0",
28
+ "jest-environment-jsdom": "^29.7.0",
29
+ "jest-environment-node": "^29.7.0",
30
+ "ts-jest": "^29.1.0",
31
+ "ts-node": "^10.9.2"
32
+ },
33
+ "main": "dist/cjs/index.js",
34
+ "module": "dist/esm/index.js",
35
+ "types": "types/src/index.d.ts",
36
+ "exports": {
37
+ ".": {
38
+ "import": "./dist/esm/src/index.js",
39
+ "require": "./dist/cjs/src/index.js",
40
+ "types": "./types/src/index.d.ts"
41
+ },
42
+ "./internal": {
43
+ "import": "./dist/esm/src/internal.js",
44
+ "require": "./dist/cjs/src/internal.js",
45
+ "types": "./types/src/internal.d.ts"
46
+ },
47
+ "./utils": {
48
+ "import": "./dist/esm/src/utils.js",
49
+ "require": "./dist/cjs/src/utils.js",
50
+ "types": "./types/src/utils.d.ts"
51
+ }
52
+ },
53
+ "files": [
54
+ "dist",
55
+ "types",
56
+ "/*.d.ts"
57
+ ],
58
+ "homepage": "https://doc.sitecore.com/xmc/en/developers/content-sdk/index.html",
59
+ "license": "Apache-2.0",
60
+ "name": "@sitecore-content-sdk/analytics-core",
61
+ "publishConfig": {
62
+ "access": "public",
63
+ "registry": "https://registry.npmjs.org/"
64
+ },
65
+ "scripts": {
66
+ "build": "npm run clean && tsc -p tsconfig.json && tsc -p tsconfig-esm.json",
67
+ "clean": "del-cli dist types",
68
+ "coverage": "jest --config jest.config.ts --coverage",
69
+ "generate-docs": "npx typedoc --plugin typedoc-plugin-markdown --outputFileStrategy Members --parametersFormat table --readme none --out ../../ref-docs/analytics-core --entryPoints src/index.ts --entryPoints src/internal.ts --entryPoints src/utils.ts --githubPages false",
70
+ "lint": "eslint \"./src/**/*.ts\"",
71
+ "prepublishOnly": "npm run build",
72
+ "test": "jest --config jest.config.ts",
73
+ "api-extractor": "npm run build && api-extractor run --local --verbose",
74
+ "api-extractor:verify": "api-extractor run"
75
+ },
76
+ "version": "2.0.0-canary.12",
77
+ "gitHead": "4a26005860f4931f25d8c63d765a030e04a5b1f4"
78
+ }
@@ -0,0 +1,38 @@
1
+ import { NativeDataFetcher, constants } from '@sitecore-content-sdk/core';
2
+ import { LIBRARY_VERSION } from '../consts';
3
+ import { resolveGetClientIdUrl } from './resolve-get-client-id-url';
4
+ const { ERROR_MESSAGES } = constants;
5
+ /**
6
+ * Gets the client ID and client key from Sitecore Edge proxy.
7
+ * @param {string} edgeUrl - The base URL for the Edge proxy API.
8
+ * @param {string} contextId - The Sitecore context ID parameter for the Edge proxy API.
9
+ * @param {number} [timeout] - The timeout in milliseconds for the call to the proxy.
10
+ * @returns {Promise<VisitorIds>} The client ID and profile ID from the proxy.
11
+ * @internal
12
+ */
13
+ export async function fetchClientIdFromEdgeProxy(edgeUrl, contextId, timeout) {
14
+ const fetchOptions = {
15
+ // eslint-disable-next-line @typescript-eslint/naming-convention
16
+ headers: {
17
+ 'X-Library-Version': LIBRARY_VERSION,
18
+ 'x-sitecore-contextid': contextId,
19
+ },
20
+ };
21
+ const url = resolveGetClientIdUrl(edgeUrl);
22
+ const fetcher = new NativeDataFetcher({ timeout });
23
+ const payload = await fetcher
24
+ .fetch(url, fetchOptions)
25
+ .then((response) => {
26
+ return response.data || null;
27
+ })
28
+ .catch((err) => {
29
+ if (err.message === ERROR_MESSAGES.IV_002 ||
30
+ err.message === ERROR_MESSAGES.IE_003)
31
+ throw new Error(err.message);
32
+ return null;
33
+ });
34
+ if (!(payload === null || payload === void 0 ? void 0 : payload.ref))
35
+ throw new Error(ERROR_MESSAGES.IE_005);
36
+ const { ref: clientId, customer_ref: profileId } = payload;
37
+ return { clientId, profileId };
38
+ }
@@ -0,0 +1,9 @@
1
+ import { getAnalyticsPlugin } from '../internal';
2
+ /**
3
+ * Gets the client ID from the cookie.
4
+ * @returns {string} The client ID if the cookie exists.
5
+ * @public
6
+ */
7
+ export function getClientId() {
8
+ return getAnalyticsPlugin().adapter.getClientId() || '';
9
+ }
@@ -0,0 +1,10 @@
1
+ import { API_VERSION } from '../consts';
2
+ /**
3
+ * Resolves the URL for retrieving the proxy settings from the EDGE events proxy.
4
+ * @param {string} edgeUrl - The base URL for the EDGE proxy.
5
+ * @returns {string} The URL string for retrieving the client ID and client key.
6
+ * @internal
7
+ */
8
+ export function resolveGetClientIdUrl(edgeUrl) {
9
+ return `${edgeUrl}/v1/events/${API_VERSION}/browser/create.json?client_key=`;
10
+ }