@chordcommerce/analytics 1.21.2 → 1.21.3

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.
@@ -45,7 +45,7 @@ var utils_js_1 = require("./utils.js");
45
45
  * Returns initial consent configuration to be tracked after CDP is fully ready.
46
46
  */
47
47
  var addCdpSnippet = function (options) { return __awaiter(void 0, void 0, void 0, function () {
48
- var normalizedCDPDomain, cdpReadyPromise, s, initialConsent;
48
+ var normalizedCDPDomain, cdpReadyPromise, escapedNamespace, existingScript, s, initialConsent;
49
49
  return __generator(this, function (_a) {
50
50
  switch (_a.label) {
51
51
  case 0:
@@ -65,14 +65,20 @@ var addCdpSnippet = function (options) { return __awaiter(void 0, void 0, void 0
65
65
  };
66
66
  checkCdpReady();
67
67
  });
68
- s = document.createElement('script');
69
- s.async = true;
70
- s.src = "".concat(normalizedCDPDomain, "/p.js");
71
- s.setAttribute('data-debug', "".concat(!!options.debug));
72
- s.setAttribute('data-init-only', 'true');
73
- s.setAttribute('data-namespace', "_".concat(options.namespace));
74
- s.setAttribute('data-write-key', options.cdpWriteKey);
75
- document.head.appendChild(s);
68
+ escapedNamespace = typeof CSS !== 'undefined' && typeof CSS.escape === 'function'
69
+ ? CSS.escape("_".concat(options.namespace))
70
+ : "_".concat(options.namespace);
71
+ existingScript = document.querySelector("script[data-namespace=\"".concat(escapedNamespace, "\"]"));
72
+ if (!existingScript) {
73
+ s = document.createElement('script');
74
+ s.async = true;
75
+ s.src = "".concat(normalizedCDPDomain, "/p.js");
76
+ s.setAttribute('data-debug', "".concat(!!options.debug));
77
+ s.setAttribute('data-init-only', 'true');
78
+ s.setAttribute('data-namespace', "_".concat(options.namespace));
79
+ s.setAttribute('data-write-key', options.cdpWriteKey);
80
+ document.head.appendChild(s);
81
+ }
76
82
  // Wait for CDP to be ready before setting up consent
77
83
  return [4 /*yield*/, cdpReadyPromise];
78
84
  case 1:
@@ -32,14 +32,21 @@ export const addCdpSnippet = (options) => __awaiter(void 0, void 0, void 0, func
32
32
  };
33
33
  checkCdpReady();
34
34
  });
35
- const s = document.createElement('script');
36
- s.async = true;
37
- s.src = `${normalizedCDPDomain}/p.js`;
38
- s.setAttribute('data-debug', `${!!options.debug}`);
39
- s.setAttribute('data-init-only', 'true');
40
- s.setAttribute('data-namespace', `_${options.namespace}`);
41
- s.setAttribute('data-write-key', options.cdpWriteKey);
42
- document.head.appendChild(s);
35
+ // Guard against duplicate script injection (e.g. React StrictMode remounts)
36
+ const escapedNamespace = typeof CSS !== 'undefined' && typeof CSS.escape === 'function'
37
+ ? CSS.escape(`_${options.namespace}`)
38
+ : `_${options.namespace}`;
39
+ const existingScript = document.querySelector(`script[data-namespace="${escapedNamespace}"]`);
40
+ if (!existingScript) {
41
+ const s = document.createElement('script');
42
+ s.async = true;
43
+ s.src = `${normalizedCDPDomain}/p.js`;
44
+ s.setAttribute('data-debug', `${!!options.debug}`);
45
+ s.setAttribute('data-init-only', 'true');
46
+ s.setAttribute('data-namespace', `_${options.namespace}`);
47
+ s.setAttribute('data-write-key', options.cdpWriteKey);
48
+ document.head.appendChild(s);
49
+ }
43
50
  // Wait for CDP to be ready before setting up consent
44
51
  yield cdpReadyPromise;
45
52
  let initialConsent = null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chordcommerce/analytics",
3
3
  "description": "Chord Commerce event tracking",
4
- "version": "1.21.2",
4
+ "version": "1.21.3",
5
5
  "author": "Chord Commerce <help@chord.co> (https://www.chord.co)",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",