-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonetrust.html
98 lines (82 loc) · 3.51 KB
/
onetrust.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Segment Integration Example</title>
<script>
const SEGMENT_WRITE_KEY = "G5BwWiwo7E5XCw85h43neqDn59j6KCTp";
const DISABLE_DESTINATIONS_BY_DEFAULT = true;
let previousOneTrustGroupIds = [];
const ONE_TRUST_SEGMENT_MAPPING = {
C0002: ["A/B Testing", "Feature Flagging", "Live Chat"],
C0003: ["Analytics", "Raw Data"],
C0004: ["Email", "Email Marketing", "CRM"]
};
function getConsentGroups() {
if (!window.OnetrustActiveGroups) {
console.log("No OneTrust groups have been consented to");
return [];
}
const oneTrustActiveGroupArray = window.OnetrustActiveGroups.split(",").filter(e => e.startsWith("C"));
previousOneTrustGroupIds = oneTrustActiveGroupArray;
localStorage.setItem('onetrust_active_groups', JSON.stringify(oneTrustActiveGroupArray));
return oneTrustActiveGroupArray;
}
async function getConsentedIntegrations(enabledIntegrations, oneTrustGroupIds) {
const segmentCategories = oneTrustGroupIds.map(oneTrustGroupId => ONE_TRUST_SEGMENT_MAPPING[oneTrustGroupId])
.filter(segmentCategory => segmentCategory)
.flat(1);
const consentedIntegrations = enabledIntegrations.filter(enabledIntegration => {
return segmentCategories.includes(enabledIntegration.category);
});
return consentedIntegrations;
}
function shouldLoadAjs(oneTrustActiveGroups) {
return oneTrustActiveGroups.includes("C0002") || oneTrustActiveGroups.includes("C0003") || oneTrustActiveGroups.includes("C0004");
}
async function fetchDestinationForWriteKey(writeKey) {
if (!writeKey) {
return [];
}
const res = await window.fetch(`https://cdn.segment.com/v1/projects/${writeKey}/integrations`);
if (!res.ok) {
throw new Error(`Failed to fetch integrations for write key ${writeKey}: HTTP ${res.status} ${res.statusText}`);
}
const destinations = await res.json();
for (const destination of destinations) {
destination.id = destination.creationName;
delete destination.creationName;
}
return destinations;
}
async function loadAnalytics() {
const enabledIntegrations = await fetchDestinationForWriteKey(SEGMENT_WRITE_KEY);
const oneTrustGroupIds = getConsentGroups();
const consentedIntegrations = await getConsentedIntegrations(enabledIntegrations, oneTrustGroupIds);
const destinationPreferences = DISABLE_DESTINATIONS_BY_DEFAULT
? { "All": false, "Segment.io": true }
: {};
consentedIntegrations.forEach(consentedIntegration => {
destinationPreferences[consentedIntegration.id] = true;
});
const consentedIntegrationArray = [];
for (const value in consentedIntegrations) {
consentedIntegrationArray.push(consentedIntegrations[value].name);
}
enabledIntegrations.forEach(integration => {
if (!consentedIntegrationArray.includes(integration.name)) {
destinationPreferences[integration.name] = false;
}
});
const loadAjs = shouldLoadAjs(oneTrustGroupIds);
if (getConsentGroupChange()) {
console.log("fire");
}
if (loadAjs) {
window.destinationPreferences = destinationPreferences;
window.analytics.load(SEGMENT_WRITE_KEY, {
"integrations": destinationPreferences
});
} else {
console.log