-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAjsOneTrustWrapper.html
200 lines (186 loc) · 5.83 KB
/
AjsOneTrustWrapper.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
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
<html>
<head>
<!-- Form: WriteKey -->
<form method="get">
<input type="text" name="writeKey" placeholder="Writekey" />
<button>Load</button>
</form>
<script>
const { searchParams } = new URL(document.location);
const writeKey = searchParams.get("writeKey");
document.querySelector("input").value = writeKey;
</script>
<!-- Form: Clear OneTrust Cookie -->
<button id="clear-ot-cookies">Clear OneTrust Cookies & Reload</button>
<script>
document.getElementById('clear-ot-cookies').addEventListener('click', () => {
['OptanonConsent', 'OptanonAlertBoxClosed'].forEach((name) => {
document.cookie =
name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
});
window.location.reload();
console.log("OneTrust Cookies cleared.");
})
</script>
<!-- OneTrust Vendor Script -->
<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" type="text/javascript"
data-domain-script="80ca7b5c-e72f-4bd0-972a-b74d052a0820-test"></script>
<!-- OneTrust Wrapper -->
<script
src="/node_modules/@segment/analytics-consent-wrapper-onetrust/dist/umd/analytics-onetrust.umd.development.js">
</script>
<!-- Segment Snippet (Modified)-->
<script>
if (writeKey) {
!(function () {
var analytics = (window.analytics = window.analytics || [])
if (!analytics.initialize)
if (analytics.invoked)
window.console &&
console.error &&
console.error('Segment snippet included twice.')
else {
analytics.invoked = !0
analytics.methods = [
'screen',
'register',
'deregister',
'trackSubmit',
'trackClick',
'trackLink',
'trackForm',
'pageview',
'identify',
'reset',
'group',
'track',
'ready',
'alias',
'debug',
'page',
'once',
'off',
'on',
'addSourceMiddleware',
'addIntegrationMiddleware',
'setAnonymousId',
'addDestinationMiddleware',
'emit'
]
analytics.factory = function (e) {
return function () {
if (window.analytics.initialized) {
// Sometimes users assigned analytics to a variable before analytics is done loading, resulting in a stale reference.
// If so, proxy any calls to the 'real' analytics instance.
return window.analytics[e].apply(window.analytics, arguments);
}
var t = Array.prototype.slice.call(arguments)
t.unshift(e)
analytics.push(t)
return analytics
}
}
for (var e = 0; e < analytics.methods.length; e++) {
var key = analytics.methods[e]
analytics[key] = analytics.factory(key)
}
analytics.load = function (key, e) {
var t = document.createElement('script')
t.type = 'text/javascript'
t.async = !0
t.src = '/node_modules/@segment/analytics-next/dist/umd/standalone.js'
var n = document.getElementsByTagName('script')[0]
n.parentNode.insertBefore(t, n)
analytics._loadOptions = e
}
analytics.SNIPPET_VERSION = '4.13.1'
analytics._writeKey = writeKey
withOneTrust(analytics).load()
analytics.page()
}
})()
}
</script>
</head>
<body>
<form>
<textarea name="event" id="event">
{
"name": "hi",
"properties": { },
"traits": { },
"options": { }
}
</textarea>
<div>
<button id="track">Track</button>
<button id="identify">Identify</button>
</div>
</form>
<h2>Consent Changed Event</h2>
<pre id="consent-changed"></pre>
<h2>Logs</h2>
<pre id="logs"></pre>
<script type="text/javascript">
const displayConsentLogs = (str) => document.querySelector('#consent-changed').textContent = str
analytics.on('track', (name, properties, options) => {
if (name.includes("Segment Consent")) {
displayConsentLogs("Consent Changed Event Fired")
setTimeout(() => displayConsentLogs(''), 3000)
}
})
const displayRegularLogs = (str) => document.querySelector('#logs').textContent = str
const logEvent = (promise) => {
if (Array.isArray(promise)) {
displayRegularLogs('Analytics is not initialized!')
setTimeout(() => displayRegularLogs(''), 5000)
return
}
if (promise) {
promise.then((ctx) => {
ctx.flush()
displayRegularLogs(JSON.stringify(
ctx.event,
null,
2
))
})
}
}
document.querySelector('#track').addEventListener('click', async (e) => {
e.preventDefault()
const contents = document.querySelector('#event').value
const evt = JSON.parse(contents)
const promise = window.analytics.track(
evt.name ?? '',
evt.properties ?? {},
evt.options ?? {}
)
logEvent(promise)
})
document
.querySelector('#identify')
.addEventListener('click', async (e) => {
e.preventDefault()
const contents = document.querySelector('#event').value
const evt = JSON.parse(contents)
const promise = window.analytics.identify(
evt.name ?? '',
evt.properties ?? {},
evt.options ?? {}
)
logEvent(promise)
})
</script>
<style>
body {
font-family: monospace;
}
#event {
margin: 2em 0;
min-height: 200px;
min-width: 700px;
}
</style>
</body>
</html>