@blotoutio/providers-google-analytics-4-sdk 0.71.0 → 1.0.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 (4) hide show
  1. package/index.cjs.js +331 -10
  2. package/index.js +331 -10
  3. package/index.mjs +331 -10
  4. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -3,6 +3,40 @@
3
3
  const packageName = 'googleAnalytics4';
4
4
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
5
5
 
6
+ const isBool = (v) => typeof v == 'boolean';
7
+ const isRecord = (v) => !!v && typeof v == 'object' && !Array.isArray(v);
8
+ /**
9
+ * This function validates user consent for a given provider type, not based on tagName.
10
+ */
11
+ const hasUserConsentForProvider = (consent, provider) => {
12
+ if (!isRecord(consent)) {
13
+ return false;
14
+ }
15
+ let allowed = isBool(consent.all) ? consent.all : false;
16
+ if (provider in consent) {
17
+ const providerSpecific = consent[provider];
18
+ if (isBool(providerSpecific)) {
19
+ allowed = providerSpecific;
20
+ }
21
+ else if (isRecord(providerSpecific)) {
22
+ return Object.keys(providerSpecific).some((instance) => providerSpecific[instance] === true);
23
+ }
24
+ }
25
+ return allowed;
26
+ };
27
+ const isCategoryConsented = (consentCategories, category) => {
28
+ if (!consentCategories)
29
+ return false;
30
+ let allowed = isBool(consentCategories.all) ? consentCategories.all : false;
31
+ if (category in consentCategories) {
32
+ const categorySpecific = consentCategories[category];
33
+ if (isBool(categorySpecific)) {
34
+ allowed = categorySpecific;
35
+ }
36
+ }
37
+ return allowed;
38
+ };
39
+
6
40
  const upsert = (map, key, update, createDefault) => {
7
41
  const currentValue = map.has(key)
8
42
  ? map.get(key)
@@ -44,8 +78,290 @@ new Set([
44
78
  ...expand('911,921,922,923,924,926,927,932,933,935,942,944,946,950,953,955,957-958,960-969,974,975,976,977,981-982,987,988,990-999'),
45
79
  ]);
46
80
 
81
+ /**
82
+ * ISO-3166 2-leter country codes and their names
83
+ * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
84
+ */
85
+ const isoCountries = new Map([
86
+ ['AD', 'Andorra'],
87
+ ['AE', 'United Arab Emirates'],
88
+ ['AF', 'Afghanistan'],
89
+ ['AG', 'Antigua and Barbuda'],
90
+ ['AI', 'Anguilla'],
91
+ ['AL', 'Albania'],
92
+ ['AM', 'Armenia'],
93
+ ['AO', 'Angola'],
94
+ ['AQ', 'Antarctica'],
95
+ ['AR', 'Argentina'],
96
+ ['AS', 'American Samoa'],
97
+ ['AT', 'Austria'],
98
+ ['AU', 'Australia'],
99
+ ['AW', 'Aruba'],
100
+ ['AX', 'Åland Islands'],
101
+ ['AZ', 'Azerbaijan'],
102
+ ['BA', 'Bosnia and Herzegovina'],
103
+ ['BB', 'Barbados'],
104
+ ['BD', 'Bangladesh'],
105
+ ['BE', 'Belgium'],
106
+ ['BF', 'Burkina Faso'],
107
+ ['BG', 'Bulgaria'],
108
+ ['BH', 'Bahrain'],
109
+ ['BI', 'Burundi'],
110
+ ['BJ', 'Benin'],
111
+ ['BL', 'Saint Barthélemy'],
112
+ ['BM', 'Bermuda'],
113
+ ['BN', 'Brunei Darussalam'],
114
+ ['BO', 'Bolivia, Plurinational State of'],
115
+ ['BQ', 'Bonaire, Sint Eustatius and Saba'],
116
+ ['BR', 'Brazil'],
117
+ ['BS', 'Bahamas'],
118
+ ['BT', 'Bhutan'],
119
+ ['BV', 'Bouvet Island'],
120
+ ['BW', 'Botswana'],
121
+ ['BY', 'Belarus'],
122
+ ['BZ', 'Belize'],
123
+ ['CA', 'Canada'],
124
+ ['CC', 'Cocos (Keeling) Islands'],
125
+ ['CD', 'Congo, Democratic Republic of the'],
126
+ ['CF', 'Central African Republic'],
127
+ ['CG', 'Congo'],
128
+ ['CH', 'Switzerland'],
129
+ ['CI', "Côte d'Ivoire"],
130
+ ['CK', 'Cook Islands'],
131
+ ['CL', 'Chile'],
132
+ ['CM', 'Cameroon'],
133
+ ['CN', 'China'],
134
+ ['CO', 'Colombia'],
135
+ ['CR', 'Costa Rica'],
136
+ ['CU', 'Cuba'],
137
+ ['CV', 'Cabo Verde'],
138
+ ['CW', 'Curaçao'],
139
+ ['CX', 'Christmas Island'],
140
+ ['CY', 'Cyprus'],
141
+ ['CZ', 'Czechia'],
142
+ ['DE', 'Germany'],
143
+ ['DJ', 'Djibouti'],
144
+ ['DK', 'Denmark'],
145
+ ['DM', 'Dominica'],
146
+ ['DO', 'Dominican Republic'],
147
+ ['DZ', 'Algeria'],
148
+ ['EC', 'Ecuador'],
149
+ ['EE', 'Estonia'],
150
+ ['EG', 'Egypt'],
151
+ ['EH', 'Western Sahara'],
152
+ ['ER', 'Eritrea'],
153
+ ['ES', 'Spain'],
154
+ ['ET', 'Ethiopia'],
155
+ ['EU', 'European Union'],
156
+ ['FI', 'Finland'],
157
+ ['FJ', 'Fiji'],
158
+ ['FK', 'Falkland Islands (Malvinas)'],
159
+ ['FM', 'Micronesia, Federated States of'],
160
+ ['FO', 'Faroe Islands'],
161
+ ['FR', 'France'],
162
+ ['GA', 'Gabon'],
163
+ ['GB', 'United Kingdom of Great Britain and Northern Ireland'],
164
+ ['GD', 'Grenada'],
165
+ ['GE', 'Georgia'],
166
+ ['GF', 'French Guiana'],
167
+ ['GG', 'Guernsey'],
168
+ ['GH', 'Ghana'],
169
+ ['GI', 'Gibraltar'],
170
+ ['GL', 'Greenland'],
171
+ ['GM', 'Gambia'],
172
+ ['GN', 'Guinea'],
173
+ ['GP', 'Guadeloupe'],
174
+ ['GQ', 'Equatorial Guinea'],
175
+ ['GR', 'Greece'],
176
+ ['GS', 'South Georgia and the South Sandwich Islands'],
177
+ ['GT', 'Guatemala'],
178
+ ['GU', 'Guam'],
179
+ ['GW', 'Guinea-Bissau'],
180
+ ['GY', 'Guyana'],
181
+ ['HK', 'Hong Kong'],
182
+ ['HM', 'Heard Island and McDonald Islands'],
183
+ ['HN', 'Honduras'],
184
+ ['HR', 'Croatia'],
185
+ ['HT', 'Haiti'],
186
+ ['HU', 'Hungary'],
187
+ ['ID', 'Indonesia'],
188
+ ['IE', 'Ireland'],
189
+ ['IL', 'Israel'],
190
+ ['IM', 'Isle of Man'],
191
+ ['IN', 'India'],
192
+ ['IO', 'British Indian Ocean Territory'],
193
+ ['IQ', 'Iraq'],
194
+ ['IR', 'Iran, Islamic Republic of'],
195
+ ['IS', 'Iceland'],
196
+ ['IT', 'Italy'],
197
+ ['JE', 'Jersey'],
198
+ ['JM', 'Jamaica'],
199
+ ['JO', 'Jordan'],
200
+ ['JP', 'Japan'],
201
+ ['KE', 'Kenya'],
202
+ ['KG', 'Kyrgyzstan'],
203
+ ['KH', 'Cambodia'],
204
+ ['KI', 'Kiribati'],
205
+ ['KM', 'Comoros'],
206
+ ['KN', 'Saint Kitts and Nevis'],
207
+ ['KP', "Korea, Democratic People's Republic of"],
208
+ ['KR', 'Korea, Republic of'],
209
+ ['KW', 'Kuwait'],
210
+ ['KY', 'Cayman Islands'],
211
+ ['KZ', 'Kazakhstan'],
212
+ ['LA', "Lao People's Democratic Republic"],
213
+ ['LB', 'Lebanon'],
214
+ ['LC', 'Saint Lucia'],
215
+ ['LI', 'Liechtenstein'],
216
+ ['LK', 'Sri Lanka'],
217
+ ['LR', 'Liberia'],
218
+ ['LS', 'Lesotho'],
219
+ ['LT', 'Lithuania'],
220
+ ['LU', 'Luxembourg'],
221
+ ['LV', 'Latvia'],
222
+ ['LY', 'Libya'],
223
+ ['MA', 'Morocco'],
224
+ ['MC', 'Monaco'],
225
+ ['MD', 'Moldova, Republic of'],
226
+ ['ME', 'Montenegro'],
227
+ ['MF', 'Saint Martin (French part)'],
228
+ ['MG', 'Madagascar'],
229
+ ['MH', 'Marshall Islands'],
230
+ ['MK', 'North Macedonia'],
231
+ ['ML', 'Mali'],
232
+ ['MM', 'Myanmar'],
233
+ ['MN', 'Mongolia'],
234
+ ['MO', 'Macao'],
235
+ ['MP', 'Northern Mariana Islands'],
236
+ ['MQ', 'Martinique'],
237
+ ['MR', 'Mauritania'],
238
+ ['MS', 'Montserrat'],
239
+ ['MT', 'Malta'],
240
+ ['MU', 'Mauritius'],
241
+ ['MV', 'Maldives'],
242
+ ['MW', 'Malawi'],
243
+ ['MX', 'Mexico'],
244
+ ['MY', 'Malaysia'],
245
+ ['MZ', 'Mozambique'],
246
+ ['NA', 'Namibia'],
247
+ ['NC', 'New Caledonia'],
248
+ ['NE', 'Niger'],
249
+ ['NF', 'Norfolk Island'],
250
+ ['NG', 'Nigeria'],
251
+ ['NI', 'Nicaragua'],
252
+ ['NL', 'Netherlands, Kingdom of the'],
253
+ ['NO', 'Norway'],
254
+ ['NP', 'Nepal'],
255
+ ['NR', 'Nauru'],
256
+ ['NU', 'Niue'],
257
+ ['NZ', 'New Zealand'],
258
+ ['OM', 'Oman'],
259
+ ['PA', 'Panama'],
260
+ ['PE', 'Peru'],
261
+ ['PF', 'French Polynesia'],
262
+ ['PG', 'Papua New Guinea'],
263
+ ['PH', 'Philippines'],
264
+ ['PK', 'Pakistan'],
265
+ ['PL', 'Poland'],
266
+ ['PM', 'Saint Pierre and Miquelon'],
267
+ ['PN', 'Pitcairn'],
268
+ ['PR', 'Puerto Rico'],
269
+ ['PS', 'Palestine, State of'],
270
+ ['PT', 'Portugal'],
271
+ ['PW', 'Palau'],
272
+ ['PY', 'Paraguay'],
273
+ ['QA', 'Qatar'],
274
+ ['RE', 'Réunion'],
275
+ ['RO', 'Romania'],
276
+ ['RS', 'Serbia'],
277
+ ['RU', 'Russian Federation'],
278
+ ['RW', 'Rwanda'],
279
+ ['SA', 'Saudi Arabia'],
280
+ ['SB', 'Solomon Islands'],
281
+ ['SC', 'Seychelles'],
282
+ ['SD', 'Sudan'],
283
+ ['SE', 'Sweden'],
284
+ ['SG', 'Singapore'],
285
+ ['SH', 'Saint Helena, Ascension and Tristan da Cunha'],
286
+ ['SI', 'Slovenia'],
287
+ ['SJ', 'Svalbard and Jan Mayen'],
288
+ ['SK', 'Slovakia'],
289
+ ['SL', 'Sierra Leone'],
290
+ ['SM', 'San Marino'],
291
+ ['SN', 'Senegal'],
292
+ ['SO', 'Somalia'],
293
+ ['SR', 'Suriname'],
294
+ ['SS', 'South Sudan'],
295
+ ['ST', 'Sao Tome and Principe'],
296
+ ['SV', 'El Salvador'],
297
+ ['SX', 'Sint Maarten (Dutch part)'],
298
+ ['SY', 'Syrian Arab Republic'],
299
+ ['SZ', 'Eswatini'],
300
+ ['TC', 'Turks and Caicos Islands'],
301
+ ['TD', 'Chad'],
302
+ ['TF', 'French Southern Territories'],
303
+ ['TG', 'Togo'],
304
+ ['TH', 'Thailand'],
305
+ ['TJ', 'Tajikistan'],
306
+ ['TK', 'Tokelau'],
307
+ ['TL', 'Timor-Leste'],
308
+ ['TM', 'Turkmenistan'],
309
+ ['TN', 'Tunisia'],
310
+ ['TO', 'Tonga'],
311
+ ['TR', 'Türkiye'],
312
+ ['TT', 'Trinidad and Tobago'],
313
+ ['TV', 'Tuvalu'],
314
+ ['TW', 'Taiwan, Province of China'],
315
+ ['TZ', 'Tanzania, United Republic of'],
316
+ ['UA', 'Ukraine'],
317
+ ['UG', 'Uganda'],
318
+ ['UK', 'United Kingdom'],
319
+ ['UM', 'United States Minor Outlying Islands'],
320
+ ['US', 'United States of America'],
321
+ ['UY', 'Uruguay'],
322
+ ['UZ', 'Uzbekistan'],
323
+ ['VA', 'Holy See'],
324
+ ['VC', 'Saint Vincent and the Grenadines'],
325
+ ['VE', 'Venezuela, Bolivarian Republic of'],
326
+ ['VG', 'Virgin Islands (British)'],
327
+ ['VI', 'Virgin Islands (U.S.)'],
328
+ ['VN', 'Viet Nam'],
329
+ ['VU', 'Vanuatu'],
330
+ ['WF', 'Wallis and Futuna'],
331
+ ['WS', 'Samoa'],
332
+ ['YE', 'Yemen'],
333
+ ['YT', 'Mayotte'],
334
+ ['ZA', 'South Africa'],
335
+ ['ZM', 'Zambia'],
336
+ ['ZW', 'Zimbabwe'],
337
+ ]);
338
+ new Set(isoCountries.keys());
339
+
340
+ // eslint-disable-next-line @nx/enforce-module-boundaries
341
+ const getGoogleConsentFromCategories = (categories) => {
342
+ const advertisingConsent = isCategoryConsented(categories, 'advertising');
343
+ const analyticsConsent = isCategoryConsented(categories, 'analytics');
344
+ return {
345
+ analytics_storage: analyticsConsent ? 'granted' : 'denied',
346
+ ad_storage: advertisingConsent ? 'granted' : 'denied',
347
+ ad_user_data: advertisingConsent ? 'granted' : 'denied',
348
+ ad_personalization: advertisingConsent ? 'granted' : 'denied',
349
+ };
350
+ };
351
+ // TODO: once categories becomes a stable field, we need to remove this and start using categories data for google consent mode.
352
+ const getGoogleConsentFromChannels = (consent) => {
353
+ const gadsConsent = hasUserConsentForProvider(consent, 'googleAdsClicks');
354
+ const ga4Consent = hasUserConsentForProvider(consent, 'googleAnalytics4');
355
+ return {
356
+ analytics_storage: ga4Consent ? 'granted' : 'denied',
357
+ ad_storage: gadsConsent ? 'granted' : 'denied',
358
+ ad_user_data: gadsConsent ? 'granted' : 'denied',
359
+ ad_personalization: gadsConsent ? 'granted' : 'denied',
360
+ };
361
+ };
362
+
47
363
  // eslint-disable-next-line @nx/enforce-module-boundaries
48
- const initGA4 = (ID, executionContext) => {
364
+ const initGA4 = (ID, advancedConsentMode, consentData, executionContext) => {
49
365
  var _a;
50
366
  window.dataLayer = window.dataLayer || [];
51
367
  window.gtag = function gtag() {
@@ -59,14 +375,15 @@ const initGA4 = (ID, executionContext) => {
59
375
  break;
60
376
  }
61
377
  }
378
+ const data = advancedConsentMode === '1'
379
+ ? getGoogleConsentFromCategories(consentData.categories)
380
+ : getGoogleConsentFromChannels(consentData.consent);
62
381
  if (isConsentInitialised) {
63
- window.gtag('consent', 'update', {
64
- analytics_storage: 'granted',
65
- });
382
+ window.gtag('consent', 'update', data);
66
383
  }
67
384
  else {
68
385
  window.gtag('consent', 'default', {
69
- analytics_storage: 'granted',
386
+ ...data,
70
387
  functional_storage: 'granted',
71
388
  personalization_storage: 'granted',
72
389
  security_storage: 'granted',
@@ -84,7 +401,7 @@ const initGA4 = (ID, executionContext) => {
84
401
  script.parentNode.insertBefore(element, script);
85
402
  }
86
403
  };
87
- const init = ({ manifest, userId, executionContext }) => {
404
+ const init = ({ manifest, userId, executionContext, consentData, }) => {
88
405
  if (!window ||
89
406
  !manifest.variables ||
90
407
  !manifest.variables['measurementId'] ||
@@ -93,7 +410,7 @@ const init = ({ manifest, userId, executionContext }) => {
93
410
  }
94
411
  if (!window.google_tag_manager ||
95
412
  !window.google_tag_manager[manifest.variables['measurementId']]) {
96
- initGA4(manifest.variables['measurementId'], executionContext);
413
+ initGA4(manifest.variables['measurementId'], manifest.variables['advancedConsentMode'], consentData, executionContext);
97
414
  }
98
415
  if (window.gtag) {
99
416
  window.gtag('config', manifest.variables['measurementId'], {
@@ -354,16 +671,20 @@ const tag = ({ data, eventName, manifestVariables, eventId }) => {
354
671
  }
355
672
  return {
356
673
  loaded: isLoaded,
357
- sdkVersion: "0.71.0" ,
674
+ sdkVersion: "1.0.0" ,
358
675
  };
359
676
  };
360
677
 
361
- const consent = ({ hasConsent }) => {
678
+ const consent = ({ consentData, variables }) => {
362
679
  if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
363
680
  return;
364
681
  }
682
+ const isAdvancedConsentModeEnabled = variables.some((variable) => { var _a; return ((_a = variable.variableSet) === null || _a === void 0 ? void 0 : _a['advancedConsentMode']) === '1'; });
683
+ const data = isAdvancedConsentModeEnabled
684
+ ? getGoogleConsentFromCategories(consentData.categories)
685
+ : getGoogleConsentFromChannels(consentData.consent);
365
686
  window.gtag('consent', 'update', {
366
- analytics_storage: hasConsent ? 'granted' : 'denied',
687
+ ...data,
367
688
  functional_storage: 'granted',
368
689
  personalization_storage: 'granted',
369
690
  security_storage: 'granted',
package/index.js CHANGED
@@ -4,6 +4,40 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
4
4
  const packageName = 'googleAnalytics4';
5
5
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
6
6
 
7
+ const isBool = (v) => typeof v == 'boolean';
8
+ const isRecord = (v) => !!v && typeof v == 'object' && !Array.isArray(v);
9
+ /**
10
+ * This function validates user consent for a given provider type, not based on tagName.
11
+ */
12
+ const hasUserConsentForProvider = (consent, provider) => {
13
+ if (!isRecord(consent)) {
14
+ return false;
15
+ }
16
+ let allowed = isBool(consent.all) ? consent.all : false;
17
+ if (provider in consent) {
18
+ const providerSpecific = consent[provider];
19
+ if (isBool(providerSpecific)) {
20
+ allowed = providerSpecific;
21
+ }
22
+ else if (isRecord(providerSpecific)) {
23
+ return Object.keys(providerSpecific).some((instance) => providerSpecific[instance] === true);
24
+ }
25
+ }
26
+ return allowed;
27
+ };
28
+ const isCategoryConsented = (consentCategories, category) => {
29
+ if (!consentCategories)
30
+ return false;
31
+ let allowed = isBool(consentCategories.all) ? consentCategories.all : false;
32
+ if (category in consentCategories) {
33
+ const categorySpecific = consentCategories[category];
34
+ if (isBool(categorySpecific)) {
35
+ allowed = categorySpecific;
36
+ }
37
+ }
38
+ return allowed;
39
+ };
40
+
7
41
  const upsert = (map, key, update, createDefault) => {
8
42
  const currentValue = map.has(key)
9
43
  ? map.get(key)
@@ -45,8 +79,290 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
45
79
  ...expand('911,921,922,923,924,926,927,932,933,935,942,944,946,950,953,955,957-958,960-969,974,975,976,977,981-982,987,988,990-999'),
46
80
  ]);
47
81
 
82
+ /**
83
+ * ISO-3166 2-leter country codes and their names
84
+ * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
85
+ */
86
+ const isoCountries = new Map([
87
+ ['AD', 'Andorra'],
88
+ ['AE', 'United Arab Emirates'],
89
+ ['AF', 'Afghanistan'],
90
+ ['AG', 'Antigua and Barbuda'],
91
+ ['AI', 'Anguilla'],
92
+ ['AL', 'Albania'],
93
+ ['AM', 'Armenia'],
94
+ ['AO', 'Angola'],
95
+ ['AQ', 'Antarctica'],
96
+ ['AR', 'Argentina'],
97
+ ['AS', 'American Samoa'],
98
+ ['AT', 'Austria'],
99
+ ['AU', 'Australia'],
100
+ ['AW', 'Aruba'],
101
+ ['AX', 'Åland Islands'],
102
+ ['AZ', 'Azerbaijan'],
103
+ ['BA', 'Bosnia and Herzegovina'],
104
+ ['BB', 'Barbados'],
105
+ ['BD', 'Bangladesh'],
106
+ ['BE', 'Belgium'],
107
+ ['BF', 'Burkina Faso'],
108
+ ['BG', 'Bulgaria'],
109
+ ['BH', 'Bahrain'],
110
+ ['BI', 'Burundi'],
111
+ ['BJ', 'Benin'],
112
+ ['BL', 'Saint Barthélemy'],
113
+ ['BM', 'Bermuda'],
114
+ ['BN', 'Brunei Darussalam'],
115
+ ['BO', 'Bolivia, Plurinational State of'],
116
+ ['BQ', 'Bonaire, Sint Eustatius and Saba'],
117
+ ['BR', 'Brazil'],
118
+ ['BS', 'Bahamas'],
119
+ ['BT', 'Bhutan'],
120
+ ['BV', 'Bouvet Island'],
121
+ ['BW', 'Botswana'],
122
+ ['BY', 'Belarus'],
123
+ ['BZ', 'Belize'],
124
+ ['CA', 'Canada'],
125
+ ['CC', 'Cocos (Keeling) Islands'],
126
+ ['CD', 'Congo, Democratic Republic of the'],
127
+ ['CF', 'Central African Republic'],
128
+ ['CG', 'Congo'],
129
+ ['CH', 'Switzerland'],
130
+ ['CI', "Côte d'Ivoire"],
131
+ ['CK', 'Cook Islands'],
132
+ ['CL', 'Chile'],
133
+ ['CM', 'Cameroon'],
134
+ ['CN', 'China'],
135
+ ['CO', 'Colombia'],
136
+ ['CR', 'Costa Rica'],
137
+ ['CU', 'Cuba'],
138
+ ['CV', 'Cabo Verde'],
139
+ ['CW', 'Curaçao'],
140
+ ['CX', 'Christmas Island'],
141
+ ['CY', 'Cyprus'],
142
+ ['CZ', 'Czechia'],
143
+ ['DE', 'Germany'],
144
+ ['DJ', 'Djibouti'],
145
+ ['DK', 'Denmark'],
146
+ ['DM', 'Dominica'],
147
+ ['DO', 'Dominican Republic'],
148
+ ['DZ', 'Algeria'],
149
+ ['EC', 'Ecuador'],
150
+ ['EE', 'Estonia'],
151
+ ['EG', 'Egypt'],
152
+ ['EH', 'Western Sahara'],
153
+ ['ER', 'Eritrea'],
154
+ ['ES', 'Spain'],
155
+ ['ET', 'Ethiopia'],
156
+ ['EU', 'European Union'],
157
+ ['FI', 'Finland'],
158
+ ['FJ', 'Fiji'],
159
+ ['FK', 'Falkland Islands (Malvinas)'],
160
+ ['FM', 'Micronesia, Federated States of'],
161
+ ['FO', 'Faroe Islands'],
162
+ ['FR', 'France'],
163
+ ['GA', 'Gabon'],
164
+ ['GB', 'United Kingdom of Great Britain and Northern Ireland'],
165
+ ['GD', 'Grenada'],
166
+ ['GE', 'Georgia'],
167
+ ['GF', 'French Guiana'],
168
+ ['GG', 'Guernsey'],
169
+ ['GH', 'Ghana'],
170
+ ['GI', 'Gibraltar'],
171
+ ['GL', 'Greenland'],
172
+ ['GM', 'Gambia'],
173
+ ['GN', 'Guinea'],
174
+ ['GP', 'Guadeloupe'],
175
+ ['GQ', 'Equatorial Guinea'],
176
+ ['GR', 'Greece'],
177
+ ['GS', 'South Georgia and the South Sandwich Islands'],
178
+ ['GT', 'Guatemala'],
179
+ ['GU', 'Guam'],
180
+ ['GW', 'Guinea-Bissau'],
181
+ ['GY', 'Guyana'],
182
+ ['HK', 'Hong Kong'],
183
+ ['HM', 'Heard Island and McDonald Islands'],
184
+ ['HN', 'Honduras'],
185
+ ['HR', 'Croatia'],
186
+ ['HT', 'Haiti'],
187
+ ['HU', 'Hungary'],
188
+ ['ID', 'Indonesia'],
189
+ ['IE', 'Ireland'],
190
+ ['IL', 'Israel'],
191
+ ['IM', 'Isle of Man'],
192
+ ['IN', 'India'],
193
+ ['IO', 'British Indian Ocean Territory'],
194
+ ['IQ', 'Iraq'],
195
+ ['IR', 'Iran, Islamic Republic of'],
196
+ ['IS', 'Iceland'],
197
+ ['IT', 'Italy'],
198
+ ['JE', 'Jersey'],
199
+ ['JM', 'Jamaica'],
200
+ ['JO', 'Jordan'],
201
+ ['JP', 'Japan'],
202
+ ['KE', 'Kenya'],
203
+ ['KG', 'Kyrgyzstan'],
204
+ ['KH', 'Cambodia'],
205
+ ['KI', 'Kiribati'],
206
+ ['KM', 'Comoros'],
207
+ ['KN', 'Saint Kitts and Nevis'],
208
+ ['KP', "Korea, Democratic People's Republic of"],
209
+ ['KR', 'Korea, Republic of'],
210
+ ['KW', 'Kuwait'],
211
+ ['KY', 'Cayman Islands'],
212
+ ['KZ', 'Kazakhstan'],
213
+ ['LA', "Lao People's Democratic Republic"],
214
+ ['LB', 'Lebanon'],
215
+ ['LC', 'Saint Lucia'],
216
+ ['LI', 'Liechtenstein'],
217
+ ['LK', 'Sri Lanka'],
218
+ ['LR', 'Liberia'],
219
+ ['LS', 'Lesotho'],
220
+ ['LT', 'Lithuania'],
221
+ ['LU', 'Luxembourg'],
222
+ ['LV', 'Latvia'],
223
+ ['LY', 'Libya'],
224
+ ['MA', 'Morocco'],
225
+ ['MC', 'Monaco'],
226
+ ['MD', 'Moldova, Republic of'],
227
+ ['ME', 'Montenegro'],
228
+ ['MF', 'Saint Martin (French part)'],
229
+ ['MG', 'Madagascar'],
230
+ ['MH', 'Marshall Islands'],
231
+ ['MK', 'North Macedonia'],
232
+ ['ML', 'Mali'],
233
+ ['MM', 'Myanmar'],
234
+ ['MN', 'Mongolia'],
235
+ ['MO', 'Macao'],
236
+ ['MP', 'Northern Mariana Islands'],
237
+ ['MQ', 'Martinique'],
238
+ ['MR', 'Mauritania'],
239
+ ['MS', 'Montserrat'],
240
+ ['MT', 'Malta'],
241
+ ['MU', 'Mauritius'],
242
+ ['MV', 'Maldives'],
243
+ ['MW', 'Malawi'],
244
+ ['MX', 'Mexico'],
245
+ ['MY', 'Malaysia'],
246
+ ['MZ', 'Mozambique'],
247
+ ['NA', 'Namibia'],
248
+ ['NC', 'New Caledonia'],
249
+ ['NE', 'Niger'],
250
+ ['NF', 'Norfolk Island'],
251
+ ['NG', 'Nigeria'],
252
+ ['NI', 'Nicaragua'],
253
+ ['NL', 'Netherlands, Kingdom of the'],
254
+ ['NO', 'Norway'],
255
+ ['NP', 'Nepal'],
256
+ ['NR', 'Nauru'],
257
+ ['NU', 'Niue'],
258
+ ['NZ', 'New Zealand'],
259
+ ['OM', 'Oman'],
260
+ ['PA', 'Panama'],
261
+ ['PE', 'Peru'],
262
+ ['PF', 'French Polynesia'],
263
+ ['PG', 'Papua New Guinea'],
264
+ ['PH', 'Philippines'],
265
+ ['PK', 'Pakistan'],
266
+ ['PL', 'Poland'],
267
+ ['PM', 'Saint Pierre and Miquelon'],
268
+ ['PN', 'Pitcairn'],
269
+ ['PR', 'Puerto Rico'],
270
+ ['PS', 'Palestine, State of'],
271
+ ['PT', 'Portugal'],
272
+ ['PW', 'Palau'],
273
+ ['PY', 'Paraguay'],
274
+ ['QA', 'Qatar'],
275
+ ['RE', 'Réunion'],
276
+ ['RO', 'Romania'],
277
+ ['RS', 'Serbia'],
278
+ ['RU', 'Russian Federation'],
279
+ ['RW', 'Rwanda'],
280
+ ['SA', 'Saudi Arabia'],
281
+ ['SB', 'Solomon Islands'],
282
+ ['SC', 'Seychelles'],
283
+ ['SD', 'Sudan'],
284
+ ['SE', 'Sweden'],
285
+ ['SG', 'Singapore'],
286
+ ['SH', 'Saint Helena, Ascension and Tristan da Cunha'],
287
+ ['SI', 'Slovenia'],
288
+ ['SJ', 'Svalbard and Jan Mayen'],
289
+ ['SK', 'Slovakia'],
290
+ ['SL', 'Sierra Leone'],
291
+ ['SM', 'San Marino'],
292
+ ['SN', 'Senegal'],
293
+ ['SO', 'Somalia'],
294
+ ['SR', 'Suriname'],
295
+ ['SS', 'South Sudan'],
296
+ ['ST', 'Sao Tome and Principe'],
297
+ ['SV', 'El Salvador'],
298
+ ['SX', 'Sint Maarten (Dutch part)'],
299
+ ['SY', 'Syrian Arab Republic'],
300
+ ['SZ', 'Eswatini'],
301
+ ['TC', 'Turks and Caicos Islands'],
302
+ ['TD', 'Chad'],
303
+ ['TF', 'French Southern Territories'],
304
+ ['TG', 'Togo'],
305
+ ['TH', 'Thailand'],
306
+ ['TJ', 'Tajikistan'],
307
+ ['TK', 'Tokelau'],
308
+ ['TL', 'Timor-Leste'],
309
+ ['TM', 'Turkmenistan'],
310
+ ['TN', 'Tunisia'],
311
+ ['TO', 'Tonga'],
312
+ ['TR', 'Türkiye'],
313
+ ['TT', 'Trinidad and Tobago'],
314
+ ['TV', 'Tuvalu'],
315
+ ['TW', 'Taiwan, Province of China'],
316
+ ['TZ', 'Tanzania, United Republic of'],
317
+ ['UA', 'Ukraine'],
318
+ ['UG', 'Uganda'],
319
+ ['UK', 'United Kingdom'],
320
+ ['UM', 'United States Minor Outlying Islands'],
321
+ ['US', 'United States of America'],
322
+ ['UY', 'Uruguay'],
323
+ ['UZ', 'Uzbekistan'],
324
+ ['VA', 'Holy See'],
325
+ ['VC', 'Saint Vincent and the Grenadines'],
326
+ ['VE', 'Venezuela, Bolivarian Republic of'],
327
+ ['VG', 'Virgin Islands (British)'],
328
+ ['VI', 'Virgin Islands (U.S.)'],
329
+ ['VN', 'Viet Nam'],
330
+ ['VU', 'Vanuatu'],
331
+ ['WF', 'Wallis and Futuna'],
332
+ ['WS', 'Samoa'],
333
+ ['YE', 'Yemen'],
334
+ ['YT', 'Mayotte'],
335
+ ['ZA', 'South Africa'],
336
+ ['ZM', 'Zambia'],
337
+ ['ZW', 'Zimbabwe'],
338
+ ]);
339
+ new Set(isoCountries.keys());
340
+
341
+ // eslint-disable-next-line @nx/enforce-module-boundaries
342
+ const getGoogleConsentFromCategories = (categories) => {
343
+ const advertisingConsent = isCategoryConsented(categories, 'advertising');
344
+ const analyticsConsent = isCategoryConsented(categories, 'analytics');
345
+ return {
346
+ analytics_storage: analyticsConsent ? 'granted' : 'denied',
347
+ ad_storage: advertisingConsent ? 'granted' : 'denied',
348
+ ad_user_data: advertisingConsent ? 'granted' : 'denied',
349
+ ad_personalization: advertisingConsent ? 'granted' : 'denied',
350
+ };
351
+ };
352
+ // TODO: once categories becomes a stable field, we need to remove this and start using categories data for google consent mode.
353
+ const getGoogleConsentFromChannels = (consent) => {
354
+ const gadsConsent = hasUserConsentForProvider(consent, 'googleAdsClicks');
355
+ const ga4Consent = hasUserConsentForProvider(consent, 'googleAnalytics4');
356
+ return {
357
+ analytics_storage: ga4Consent ? 'granted' : 'denied',
358
+ ad_storage: gadsConsent ? 'granted' : 'denied',
359
+ ad_user_data: gadsConsent ? 'granted' : 'denied',
360
+ ad_personalization: gadsConsent ? 'granted' : 'denied',
361
+ };
362
+ };
363
+
48
364
  // eslint-disable-next-line @nx/enforce-module-boundaries
49
- const initGA4 = (ID, executionContext) => {
365
+ const initGA4 = (ID, advancedConsentMode, consentData, executionContext) => {
50
366
  var _a;
51
367
  window.dataLayer = window.dataLayer || [];
52
368
  window.gtag = function gtag() {
@@ -60,14 +376,15 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
60
376
  break;
61
377
  }
62
378
  }
379
+ const data = advancedConsentMode === '1'
380
+ ? getGoogleConsentFromCategories(consentData.categories)
381
+ : getGoogleConsentFromChannels(consentData.consent);
63
382
  if (isConsentInitialised) {
64
- window.gtag('consent', 'update', {
65
- analytics_storage: 'granted',
66
- });
383
+ window.gtag('consent', 'update', data);
67
384
  }
68
385
  else {
69
386
  window.gtag('consent', 'default', {
70
- analytics_storage: 'granted',
387
+ ...data,
71
388
  functional_storage: 'granted',
72
389
  personalization_storage: 'granted',
73
390
  security_storage: 'granted',
@@ -85,7 +402,7 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
85
402
  script.parentNode.insertBefore(element, script);
86
403
  }
87
404
  };
88
- const init = ({ manifest, userId, executionContext }) => {
405
+ const init = ({ manifest, userId, executionContext, consentData, }) => {
89
406
  if (!window ||
90
407
  !manifest.variables ||
91
408
  !manifest.variables['measurementId'] ||
@@ -94,7 +411,7 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
94
411
  }
95
412
  if (!window.google_tag_manager ||
96
413
  !window.google_tag_manager[manifest.variables['measurementId']]) {
97
- initGA4(manifest.variables['measurementId'], executionContext);
414
+ initGA4(manifest.variables['measurementId'], manifest.variables['advancedConsentMode'], consentData, executionContext);
98
415
  }
99
416
  if (window.gtag) {
100
417
  window.gtag('config', manifest.variables['measurementId'], {
@@ -355,16 +672,20 @@ var ProvidersGoogleAnalytics4Sdk = (function () {
355
672
  }
356
673
  return {
357
674
  loaded: isLoaded,
358
- sdkVersion: "0.71.0" ,
675
+ sdkVersion: "1.0.0" ,
359
676
  };
360
677
  };
361
678
 
362
- const consent = ({ hasConsent }) => {
679
+ const consent = ({ consentData, variables }) => {
363
680
  if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
364
681
  return;
365
682
  }
683
+ const isAdvancedConsentModeEnabled = variables.some((variable) => { var _a; return ((_a = variable.variableSet) === null || _a === void 0 ? void 0 : _a['advancedConsentMode']) === '1'; });
684
+ const data = isAdvancedConsentModeEnabled
685
+ ? getGoogleConsentFromCategories(consentData.categories)
686
+ : getGoogleConsentFromChannels(consentData.consent);
366
687
  window.gtag('consent', 'update', {
367
- analytics_storage: hasConsent ? 'granted' : 'denied',
688
+ ...data,
368
689
  functional_storage: 'granted',
369
690
  personalization_storage: 'granted',
370
691
  security_storage: 'granted',
package/index.mjs CHANGED
@@ -1,6 +1,40 @@
1
1
  const packageName = 'googleAnalytics4';
2
2
  const tagManagerUrl = 'https://www.googletagmanager.com/gtag/js';
3
3
 
4
+ const isBool = (v) => typeof v == 'boolean';
5
+ const isRecord = (v) => !!v && typeof v == 'object' && !Array.isArray(v);
6
+ /**
7
+ * This function validates user consent for a given provider type, not based on tagName.
8
+ */
9
+ const hasUserConsentForProvider = (consent, provider) => {
10
+ if (!isRecord(consent)) {
11
+ return false;
12
+ }
13
+ let allowed = isBool(consent.all) ? consent.all : false;
14
+ if (provider in consent) {
15
+ const providerSpecific = consent[provider];
16
+ if (isBool(providerSpecific)) {
17
+ allowed = providerSpecific;
18
+ }
19
+ else if (isRecord(providerSpecific)) {
20
+ return Object.keys(providerSpecific).some((instance) => providerSpecific[instance] === true);
21
+ }
22
+ }
23
+ return allowed;
24
+ };
25
+ const isCategoryConsented = (consentCategories, category) => {
26
+ if (!consentCategories)
27
+ return false;
28
+ let allowed = isBool(consentCategories.all) ? consentCategories.all : false;
29
+ if (category in consentCategories) {
30
+ const categorySpecific = consentCategories[category];
31
+ if (isBool(categorySpecific)) {
32
+ allowed = categorySpecific;
33
+ }
34
+ }
35
+ return allowed;
36
+ };
37
+
4
38
  const upsert = (map, key, update, createDefault) => {
5
39
  const currentValue = map.has(key)
6
40
  ? map.get(key)
@@ -42,8 +76,290 @@ new Set([
42
76
  ...expand('911,921,922,923,924,926,927,932,933,935,942,944,946,950,953,955,957-958,960-969,974,975,976,977,981-982,987,988,990-999'),
43
77
  ]);
44
78
 
79
+ /**
80
+ * ISO-3166 2-leter country codes and their names
81
+ * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
82
+ */
83
+ const isoCountries = new Map([
84
+ ['AD', 'Andorra'],
85
+ ['AE', 'United Arab Emirates'],
86
+ ['AF', 'Afghanistan'],
87
+ ['AG', 'Antigua and Barbuda'],
88
+ ['AI', 'Anguilla'],
89
+ ['AL', 'Albania'],
90
+ ['AM', 'Armenia'],
91
+ ['AO', 'Angola'],
92
+ ['AQ', 'Antarctica'],
93
+ ['AR', 'Argentina'],
94
+ ['AS', 'American Samoa'],
95
+ ['AT', 'Austria'],
96
+ ['AU', 'Australia'],
97
+ ['AW', 'Aruba'],
98
+ ['AX', 'Åland Islands'],
99
+ ['AZ', 'Azerbaijan'],
100
+ ['BA', 'Bosnia and Herzegovina'],
101
+ ['BB', 'Barbados'],
102
+ ['BD', 'Bangladesh'],
103
+ ['BE', 'Belgium'],
104
+ ['BF', 'Burkina Faso'],
105
+ ['BG', 'Bulgaria'],
106
+ ['BH', 'Bahrain'],
107
+ ['BI', 'Burundi'],
108
+ ['BJ', 'Benin'],
109
+ ['BL', 'Saint Barthélemy'],
110
+ ['BM', 'Bermuda'],
111
+ ['BN', 'Brunei Darussalam'],
112
+ ['BO', 'Bolivia, Plurinational State of'],
113
+ ['BQ', 'Bonaire, Sint Eustatius and Saba'],
114
+ ['BR', 'Brazil'],
115
+ ['BS', 'Bahamas'],
116
+ ['BT', 'Bhutan'],
117
+ ['BV', 'Bouvet Island'],
118
+ ['BW', 'Botswana'],
119
+ ['BY', 'Belarus'],
120
+ ['BZ', 'Belize'],
121
+ ['CA', 'Canada'],
122
+ ['CC', 'Cocos (Keeling) Islands'],
123
+ ['CD', 'Congo, Democratic Republic of the'],
124
+ ['CF', 'Central African Republic'],
125
+ ['CG', 'Congo'],
126
+ ['CH', 'Switzerland'],
127
+ ['CI', "Côte d'Ivoire"],
128
+ ['CK', 'Cook Islands'],
129
+ ['CL', 'Chile'],
130
+ ['CM', 'Cameroon'],
131
+ ['CN', 'China'],
132
+ ['CO', 'Colombia'],
133
+ ['CR', 'Costa Rica'],
134
+ ['CU', 'Cuba'],
135
+ ['CV', 'Cabo Verde'],
136
+ ['CW', 'Curaçao'],
137
+ ['CX', 'Christmas Island'],
138
+ ['CY', 'Cyprus'],
139
+ ['CZ', 'Czechia'],
140
+ ['DE', 'Germany'],
141
+ ['DJ', 'Djibouti'],
142
+ ['DK', 'Denmark'],
143
+ ['DM', 'Dominica'],
144
+ ['DO', 'Dominican Republic'],
145
+ ['DZ', 'Algeria'],
146
+ ['EC', 'Ecuador'],
147
+ ['EE', 'Estonia'],
148
+ ['EG', 'Egypt'],
149
+ ['EH', 'Western Sahara'],
150
+ ['ER', 'Eritrea'],
151
+ ['ES', 'Spain'],
152
+ ['ET', 'Ethiopia'],
153
+ ['EU', 'European Union'],
154
+ ['FI', 'Finland'],
155
+ ['FJ', 'Fiji'],
156
+ ['FK', 'Falkland Islands (Malvinas)'],
157
+ ['FM', 'Micronesia, Federated States of'],
158
+ ['FO', 'Faroe Islands'],
159
+ ['FR', 'France'],
160
+ ['GA', 'Gabon'],
161
+ ['GB', 'United Kingdom of Great Britain and Northern Ireland'],
162
+ ['GD', 'Grenada'],
163
+ ['GE', 'Georgia'],
164
+ ['GF', 'French Guiana'],
165
+ ['GG', 'Guernsey'],
166
+ ['GH', 'Ghana'],
167
+ ['GI', 'Gibraltar'],
168
+ ['GL', 'Greenland'],
169
+ ['GM', 'Gambia'],
170
+ ['GN', 'Guinea'],
171
+ ['GP', 'Guadeloupe'],
172
+ ['GQ', 'Equatorial Guinea'],
173
+ ['GR', 'Greece'],
174
+ ['GS', 'South Georgia and the South Sandwich Islands'],
175
+ ['GT', 'Guatemala'],
176
+ ['GU', 'Guam'],
177
+ ['GW', 'Guinea-Bissau'],
178
+ ['GY', 'Guyana'],
179
+ ['HK', 'Hong Kong'],
180
+ ['HM', 'Heard Island and McDonald Islands'],
181
+ ['HN', 'Honduras'],
182
+ ['HR', 'Croatia'],
183
+ ['HT', 'Haiti'],
184
+ ['HU', 'Hungary'],
185
+ ['ID', 'Indonesia'],
186
+ ['IE', 'Ireland'],
187
+ ['IL', 'Israel'],
188
+ ['IM', 'Isle of Man'],
189
+ ['IN', 'India'],
190
+ ['IO', 'British Indian Ocean Territory'],
191
+ ['IQ', 'Iraq'],
192
+ ['IR', 'Iran, Islamic Republic of'],
193
+ ['IS', 'Iceland'],
194
+ ['IT', 'Italy'],
195
+ ['JE', 'Jersey'],
196
+ ['JM', 'Jamaica'],
197
+ ['JO', 'Jordan'],
198
+ ['JP', 'Japan'],
199
+ ['KE', 'Kenya'],
200
+ ['KG', 'Kyrgyzstan'],
201
+ ['KH', 'Cambodia'],
202
+ ['KI', 'Kiribati'],
203
+ ['KM', 'Comoros'],
204
+ ['KN', 'Saint Kitts and Nevis'],
205
+ ['KP', "Korea, Democratic People's Republic of"],
206
+ ['KR', 'Korea, Republic of'],
207
+ ['KW', 'Kuwait'],
208
+ ['KY', 'Cayman Islands'],
209
+ ['KZ', 'Kazakhstan'],
210
+ ['LA', "Lao People's Democratic Republic"],
211
+ ['LB', 'Lebanon'],
212
+ ['LC', 'Saint Lucia'],
213
+ ['LI', 'Liechtenstein'],
214
+ ['LK', 'Sri Lanka'],
215
+ ['LR', 'Liberia'],
216
+ ['LS', 'Lesotho'],
217
+ ['LT', 'Lithuania'],
218
+ ['LU', 'Luxembourg'],
219
+ ['LV', 'Latvia'],
220
+ ['LY', 'Libya'],
221
+ ['MA', 'Morocco'],
222
+ ['MC', 'Monaco'],
223
+ ['MD', 'Moldova, Republic of'],
224
+ ['ME', 'Montenegro'],
225
+ ['MF', 'Saint Martin (French part)'],
226
+ ['MG', 'Madagascar'],
227
+ ['MH', 'Marshall Islands'],
228
+ ['MK', 'North Macedonia'],
229
+ ['ML', 'Mali'],
230
+ ['MM', 'Myanmar'],
231
+ ['MN', 'Mongolia'],
232
+ ['MO', 'Macao'],
233
+ ['MP', 'Northern Mariana Islands'],
234
+ ['MQ', 'Martinique'],
235
+ ['MR', 'Mauritania'],
236
+ ['MS', 'Montserrat'],
237
+ ['MT', 'Malta'],
238
+ ['MU', 'Mauritius'],
239
+ ['MV', 'Maldives'],
240
+ ['MW', 'Malawi'],
241
+ ['MX', 'Mexico'],
242
+ ['MY', 'Malaysia'],
243
+ ['MZ', 'Mozambique'],
244
+ ['NA', 'Namibia'],
245
+ ['NC', 'New Caledonia'],
246
+ ['NE', 'Niger'],
247
+ ['NF', 'Norfolk Island'],
248
+ ['NG', 'Nigeria'],
249
+ ['NI', 'Nicaragua'],
250
+ ['NL', 'Netherlands, Kingdom of the'],
251
+ ['NO', 'Norway'],
252
+ ['NP', 'Nepal'],
253
+ ['NR', 'Nauru'],
254
+ ['NU', 'Niue'],
255
+ ['NZ', 'New Zealand'],
256
+ ['OM', 'Oman'],
257
+ ['PA', 'Panama'],
258
+ ['PE', 'Peru'],
259
+ ['PF', 'French Polynesia'],
260
+ ['PG', 'Papua New Guinea'],
261
+ ['PH', 'Philippines'],
262
+ ['PK', 'Pakistan'],
263
+ ['PL', 'Poland'],
264
+ ['PM', 'Saint Pierre and Miquelon'],
265
+ ['PN', 'Pitcairn'],
266
+ ['PR', 'Puerto Rico'],
267
+ ['PS', 'Palestine, State of'],
268
+ ['PT', 'Portugal'],
269
+ ['PW', 'Palau'],
270
+ ['PY', 'Paraguay'],
271
+ ['QA', 'Qatar'],
272
+ ['RE', 'Réunion'],
273
+ ['RO', 'Romania'],
274
+ ['RS', 'Serbia'],
275
+ ['RU', 'Russian Federation'],
276
+ ['RW', 'Rwanda'],
277
+ ['SA', 'Saudi Arabia'],
278
+ ['SB', 'Solomon Islands'],
279
+ ['SC', 'Seychelles'],
280
+ ['SD', 'Sudan'],
281
+ ['SE', 'Sweden'],
282
+ ['SG', 'Singapore'],
283
+ ['SH', 'Saint Helena, Ascension and Tristan da Cunha'],
284
+ ['SI', 'Slovenia'],
285
+ ['SJ', 'Svalbard and Jan Mayen'],
286
+ ['SK', 'Slovakia'],
287
+ ['SL', 'Sierra Leone'],
288
+ ['SM', 'San Marino'],
289
+ ['SN', 'Senegal'],
290
+ ['SO', 'Somalia'],
291
+ ['SR', 'Suriname'],
292
+ ['SS', 'South Sudan'],
293
+ ['ST', 'Sao Tome and Principe'],
294
+ ['SV', 'El Salvador'],
295
+ ['SX', 'Sint Maarten (Dutch part)'],
296
+ ['SY', 'Syrian Arab Republic'],
297
+ ['SZ', 'Eswatini'],
298
+ ['TC', 'Turks and Caicos Islands'],
299
+ ['TD', 'Chad'],
300
+ ['TF', 'French Southern Territories'],
301
+ ['TG', 'Togo'],
302
+ ['TH', 'Thailand'],
303
+ ['TJ', 'Tajikistan'],
304
+ ['TK', 'Tokelau'],
305
+ ['TL', 'Timor-Leste'],
306
+ ['TM', 'Turkmenistan'],
307
+ ['TN', 'Tunisia'],
308
+ ['TO', 'Tonga'],
309
+ ['TR', 'Türkiye'],
310
+ ['TT', 'Trinidad and Tobago'],
311
+ ['TV', 'Tuvalu'],
312
+ ['TW', 'Taiwan, Province of China'],
313
+ ['TZ', 'Tanzania, United Republic of'],
314
+ ['UA', 'Ukraine'],
315
+ ['UG', 'Uganda'],
316
+ ['UK', 'United Kingdom'],
317
+ ['UM', 'United States Minor Outlying Islands'],
318
+ ['US', 'United States of America'],
319
+ ['UY', 'Uruguay'],
320
+ ['UZ', 'Uzbekistan'],
321
+ ['VA', 'Holy See'],
322
+ ['VC', 'Saint Vincent and the Grenadines'],
323
+ ['VE', 'Venezuela, Bolivarian Republic of'],
324
+ ['VG', 'Virgin Islands (British)'],
325
+ ['VI', 'Virgin Islands (U.S.)'],
326
+ ['VN', 'Viet Nam'],
327
+ ['VU', 'Vanuatu'],
328
+ ['WF', 'Wallis and Futuna'],
329
+ ['WS', 'Samoa'],
330
+ ['YE', 'Yemen'],
331
+ ['YT', 'Mayotte'],
332
+ ['ZA', 'South Africa'],
333
+ ['ZM', 'Zambia'],
334
+ ['ZW', 'Zimbabwe'],
335
+ ]);
336
+ new Set(isoCountries.keys());
337
+
338
+ // eslint-disable-next-line @nx/enforce-module-boundaries
339
+ const getGoogleConsentFromCategories = (categories) => {
340
+ const advertisingConsent = isCategoryConsented(categories, 'advertising');
341
+ const analyticsConsent = isCategoryConsented(categories, 'analytics');
342
+ return {
343
+ analytics_storage: analyticsConsent ? 'granted' : 'denied',
344
+ ad_storage: advertisingConsent ? 'granted' : 'denied',
345
+ ad_user_data: advertisingConsent ? 'granted' : 'denied',
346
+ ad_personalization: advertisingConsent ? 'granted' : 'denied',
347
+ };
348
+ };
349
+ // TODO: once categories becomes a stable field, we need to remove this and start using categories data for google consent mode.
350
+ const getGoogleConsentFromChannels = (consent) => {
351
+ const gadsConsent = hasUserConsentForProvider(consent, 'googleAdsClicks');
352
+ const ga4Consent = hasUserConsentForProvider(consent, 'googleAnalytics4');
353
+ return {
354
+ analytics_storage: ga4Consent ? 'granted' : 'denied',
355
+ ad_storage: gadsConsent ? 'granted' : 'denied',
356
+ ad_user_data: gadsConsent ? 'granted' : 'denied',
357
+ ad_personalization: gadsConsent ? 'granted' : 'denied',
358
+ };
359
+ };
360
+
45
361
  // eslint-disable-next-line @nx/enforce-module-boundaries
46
- const initGA4 = (ID, executionContext) => {
362
+ const initGA4 = (ID, advancedConsentMode, consentData, executionContext) => {
47
363
  var _a;
48
364
  window.dataLayer = window.dataLayer || [];
49
365
  window.gtag = function gtag() {
@@ -57,14 +373,15 @@ const initGA4 = (ID, executionContext) => {
57
373
  break;
58
374
  }
59
375
  }
376
+ const data = advancedConsentMode === '1'
377
+ ? getGoogleConsentFromCategories(consentData.categories)
378
+ : getGoogleConsentFromChannels(consentData.consent);
60
379
  if (isConsentInitialised) {
61
- window.gtag('consent', 'update', {
62
- analytics_storage: 'granted',
63
- });
380
+ window.gtag('consent', 'update', data);
64
381
  }
65
382
  else {
66
383
  window.gtag('consent', 'default', {
67
- analytics_storage: 'granted',
384
+ ...data,
68
385
  functional_storage: 'granted',
69
386
  personalization_storage: 'granted',
70
387
  security_storage: 'granted',
@@ -82,7 +399,7 @@ const initGA4 = (ID, executionContext) => {
82
399
  script.parentNode.insertBefore(element, script);
83
400
  }
84
401
  };
85
- const init = ({ manifest, userId, executionContext }) => {
402
+ const init = ({ manifest, userId, executionContext, consentData, }) => {
86
403
  if (!window ||
87
404
  !manifest.variables ||
88
405
  !manifest.variables['measurementId'] ||
@@ -91,7 +408,7 @@ const init = ({ manifest, userId, executionContext }) => {
91
408
  }
92
409
  if (!window.google_tag_manager ||
93
410
  !window.google_tag_manager[manifest.variables['measurementId']]) {
94
- initGA4(manifest.variables['measurementId'], executionContext);
411
+ initGA4(manifest.variables['measurementId'], manifest.variables['advancedConsentMode'], consentData, executionContext);
95
412
  }
96
413
  if (window.gtag) {
97
414
  window.gtag('config', manifest.variables['measurementId'], {
@@ -352,16 +669,20 @@ const tag = ({ data, eventName, manifestVariables, eventId }) => {
352
669
  }
353
670
  return {
354
671
  loaded: isLoaded,
355
- sdkVersion: "0.71.0" ,
672
+ sdkVersion: "1.0.0" ,
356
673
  };
357
674
  };
358
675
 
359
- const consent = ({ hasConsent }) => {
676
+ const consent = ({ consentData, variables }) => {
360
677
  if (!(window === null || window === void 0 ? void 0 : window.gtag)) {
361
678
  return;
362
679
  }
680
+ const isAdvancedConsentModeEnabled = variables.some((variable) => { var _a; return ((_a = variable.variableSet) === null || _a === void 0 ? void 0 : _a['advancedConsentMode']) === '1'; });
681
+ const data = isAdvancedConsentModeEnabled
682
+ ? getGoogleConsentFromCategories(consentData.categories)
683
+ : getGoogleConsentFromChannels(consentData.consent);
363
684
  window.gtag('consent', 'update', {
364
- analytics_storage: hasConsent ? 'granted' : 'denied',
685
+ ...data,
365
686
  functional_storage: 'granted',
366
687
  personalization_storage: 'granted',
367
688
  security_storage: 'granted',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/providers-google-analytics-4-sdk",
3
- "version": "0.71.0",
3
+ "version": "1.0.0",
4
4
  "description": "Google Analytics 4 Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",