Skip to content

Commit c29aeb7

Browse files
Add cookie check for VWO script loading
1 parent b759b05 commit c29aeb7

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

tbx/project_styleguide/templates/patterns/base_page.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
{% endif %}
1515

1616
{% if not request.in_preview_panel %}
17+
{% if request.COOKIES.VWOCookie != 'Yes' %}
18+
<template data-vwo-loader>
19+
{% endif %}
1720
<!-- Start VWO Async SmartCode -->
1821
<link rel="preconnect" href="https://dev.visualwebsiteoptimizer.com" />
1922
<script type='text/javascript' id='vwoCode'>
@@ -27,6 +30,13 @@
2730
f=false,w=window,d=document,v=d.querySelector('#vwoCode'),cK='_vwo_'+account_id+'_settings',cc={};try{var c=JSON.parse(localStorage.getItem('_vwo_'+account_id+'_config'));cc=c&&typeof c==='object'?c:{}}catch(e){}var stT=cc.stT==='session'?w.sessionStorage:w.localStorage;code={nonce:v&&v.nonce,library_tolerance:function(){return typeof library_tolerance!=='undefined'?library_tolerance:undefined},settings_tolerance:function(){return cc.sT||settings_tolerance},hide_element_style:function(){return'{'+(cc.hES||hide_element_style)+'}'},hide_element:function(){if(performance.getEntriesByName('first-contentful-paint')[0]){return''}return typeof cc.hE==='string'?cc.hE:hide_element},getVersion:function(){return version},finish:function(e){if(!f){f=true;var t=d.getElementById('_vis_opt_path_hides');if(t)t.parentNode.removeChild(t);if(e)(new Image).src='https://dev.visualwebsiteoptimizer.com/ee.gif?a='+account_id+e}},finished:function(){return f},addScript:function(e){var t=d.createElement('script');t.type='text/javascript';if(e.src){t.src=e.src}else{t.text=e.text}v&&t.setAttribute('nonce',v.nonce);d.getElementsByTagName('head')[0].appendChild(t)},load:function(e,t){var n=this.getSettings(),i=d.createElement('script'),r=this;t=t||{};if(n){i.textContent=n;d.getElementsByTagName('head')[0].appendChild(i);if(!w.VWO||VWO.caE){stT.removeItem(cK);r.load(e)}}else{var o=new XMLHttpRequest;o.open('GET',e,true);o.withCredentials=!t.dSC;o.responseType=t.responseType||'text';o.onload=function(){if(t.onloadCb){return t.onloadCb(o,e)}if(o.status===200||o.status===304){_vwo_code.addScript({text:o.responseText})}else{_vwo_code.finish('&e=loading_failure:'+e)}};o.onerror=function(){if(t.onerrorCb){return t.onerrorCb(e)}_vwo_code.finish('&e=loading_failure:'+e)};o.send()}},getSettings:function(){try{var e=stT.getItem(cK);if(!e){return}e=JSON.parse(e);if(Date.now()>e.e){stT.removeItem(cK);return}return e.s}catch(e){return}},init:function(){if(d.URL.indexOf('__vwo_disable__')>-1)return;var e=this.settings_tolerance();w._vwo_settings_timer=setTimeout(function(){_vwo_code.finish();stT.removeItem(cK)},e);var t;if(this.hide_element()!=='body'){t=d.createElement('style');var n=this.hide_element(),i=n?n+this.hide_element_style():'',r=d.getElementsByTagName('head')[0];t.setAttribute('id','_vis_opt_path_hides');v&&t.setAttribute('nonce',v.nonce);t.setAttribute('type','text/css');if(t.styleSheet)t.styleSheet.cssText=i;else t.appendChild(d.createTextNode(i));r.appendChild(t)}else{t=d.getElementsByTagName('head')[0];var i=d.createElement('div');i.style.cssText='z-index: 2147483647 !important;position: fixed !important;left: 0 !important;top: 0 !important;width: 100% !important;height: 100% !important;background: white !important;display: block !important;';i.setAttribute('id','_vis_opt_path_hides');i.classList.add('_vis_hide_layer');t.parentNode.insertBefore(i,t.nextSibling)}var o=window._vis_opt_url||d.URL,s='https://dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(o)+'&vn='+version;if(w.location.search.indexOf('_vwo_xhr')!==-1){this.addScript({src:s})}else{this.load(s+'&x=true')}}};w._vwo_code=code;code.init();})();
2831
</script>
2932
<!-- End VWO Async SmartCode -->
33+
{% if request.COOKIES.VWOCookie != 'Yes' %}
34+
</template>
35+
{% endif %}
36+
<script>
37+
window.VWO = window.VWO || [];
38+
window.VWO.init = window.VWO.init || function(state) { window.VWO.consentState = state; }
39+
</script>
3040
{% endif %}
3141

3242
{# favicons #}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const getCookie = (name) => {
2+
const value = `; ${document.cookie}`;
3+
const parts = value.split(`; ${name}=`);
4+
if (parts.length === 2) return parts.pop().split(';').shift();
5+
return null;
6+
};
7+
8+
class VWOLoader {
9+
static selector() {
10+
return '[data-vwo-loader]';
11+
}
12+
13+
constructor() {
14+
this.template = document.querySelector(VWOLoader.selector());
15+
if (!this.template) return;
16+
this.initiated = false;
17+
this.bindEvents();
18+
}
19+
20+
bindEvents() {
21+
window.addEventListener('CassieSubmittedConsent', () =>
22+
this.toggleVWO(),
23+
);
24+
}
25+
26+
toggleVWO() {
27+
const isEnabled = getCookie('VWOCookie') === 'Yes';
28+
if (isEnabled) {
29+
window.VWO.init(1);
30+
window.VWO.push(['optInVisitor']);
31+
if (!this.initiated) {
32+
this.initiated = true;
33+
// add the template contents after the template node
34+
this.template.after(this.template.content.cloneNode(true));
35+
}
36+
} else {
37+
window.VWO.init(3);
38+
window.VWO.push(['optOutVisitor']);
39+
}
40+
}
41+
}
42+
43+
export default VWOLoader;

tbx/static_src/javascript/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import YouTubeConsentManager from './components/youtube-embed';
99
import Tabs from './components/tabs';
1010
import TableHint from './components/table-hint';
1111
import ModeSwitcher from './components/mode-switcher';
12+
import VWOLoader from './components/vwo-loader';
1213

1314
// IE11 polyfills
1415
import foreachPolyfill from './polyfills/foreach-polyfill';
@@ -40,4 +41,5 @@ document.addEventListener('DOMContentLoaded', () => {
4041
initComponent(TableHint);
4142
initComponent(ModeSwitcher);
4243
new DesktopCloseMenus();
44+
new VWOLoader();
4345
});

0 commit comments

Comments
 (0)