Skip to content

Commit 3e7df0b

Browse files
authored
Merge pull request #401 from torchbox/feature/vwo-script
[TWE-660] Add VWO code to base page
2 parents 0120177 + afd6b23 commit 3e7df0b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

tbx/project_styleguide/templates/patterns/base_page.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@
1313
<!-- End Google Tag Manager -->
1414
{% endif %}
1515

16+
{% if not request.in_preview_panel %}
17+
{% if request.COOKIES.torchbox_cookie != 'true' %}
18+
<template data-vwo-loader>
19+
{% endif %}
20+
<!-- Start VWO Async SmartCode -->
21+
<link rel="preconnect" href="https://dev.visualwebsiteoptimizer.com" />
22+
<script type='text/javascript' id='vwoCode'>
23+
window._vwo_code || (function() {
24+
var account_id=741605,
25+
version=2.1,
26+
settings_tolerance=2000,
27+
hide_element='body',
28+
hide_element_style = 'opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;transition:none !important;',
29+
/* DO NOT EDIT BELOW THIS LINE */
30+
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();})();
31+
</script>
32+
<!-- End VWO Async SmartCode -->
33+
{% if request.COOKIES.torchbox_cookie != 'true' %}
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>
40+
{% endif %}
41+
1642
{# favicons #}
1743
{% comment %}
1844
Generate favicons by http://realfavicongenerator.net and select the option to serve files up from the root of the site.

tbx/static_src/javascript/components/cookie-message.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ class CookieWarning {
55
return '[data-cookie-message]';
66
}
77

8+
static vwoSelector() {
9+
return '[data-vwo-loader]';
10+
}
11+
812
constructor(node) {
913
this.messageContainer = node;
1014
this.optInButton = this.messageContainer.querySelector(
@@ -13,10 +17,12 @@ class CookieWarning {
1317
this.optOutButton = this.messageContainer.querySelector(
1418
'[data-cookie-opt-out]',
1519
);
16-
this.cookieName = 'torchbox-cookie';
20+
this.cookieName = 'torchbox_cookie';
1721
this.cookieDuration = 365;
1822
this.activeClass = 'active';
1923
this.inactiveClass = 'inactive';
24+
this.template = document.querySelector(CookieWarning.vwoSelector());
25+
this.templateInitiated = false;
2026

2127
this.checkCookie();
2228
this.bindEvents();
@@ -43,6 +49,18 @@ class CookieWarning {
4349
Cookies.set(this.cookieName, cookieValue, {
4450
expires: this.cookieDuration,
4551
});
52+
// Only allow VWO to be enabled if cookies have been accepted
53+
if (cookieValue) {
54+
window.VWO.init(1);
55+
window.VWO.push(['optInVisitor']);
56+
if (!this.templateInitiated) {
57+
this.templateInitiated = true;
58+
this.template.after(this.template.content.cloneNode(true));
59+
}
60+
} else {
61+
window.VWO.init(3);
62+
window.VWO.push(['optOutVisitor']);
63+
}
4664
}
4765

4866
bindEvents() {

0 commit comments

Comments
 (0)