-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
230 lines (204 loc) · 5.35 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { getCookie, gtag, isObject } from './utils';
/**
* Cookie format: {consents: {'necessary': true, 'analytics': true, 'marketing': false}, version: 1}
*/
export const COOKIE_NAME = 'gds-consent';
export const EVENT_CONSENT = 'gds-cmp.consent';
export const NECESSARY_STORAGE_CONSENT = 'necessary';
export const PREFERENCES_STORAGE_CONSENT = 'preferences';
export const AD_STORAGE_CONSENT = 'marketing';
export const ANALYTICS_STORAGE_CONSENT = 'statistics';
export const NECESSARY_COOKIES = [
COOKIE_NAME,
`${COOKIE_NAME}-hash`,
];
/**
* @typedef {('necessary'|'preferences'|'marketing'|'statistics')} Consent
*/
/**
* @typedef {object} ConsentData
* @property {Consents} consents
* @property {number|undefined} version
*/
/**
* @typedef {object} Consents
* @property {boolean} necessary
* @property {boolean} analytics
* @property {boolean} marketing
*/
/**
* Return if user has granted the passed consents.
*
* @param {...Consent} consents Consent name eg. 'necessary`, `preferences`, `marketing`, `statistics`.
* @returns {boolean} if user has granted all the consents
*/
export function hasConsent(...consents) {
const consentData = window.gdsCmp.getConsentData();
return consents.every((consent) => {
return consentData.consents?.[consent] === true;
})
}
/**
* Return the consent data object read from the current cookie.
*
* @returns {ConsentData}
*/
export function getConsentData() {
return parseConsentString(
getCookie(COOKIE_NAME)
);
}
/**
* Return a serialized consent data json string that can be stored as a cookie.
*
* @param {Consents} consents
* @param {number} version
* @returns {JSON} Serialized consent data
*/
export function buildConsentString(consents, version = 1) {
return JSON.stringify({
consents,
version,
});
}
/**
* @private
* @param {string} consentString The serialized JSON object stored in a cookie
* @returns {ConsentData}
*/
function parseConsentString(consentString) {
let data = {
consents: {},
version: undefined,
};
try {
const parsedData = JSON.parse(decodeURIComponent(consentString));
if (isObject(parsedData)) {
if (isObject(parsedData.consents)) {
data.consents = parsedData.consents;
}
if (typeof parsedData.version === 'number') {
data.version = parsedData.version;
}
}
} catch (e) {
// Migrate legacy data.
if (typeof consentString === 'string' && /[0-9](,[01])+/.test(consentString)) {
const parsedData = consentString.split(',');
data = {
...data,
...{
version: parsedData.shift() || undefined,
consents: {
[NECESSARY_STORAGE_CONSENT]: parsedData.shift() === '1',
[ANALYTICS_STORAGE_CONSENT]: parsedData.shift() === '1',
[AD_STORAGE_CONSENT]: parsedData.shift() === '1',
},
},
};
}
}
return data;
}
/**
* Update all supported consent modes.
*
* @param {bool} synchronous Set the consent mode in the same thread (default: false)
* @returns {void}
*/
export function updateConsentMode(synchronous = false) {
[
googleConsentMode,
metaConsentMode,
tiktokConsentMode,
wpConsentMode,
].forEach((cb) => {
if (synchronous) {
cb();
} else {
setTimeout(cb, 0);
}
});
}
/**
* Update Google Tag Manager consent mode.
*
* @returns {void}
*/
export function googleConsentMode() {
const consentData = window.gdsCmp.getConsentData();
const gtmConsents = {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
functionality_storage: 'denied',
personalization_storage: 'denied',
security_storage: 'granted',
};
// Map custom consent names to GTM consent names.
for (const [consent, value] of Object.entries(consentData.consents)) {
const consentSettings = window.gdsCmp?.consents?.find?.((c) => c.id === consent);
if (!consentSettings) {
continue;
}
for (const gtmConsentModeId of consentSettings.gtmConsentModes) {
gtmConsents[gtmConsentModeId] = value === true ? 'granted' : 'denied';
}
}
gtag('consent', 'update', gtmConsents);
gtag('set', {consents: gtmConsents});
}
/**
* Update Meta pixel consent mode.
*
* @returns {void}
*/
export function metaConsentMode() {
if (!window.fbq) {
return;
}
if (window.gdsCmp.hasConsent(AD_STORAGE_CONSENT, ANALYTICS_STORAGE_CONSENT)) {
window.fbq('consent', 'grant');
console.debug('meta pixel consent granted');
} else {
window.fbq('consent', 'revoke');
console.debug('meta pixel consent revoked');
}
}
/**
* Update TikTok consent mode.
*
* @returns {void}
*/
export function tiktokConsentMode() {
if (!window.ttq) {
return;
}
if (window.gdsCmp.hasConsent(AD_STORAGE_CONSENT, ANALYTICS_STORAGE_CONSENT)) {
window.ttq.enableCookie();
console.debug('tiktok enable cookies.');
} else {
window.ttq.disableCookie();
console.debug('tiktok disable cookies.');
}
}
/**
* Update WP Consent API consent mode.
*
* @returns {void}
*/
export function wpConsentMode() {
if (!window.wp_set_consent) {
return;
}
for (const consent of window.gdsCmp.consents) {
if (! consent.wpConsentApiCategory) {
continue;
}
window.wp_set_consent(
consent.wpConsentApiCategory,
window.gdsCmp.hasConsent(consent.id) ? 'allow' : 'deny'
);
}
}