@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 +1 -1
- package/README.md +18 -3
- package/package.json +1 -1
- package/src/consent-loader.js +13 -8
- package/src/consent.js +5 -2
package/LICENSE.txt
CHANGED
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
|
|
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"
|
|
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 -
|
|
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
package/src/consent-loader.js
CHANGED
|
@@ -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:
|
|
15
|
-
ad_personalization:
|
|
15
|
+
ad_user_data: 'denied',
|
|
16
|
+
ad_personalization: 'denied'
|
|
16
17
|
};
|
|
17
18
|
|
|
18
|
-
|
|
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
|
|
32
|
-
.
|
|
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
|
-
|
|
41
|
-
|
|
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
|
-
|
|
8
|
+
denyAll: 'Deny All',
|
|
9
9
|
allowSelected: 'Allow Selected',
|
|
10
|
-
|
|
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]);
|