@blotoutio/providers-google-analytics-4-sdk 0.35.3 → 0.36.1

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.
package/index.cjs.js CHANGED
@@ -3,13 +3,45 @@
3
3
  const packageName = 'googleAnalytics4';
4
4
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
5
5
 
6
+ const upsert = (map, key, update, createDefault) => {
7
+ const currentValue = map.has(key)
8
+ ? map.get(key)
9
+ : createDefault();
10
+ return map.set(key, update(currentValue));
11
+ };
12
+
6
13
  // eslint-disable-next-line @nx/enforce-module-boundaries
7
- const initGA4 = (ID) => {
14
+ const initGA4 = (ID, executionContext) => {
15
+ var _a;
8
16
  window.dataLayer = window.dataLayer || [];
9
17
  window.gtag = function gtag() {
10
18
  // eslint-disable-next-line prefer-rest-params
11
19
  window.dataLayer.push(arguments);
12
20
  };
21
+ let isConsentInitialised = false;
22
+ for (const otherProvider of ['googleAdsClicks', 'googleAnalytics4']) {
23
+ if ((_a = executionContext.get(otherProvider)) === null || _a === void 0 ? void 0 : _a.get('gtag')) {
24
+ isConsentInitialised = true;
25
+ break;
26
+ }
27
+ }
28
+ if (isConsentInitialised) {
29
+ window.gtag('consent', 'update', {
30
+ analytics_storage: 'granted',
31
+ });
32
+ }
33
+ else {
34
+ window.gtag('consent', 'default', {
35
+ analytics_storage: 'granted',
36
+ functional_storage: 'granted',
37
+ personalization_storage: 'granted',
38
+ security_storage: 'granted',
39
+ ad_storage: 'denied',
40
+ ad_user_data: 'denied',
41
+ ad_personalization: 'denied',
42
+ });
43
+ }
44
+ upsert(executionContext, packageName, (providerMap) => providerMap.set('gtag', true), () => new Map());
13
45
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
46
  // @ts-ignore
15
47
  window.gtag('js', new Date());
@@ -21,14 +53,15 @@ const initGA4 = (ID) => {
21
53
  script.parentNode.insertBefore(element, script);
22
54
  }
23
55
  };
24
- const init = ({ manifest, userId }) => {
56
+ const init = ({ manifest, userId, executionContext }) => {
25
57
  if (!window ||
26
58
  !manifest.variables ||
27
59
  manifest.variables['flowType'] === 'server') {
28
60
  return;
29
61
  }
30
- if (!window.gtag) {
31
- initGA4(manifest.variables['measurementId']);
62
+ if (!window.google_tag_manager ||
63
+ !window.google_tag_manager[manifest.variables['tagId']]) {
64
+ initGA4(manifest.variables['measurementId'], executionContext);
32
65
  }
33
66
  if (window.gtag) {
34
67
  window.gtag('config', manifest.variables['measurementId'], {
@@ -268,15 +301,28 @@ const tag = ({ data, eventName, manifestVariables, eventId }) => {
268
301
  }
269
302
  return {
270
303
  loaded: isLoaded,
271
- sdkVersion: "0.35.3" ,
304
+ sdkVersion: "0.36.1" ,
272
305
  };
273
306
  };
274
307
 
308
+ const consent = ({ hasConsent }) => {
309
+ if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
310
+ return;
311
+ }
312
+ window.gtag('consent', 'update', {
313
+ analytics_storage: hasConsent ? 'granted' : 'denied',
314
+ functional_storage: 'granted',
315
+ personalization_storage: 'granted',
316
+ security_storage: 'granted',
317
+ });
318
+ };
319
+
275
320
  // eslint-disable-next-line @nx/enforce-module-boundaries
276
321
  const data = {
277
322
  name: packageName,
278
323
  init,
279
324
  tag,
325
+ consent,
280
326
  };
281
327
  try {
282
328
  if (window) {
package/index.js CHANGED
@@ -4,13 +4,45 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
4
4
  const packageName = 'googleAnalytics4';
5
5
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
6
6
 
7
+ const upsert = (map, key, update, createDefault) => {
8
+ const currentValue = map.has(key)
9
+ ? map.get(key)
10
+ : createDefault();
11
+ return map.set(key, update(currentValue));
12
+ };
13
+
7
14
  // eslint-disable-next-line @nx/enforce-module-boundaries
8
- const initGA4 = (ID) => {
15
+ const initGA4 = (ID, executionContext) => {
16
+ var _a;
9
17
  window.dataLayer = window.dataLayer || [];
10
18
  window.gtag = function gtag() {
11
19
  // eslint-disable-next-line prefer-rest-params
12
20
  window.dataLayer.push(arguments);
13
21
  };
22
+ let isConsentInitialised = false;
23
+ for (const otherProvider of ['googleAdsClicks', 'googleAnalytics4']) {
24
+ if ((_a = executionContext.get(otherProvider)) === null || _a === void 0 ? void 0 : _a.get('gtag')) {
25
+ isConsentInitialised = true;
26
+ break;
27
+ }
28
+ }
29
+ if (isConsentInitialised) {
30
+ window.gtag('consent', 'update', {
31
+ analytics_storage: 'granted',
32
+ });
33
+ }
34
+ else {
35
+ window.gtag('consent', 'default', {
36
+ analytics_storage: 'granted',
37
+ functional_storage: 'granted',
38
+ personalization_storage: 'granted',
39
+ security_storage: 'granted',
40
+ ad_storage: 'denied',
41
+ ad_user_data: 'denied',
42
+ ad_personalization: 'denied',
43
+ });
44
+ }
45
+ upsert(executionContext, packageName, (providerMap) => providerMap.set('gtag', true), () => new Map());
14
46
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
15
47
  // @ts-ignore
16
48
  window.gtag('js', new Date());
@@ -22,14 +54,15 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
22
54
  script.parentNode.insertBefore(element, script);
23
55
  }
24
56
  };
25
- const init = ({ manifest, userId }) => {
57
+ const init = ({ manifest, userId, executionContext }) => {
26
58
  if (!window ||
27
59
  !manifest.variables ||
28
60
  manifest.variables['flowType'] === 'server') {
29
61
  return;
30
62
  }
31
- if (!window.gtag) {
32
- initGA4(manifest.variables['measurementId']);
63
+ if (!window.google_tag_manager ||
64
+ !window.google_tag_manager[manifest.variables['tagId']]) {
65
+ initGA4(manifest.variables['measurementId'], executionContext);
33
66
  }
34
67
  if (window.gtag) {
35
68
  window.gtag('config', manifest.variables['measurementId'], {
@@ -269,15 +302,28 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
269
302
  }
270
303
  return {
271
304
  loaded: isLoaded,
272
- sdkVersion: "0.35.3" ,
305
+ sdkVersion: "0.36.1" ,
273
306
  };
274
307
  };
275
308
 
309
+ const consent = ({ hasConsent }) => {
310
+ if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
311
+ return;
312
+ }
313
+ window.gtag('consent', 'update', {
314
+ analytics_storage: hasConsent ? 'granted' : 'denied',
315
+ functional_storage: 'granted',
316
+ personalization_storage: 'granted',
317
+ security_storage: 'granted',
318
+ });
319
+ };
320
+
276
321
  // eslint-disable-next-line @nx/enforce-module-boundaries
277
322
  const data = {
278
323
  name: packageName,
279
324
  init,
280
325
  tag,
326
+ consent,
281
327
  };
282
328
  try {
283
329
  if (window) {
package/index.mjs CHANGED
@@ -1,13 +1,45 @@
1
1
  const packageName = 'googleAnalytics4';
2
2
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
3
3
 
4
+ const upsert = (map, key, update, createDefault) => {
5
+ const currentValue = map.has(key)
6
+ ? map.get(key)
7
+ : createDefault();
8
+ return map.set(key, update(currentValue));
9
+ };
10
+
4
11
  // eslint-disable-next-line @nx/enforce-module-boundaries
5
- const initGA4 = (ID) => {
12
+ const initGA4 = (ID, executionContext) => {
13
+ var _a;
6
14
  window.dataLayer = window.dataLayer || [];
7
15
  window.gtag = function gtag() {
8
16
  // eslint-disable-next-line prefer-rest-params
9
17
  window.dataLayer.push(arguments);
10
18
  };
19
+ let isConsentInitialised = false;
20
+ for (const otherProvider of ['googleAdsClicks', 'googleAnalytics4']) {
21
+ if ((_a = executionContext.get(otherProvider)) === null || _a === void 0 ? void 0 : _a.get('gtag')) {
22
+ isConsentInitialised = true;
23
+ break;
24
+ }
25
+ }
26
+ if (isConsentInitialised) {
27
+ window.gtag('consent', 'update', {
28
+ analytics_storage: 'granted',
29
+ });
30
+ }
31
+ else {
32
+ window.gtag('consent', 'default', {
33
+ analytics_storage: 'granted',
34
+ functional_storage: 'granted',
35
+ personalization_storage: 'granted',
36
+ security_storage: 'granted',
37
+ ad_storage: 'denied',
38
+ ad_user_data: 'denied',
39
+ ad_personalization: 'denied',
40
+ });
41
+ }
42
+ upsert(executionContext, packageName, (providerMap) => providerMap.set('gtag', true), () => new Map());
11
43
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
12
44
  // @ts-ignore
13
45
  window.gtag('js', new Date());
@@ -19,14 +51,15 @@ const initGA4 = (ID) => {
19
51
  script.parentNode.insertBefore(element, script);
20
52
  }
21
53
  };
22
- const init = ({ manifest, userId }) => {
54
+ const init = ({ manifest, userId, executionContext }) => {
23
55
  if (!window ||
24
56
  !manifest.variables ||
25
57
  manifest.variables['flowType'] === 'server') {
26
58
  return;
27
59
  }
28
- if (!window.gtag) {
29
- initGA4(manifest.variables['measurementId']);
60
+ if (!window.google_tag_manager ||
61
+ !window.google_tag_manager[manifest.variables['tagId']]) {
62
+ initGA4(manifest.variables['measurementId'], executionContext);
30
63
  }
31
64
  if (window.gtag) {
32
65
  window.gtag('config', manifest.variables['measurementId'], {
@@ -266,15 +299,28 @@ const tag = ({ data, eventName, manifestVariables, eventId }) => {
266
299
  }
267
300
  return {
268
301
  loaded: isLoaded,
269
- sdkVersion: "0.35.3" ,
302
+ sdkVersion: "0.36.1" ,
270
303
  };
271
304
  };
272
305
 
306
+ const consent = ({ hasConsent }) => {
307
+ if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
308
+ return;
309
+ }
310
+ window.gtag('consent', 'update', {
311
+ analytics_storage: hasConsent ? 'granted' : 'denied',
312
+ functional_storage: 'granted',
313
+ personalization_storage: 'granted',
314
+ security_storage: 'granted',
315
+ });
316
+ };
317
+
273
318
  // eslint-disable-next-line @nx/enforce-module-boundaries
274
319
  const data = {
275
320
  name: packageName,
276
321
  init,
277
322
  tag,
323
+ consent,
278
324
  };
279
325
  try {
280
326
  if (window) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/providers-google-analytics-4-sdk",
3
- "version": "0.35.3",
3
+ "version": "0.36.1",
4
4
  "description": "Google Analytics 4 Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",