-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcfapi.js
104 lines (92 loc) · 2.49 KB
/
tcfapi.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
/* eslint no-unused-vars:"off" */
import {
AD_STORAGE_CONSENT,
ANALYTICS_STORAGE_CONSENT,
EVENT_CONSENT,
NECESSARY_STORAGE_CONSENT,
PREFERENCES_STORAGE_CONSENT,
} from './api';
/**
* @param {TCData} tcData
* @returns {ConsentData}
*/
function getTcfMappedConsentData({purpose}) {
const {consents} = purpose;
const hasStorageConsent = consents[1];
const hasRegularAdsConsent = consents[2];
const hasPersonalizedAdProfileConsent = consents[3];
const hasPersonalizedAdsConsent = consents[4];
const hasPersonalizedContentProfileConsent = consents[5];
const hasPersonalizedSelectionConsent = consents[6];
const hasAdMeasurementConsent = consents[7];
const hasContentMeasurementConsent = consents[8];
const hasMarketResearchConsent = consents[9];
const hasProductDevelopmentConsent = consents[10];
const hasNecessaryConsent = (
true
);
const hasPreferencesConsent = (
hasStorageConsent
);
const hasStatisticsConsent = (
hasStorageConsent &&
hasAdMeasurementConsent &&
// hasContentMeasurementConsent &&
hasMarketResearchConsent
);
const hasMarketingConsent = (
hasStorageConsent &&
hasRegularAdsConsent &&
hasPersonalizedAdProfileConsent &&
hasPersonalizedAdsConsent
)
return {
consents: {
[NECESSARY_STORAGE_CONSENT]: hasNecessaryConsent,
[PREFERENCES_STORAGE_CONSENT]: hasPreferencesConsent,
[AD_STORAGE_CONSENT]: hasMarketingConsent,
[ANALYTICS_STORAGE_CONSENT]: hasStatisticsConsent,
},
version: undefined,
}
}
function getConsentData() {
return window.gdsCmp?.tcfMappedConsents;
}
function tcfapi() {
if (!window.__tcfapi) {
return setTimeout(() => tcfapi(...arguments), 100);
}
window.__tcfapi(...arguments);
}
function runEvent() {
window.dispatchEvent(new CustomEvent(EVENT_CONSENT));
for (const [consent, value] of Object.entries(getConsentData().consents)) {
if (value) {
window.dispatchEvent(new CustomEvent(`${EVENT_CONSENT}.${consent}`));
}
}
}
window.gdsCmp = {
...(window.gdsCmp || {}),
tcfMappedConsents: {
version: undefined,
consents: {},
},
getConsentData,
show() {
tcfapi('displayConsentUi', 2, () => {});
},
hide() {
tcfapi('displayConsentUi', 2, () => {});
},
withdraw() {
// @TODO
},
}
tcfapi('addEventListener', 2, (tcData, success) => {
if (success && ['tcloaded', 'useractioncomplete'].includes(tcData.eventStatus)) {
window.gdsCmp.tcfMappedConsents = getTcfMappedConsentData(tcData);
runEvent();
}
});