|
40 | 40 |
|
41 | 41 | <script src="https://cdn.tailwindcss.com?plugins=forms,aspect-ratio"></script>
|
42 | 42 |
|
43 |
| - <script |
44 |
| - async |
45 |
| - class="elbwalker" |
46 |
| - src="https://cdn.jsdelivr.net/npm/@elbwalker/walker.js@latest/dist/walker.js" |
47 |
| - ></script> |
48 |
| - |
49 | 43 | <script type="module">
|
50 |
| - window.elb = (...args) => { |
51 |
| - (window.elbLayer = window.elbLayer || []).push(...args); |
52 |
| - }; |
53 |
| - |
54 |
| - // Configuration |
55 |
| - elb('walker config', { tagging: 2 }); |
| 44 | + // Consent management |
| 45 | + // This code simulates a CMPs functionality |
56 | 46 |
|
57 |
| - // Utils |
58 |
| - window.utils = await import( |
| 47 | + const consentKey = 'consentState'; |
| 48 | + const utils = await import( |
59 | 49 | 'https://cdn.jsdelivr.net/npm/@elbwalker/utils@latest/dist/index.mjs'
|
60 | 50 | );
|
61 | 51 |
|
62 |
| - // Start |
63 |
| - elb('walker run'); |
64 |
| - |
65 |
| - // Session handling |
66 |
| - const session = utils.sessionStart(); |
67 |
| - if (session) elb('session start', session); |
68 |
| - |
69 |
| - // Destination Console |
70 |
| - elb('walker destination', { |
71 |
| - push: console.log, |
72 |
| - }); |
73 |
| - |
74 |
| - // Destination GTM |
75 |
| - const gtm = ( |
76 |
| - await import( |
77 |
| - 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-google-gtm@latest/dist/index.mjs' |
78 |
| - ) |
79 |
| - ).default; |
80 |
| - elb('walker destination', gtm, { |
81 |
| - loadScript: true, |
82 |
| - custom: { |
83 |
| - containerId: 'GTM-WCFR4W5', |
84 |
| - }, |
85 |
| - }); |
86 |
| - |
87 |
| - // Destination Console Consent |
88 |
| - elb( |
89 |
| - 'walker destination', |
90 |
| - { push: (event) => console.log('CONSENT', event) }, |
91 |
| - { consent: { functional: true } }, |
92 |
| - ); |
93 |
| - |
94 |
| - // Destination API |
95 |
| - const api = ( |
96 |
| - await import( |
97 |
| - 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-api@latest/dist/index.mjs' |
98 |
| - ) |
99 |
| - ).default; |
100 |
| - elb('walker destination', api, { |
101 |
| - consent: { functional: true }, |
102 |
| - custom: { |
103 |
| - // Server-side tagmanager |
104 |
| - url: 'https://server-side-tagging-ybbfvkxhia-ey.a.run.app/quokka', |
105 |
| - transport: 'beacon', |
106 |
| - }, |
107 |
| - }); |
108 |
| - |
109 |
| - // Destination GA4 |
110 |
| - const ga4 = ( |
111 |
| - await import( |
112 |
| - 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-google-ga4@latest/dist/index.mjs' |
113 |
| - ) |
114 |
| - ).default; |
115 |
| - elb('walker destination', ga4, { |
116 |
| - consent: { marketing: true, ga4: true }, |
117 |
| - custom: { measurementId: 'G-4WP1Y3GPLW' }, |
118 |
| - loadScript: true, |
119 |
| - }); |
120 |
| - |
121 |
| - // Consent |
122 |
| - // Code snippets for consent choice |
123 |
| - // Simulate the CMPs functionality |
124 | 52 | window.consentAccept = () => {
|
125 |
| - window.utils.storageWrite('consentState', 'accepted'); |
126 |
| - consentGranted(); |
127 |
| - consentRefresh(); |
| 53 | + // Save granted choice |
| 54 | + utils.storageWrite(consentKey, 'accepted'); |
| 55 | + consentRefresh(true); |
128 | 56 | };
|
129 | 57 | window.consentDeny = () => {
|
130 |
| - window.utils.storageWrite('consentState', 'denied'); |
131 |
| - consentDenied(); |
| 58 | + // Save denied choice |
| 59 | + utils.storageWrite(consentKey, 'denied'); |
132 | 60 | consentRefresh();
|
133 | 61 | };
|
134 | 62 | window.consentReset = () => {
|
135 |
| - window.utils.storageDelete('consentState'); |
| 63 | + // Reset choice |
| 64 | + utils.storageDelete(consentKey); |
136 | 65 | consentRefresh();
|
137 | 66 | };
|
138 | 67 |
|
139 |
| - window.consentGranted = () => { |
140 |
| - // Set session and device ids |
141 |
| - elb('walker user', getUser()); |
142 |
| - |
143 |
| - // Allow sending events to functional and marketing destinations |
144 |
| - elb('walker consent', { functional: true, marketing: true }); |
145 |
| - }; |
146 |
| - |
147 |
| - window.consentDenied = () => { |
148 |
| - // Only set a random session id, but no persistant device id |
149 |
| - elb('walker user', { id: 'anonymous', device: undefined }); |
150 |
| - |
151 |
| - // Allow sending events to functional destinations at most |
152 |
| - elb('walker consent', { functional: true }); |
153 |
| - }; |
154 |
| - |
155 |
| - function getUser() { |
156 |
| - const idKey = 'elb_user_id'; |
157 |
| - const sessionKey = 'elb_user_session'; |
158 |
| - const deviceKey = 'elb_user_device'; |
159 |
| - var id = window.utils.storageRead(idKey); |
160 |
| - var session = window.utils.storageRead(sessionKey); |
161 |
| - var device = window.utils.storageRead(deviceKey); |
162 |
| - |
163 |
| - if (!id) { |
164 |
| - id = window.utils.getId(5); |
165 |
| - window.utils.storageWrite(idKey, id); |
166 |
| - } |
167 |
| - |
168 |
| - if (!session) { |
169 |
| - session = window.utils.getId(10); |
170 |
| - window.utils.storageWrite(sessionKey, session); |
| 68 | + // Code that gets called after a consent choice was made |
| 69 | + function consentCode(granted = false) { |
| 70 | + const functional = true; |
| 71 | + var marketing = false; |
| 72 | + |
| 73 | + if (granted) { |
| 74 | + // User granted consent |
| 75 | + |
| 76 | + // Set a user id (device and session ids are set by walker.js automatically) |
| 77 | + const key = 'elbUserId'; |
| 78 | + const id = |
| 79 | + // Read existing id or generate and save a new one |
| 80 | + utils.storageRead(key) || utils.storageWrite(key, utils.getId(5)); |
| 81 | + elb('walker user', { id }); |
| 82 | + |
| 83 | + marketing = true; // Granted marketing stage |
| 84 | + } else { |
| 85 | + // User declined consent |
| 86 | + |
| 87 | + // Use an anonymous user id and reset others |
| 88 | + elb('walker user', { |
| 89 | + id: 'anonymous', |
| 90 | + device: undefined, |
| 91 | + session: undefined, |
| 92 | + }); |
171 | 93 | }
|
172 | 94 |
|
173 |
| - if (!device) { |
174 |
| - device = window.utils.getId(8); |
175 |
| - window.utils.storageWrite(deviceKey, device); |
176 |
| - elb('first visit', { device }); |
177 |
| - } |
178 |
| - |
179 |
| - return { |
180 |
| - id, |
181 |
| - session, |
182 |
| - device, |
183 |
| - }; |
| 95 | + // Update walkers consent state |
| 96 | + elb('walker consent', { functional, marketing }); |
184 | 97 | }
|
185 | 98 |
|
| 99 | + // Update the consent state |
186 | 100 | function consentRefresh() {
|
187 |
| - var consentStateElem = document.getElementById('consent_state'); |
188 |
| - var consentState = |
189 |
| - window.utils.storageRead('consentState') || 'unknown'; |
| 101 | + // Look for a stored consent choice |
| 102 | + const consentState = utils.storageRead(consentKey); |
190 | 103 |
|
191 |
| - if (consentState == 'accepted') consentGranted(); |
192 |
| - if (consentState == 'denied') consentDenied(); |
| 104 | + // If a choice was made execute the code |
| 105 | + if (consentState) consentCode(consentState == 'accepted'); |
193 | 106 |
|
194 |
| - consentStateElem.innerText = consentState; |
| 107 | + // Display the status |
| 108 | + document.getElementById('consent_state').innerText = |
| 109 | + consentState || 'unknown'; |
195 | 110 | }
|
196 | 111 |
|
| 112 | + // Check consent state initially |
197 | 113 | consentRefresh();
|
| 114 | + |
| 115 | + // Make Utils globally available |
| 116 | + window.utils = utils; |
| 117 | + </script> |
| 118 | + |
| 119 | + <script type="module"> |
| 120 | + // elb |
| 121 | + window.elb = function () { |
| 122 | + (window.elbLayer = window.elbLayer || []).push(arguments); |
| 123 | + }; |
| 124 | + |
| 125 | + // Add a destination that logs events in the console |
| 126 | + elb('walker destination', { |
| 127 | + push: console.log, |
| 128 | + }); |
| 129 | + |
| 130 | + // Demo with GTM setup |
| 131 | + let gtm = new URL(document.location).searchParams.get('gtm'); // Option for custom Container |
| 132 | + if (gtm === '') gtm = 'GTM-WCFR4W5'; // Demo container |
| 133 | + if (gtm) { |
| 134 | + // Typical GTM install script |
| 135 | + (function (w, d, s, l, i) { |
| 136 | + w[l] = w[l] || []; |
| 137 | + w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' }); |
| 138 | + var f = d.getElementsByTagName(s)[0], |
| 139 | + j = d.createElement(s), |
| 140 | + dl = l != 'dataLayer' ? '&l=' + l : ''; |
| 141 | + j.async = true; |
| 142 | + j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; |
| 143 | + f.parentNode.insertBefore(j, f); |
| 144 | + })(window, document, 'script', 'dataLayer', gtm); |
| 145 | + } else { |
| 146 | + // Demo with custom code setup |
| 147 | + |
| 148 | + // Load walker.js |
| 149 | + const Walkerjs = await import( |
| 150 | + 'https://cdn.jsdelivr.net/npm/@elbwalker/[email protected]/dist/index.mjs' |
| 151 | + ); |
| 152 | + |
| 153 | + // Create a walker.js instance with custom config |
| 154 | + window.walkerjs = Walkerjs.default({ |
| 155 | + run: true, // Automatically start running |
| 156 | + session: { |
| 157 | + // Enable session detection and the usage of user ids |
| 158 | + consent: 'marketing', // Require marketing consent for storage access |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + // Change configuration via elb command |
| 163 | + elb('walker config', { tagging: 2 }); |
| 164 | + |
| 165 | + // Add a Console destination that requires functional consent |
| 166 | + elb( |
| 167 | + 'walker destination', |
| 168 | + { push: (event) => console.log('CONSENT', event) }, |
| 169 | + { consent: { functional: true } }, |
| 170 | + ); |
| 171 | + |
| 172 | + // Disabled GTM destination |
| 173 | + // const destinationGTM = ( |
| 174 | + // await import( |
| 175 | + // 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-google-gtm@latest/dist/index.mjs' |
| 176 | + // ) |
| 177 | + // ).default; |
| 178 | + // elb('walker destination', destinationGTM, { |
| 179 | + // loadScript: true, |
| 180 | + // custom: { |
| 181 | + // containerId: 'GTM-WCFR4W5', |
| 182 | + // }, |
| 183 | + // }); |
| 184 | + |
| 185 | + // Destination API |
| 186 | + const api = ( |
| 187 | + await import( |
| 188 | + 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-api@latest/dist/index.mjs' |
| 189 | + ) |
| 190 | + ).default; |
| 191 | + elb('walker destination', api, { |
| 192 | + consent: { functional: true }, |
| 193 | + custom: { |
| 194 | + // Server-side Tag Manager |
| 195 | + url: 'https://server-side-tagging-ybbfvkxhia-ey.a.run.app/quokka', |
| 196 | + transport: 'beacon', |
| 197 | + }, |
| 198 | + }); |
| 199 | + |
| 200 | + // Destination GA4 |
| 201 | + const ga4 = ( |
| 202 | + await import( |
| 203 | + 'https://cdn.jsdelivr.net/npm/@elbwalker/destination-web-google-ga4@latest/dist/index.mjs' |
| 204 | + ) |
| 205 | + ).default; |
| 206 | + elb('walker destination', ga4, { |
| 207 | + consent: { marketing: true, ga4: true }, // Require consent |
| 208 | + custom: { measurementId: 'G-4WP1Y3GPLW' }, |
| 209 | + loadScript: true, |
| 210 | + }); |
| 211 | + } |
198 | 212 | </script>
|
199 | 213 | </head>
|
200 | 214 | <body class="relative">
|
|
0 commit comments