@jarijokinen/consent 0.0.6 → 0.0.8

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/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
  The MIT License (MIT)
2
- Copyright (c) 2022 - 2025 Jari Jokinen
2
+ Copyright (c) 2022 - 2026 Jari Jokinen
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy of
5
5
  this software and associated documentation files (the "Software"), to deal in
package/README.md CHANGED
@@ -11,10 +11,10 @@ NPM:
11
11
 
12
12
  ## Usage
13
13
 
14
- Load the consent-loader.js in the HEAD section deferred before any GTM tags.
14
+ Load the consent-loader.js in the HEAD section before any GTM tags.
15
15
 
16
16
  <head>
17
- <script src="path/to/consent-loader.js" defer></script>
17
+ <script src="path/to/consent-loader.js"></script>
18
18
  <script>
19
19
  // GTM and any other scripts
20
20
  </script>
@@ -34,6 +34,21 @@ Add link that opens the dialog:
34
34
 
35
35
  ## Configuration
36
36
 
37
+ `ad_user_data` and `ad_personalization` are always denied, regardless of
38
+ country, saved preferences, or dialog selections (including Allow All).
39
+
40
+ The country cookie name defaults to `cc`. To use a different name, set
41
+ `window.consent.countryCookieName` before loading `consent-loader.js`:
42
+
43
+ <script>
44
+ window.consent = window.consent || {};
45
+ window.consent.countryCookieName = 'country_code';
46
+ </script>
47
+ <script src="path/to/consent-loader.js"></script>
48
+
49
+ The cookie value should be a two-letter country code, such as `FI` or `US`.
50
+ If the configured cookie is missing or empty, consent defaults to denied.
51
+
37
52
  Customize configuration options by passing some or all of them as a first
38
53
  argument to the consent() function.
39
54
 
@@ -63,6 +78,6 @@ argument to the consent() function.
63
78
 
64
79
  ## License
65
80
 
66
- MIT License. Copyright (c) 2022 - 2025 [Jari Jokinen](https://jarijokinen.com).
81
+ MIT License. Copyright (c) 2022 - 2026 [Jari Jokinen](https://jarijokinen.com).
67
82
  See [LICENSE](https://github.com/jarijokinen/consent/blob/main/LICENSE.txt)
68
83
  for further details.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jarijokinen/consent",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A zero-dependency, vanilla JavaScript consent manager with native Google Consent Mode v2 support.",
5
5
  "main": "src/consent.js",
6
6
  "repository": {
@@ -2,6 +2,7 @@ window.dataLayer = window.dataLayer || [];
2
2
  function gtag() { dataLayer.push(arguments); }
3
3
 
4
4
  window.consent = window.consent || {};
5
+ window.consent.countryCookieName = window.consent.countryCookieName ?? 'cc';
5
6
  window.consent.countries = window.consent.countries || [
6
7
  'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU',
7
8
  'IS', 'IE', 'IT', 'LV', 'LI', 'LT', 'LU', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO',
@@ -11,11 +12,11 @@ window.consent.consentMissing = true
11
12
  window.consent.state = {
12
13
  analytics_storage: null,
13
14
  ad_storage: null,
14
- ad_user_data: null,
15
- ad_personalization: null
15
+ ad_user_data: 'denied',
16
+ ad_personalization: 'denied'
16
17
  };
17
18
 
18
- Object.keys(window.consent.state).forEach(k => {
19
+ ['analytics_storage', 'ad_storage'].forEach(k => {
19
20
  try {
20
21
  const v = localStorage.getItem('consent_' + k);
21
22
  if (v === 'granted' || v === 'denied') {
@@ -28,8 +29,11 @@ Object.keys(window.consent.state).forEach(k => {
28
29
  }
29
30
  });
30
31
 
31
- const country = document.cookie.split(/(?:^|;\s*)cc=/).pop().split(';')[0]
32
- .trim().toUpperCase();
32
+ const countryCookie = document.cookie.split(';').map(cookie => cookie.trim())
33
+ .find(cookie => cookie.startsWith(window.consent.countryCookieName + '='));
34
+ const country = countryCookie
35
+ ? countryCookie.slice(countryCookie.indexOf('=') + 1).trim().toUpperCase()
36
+ : '';
33
37
  const countryDefault = !country || window.consent.countries.includes(country)
34
38
  ? 'denied' : 'granted';
35
39
 
@@ -37,7 +41,8 @@ if (countryDefault === 'granted') {
37
41
  window.consent.consentMissing = false;
38
42
  }
39
43
 
40
- gtag('consent', 'default', {
41
- ...Object.fromEntries(Object.entries(window.consent.state).map(([k, v]) =>
42
- [k, v ?? countryDefault]))
44
+ Object.keys(window.consent.state).forEach(k => {
45
+ window.consent.state[k] = window.consent.state[k] ?? countryDefault;
43
46
  });
47
+
48
+ gtag('consent', 'default', { ...window.consent.state });
package/src/consent.js CHANGED
@@ -5,9 +5,9 @@ export const consent = (options) => {
5
5
  ad_storage: 'Marketing'
6
6
  },
7
7
  actions: {
8
- allowAll: 'Allow All',
8
+ denyAll: 'Deny All',
9
9
  allowSelected: 'Allow Selected',
10
- denyAll: 'Deny All'
10
+ allowAll: 'Allow All'
11
11
  },
12
12
  dialogMarkup: `
13
13
  <h2 class="consent-title"></h2>
@@ -95,6 +95,9 @@ export const consent = (options) => {
95
95
  };
96
96
 
97
97
  const saveState = () => {
98
+ window.consent.state.ad_user_data = 'denied';
99
+ window.consent.state.ad_personalization = 'denied';
100
+
98
101
  Object.keys(window.consent.state).forEach(k => {
99
102
  try {
100
103
  localStorage.setItem('consent_' + k, window.consent.state[k]);