<script>
//30 min cookie
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (30*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
if ({{session_id_cookie}}) {
setCookie('sessionID', {{session_id_cookie}});
} else {
setCookie('sessionID', {{session_id}});
}
//if first pageview
if (!{{start_session}}) {
//first step for main funnel
if ({{Page Path}} == '/') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '1. main page', 'eventLabel': ''})
setCookie('funnel_type', 'main');
setCookie('previous_page', 'main');
}
//first step for category or search funnel
if ({{pageType}} == 'Category' || {{Page Path}} == '/search/new') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_cat_search', 'eventAction': '1. cat or search page', 'eventLabel': ''})
setCookie('funnel_type', 'category/search');
setCookie('previous_page', 'category/search');
}
//first step for product funnel
if ({{pageType}} == 'Product') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_product', 'eventAction': '1. product page', 'eventLabel': ''})
setCookie('funnel_type', 'product');
setCookie('previous_page', 'product');
}
setCookie('start_session', 1);
//if second or more pageviews
} else {
//second step for main funnel
if ({{pageType}} == 'Category' || {{Page Path}} == '/search/new') {
if ({{funnel_type}} == 'main' && {{previous_page}} == 'main') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '2. category page', 'eventLabel': ''})
setCookie('previous_page', 'category/search');
}
}
if ({{pageType}} == 'Product') {
if ({{funnel_type}} == 'main' && {{previous_page}} == 'category/search') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '3. product page', 'eventLabel': ''})
setCookie('previous_page', 'product');
}
if ({{funnel_type}} == 'category/search' && {{previous_page}} == 'category/search') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_cat_search', 'eventAction': '2. product page', 'eventLabel': ''})
setCookie('previous_page', 'product');
}
}
if ({{Page Path}} == '/moyobasket/basket.html' && {{previous_page}} == 'product') {
if ({{funnel_type}} == 'main') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '4. cart page', 'eventLabel': ''})
setCookie('previous_page', 'cart');
}
if ({{funnel_type}} == 'category/search' && {{previous_page}} == 'product') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_cat_search', 'eventAction': '3. cart page', 'eventLabel': ''})
setCookie('previous_page', 'cart');
}
if ({{funnel_type}} == 'product' && {{previous_page}} == 'product') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_product', 'eventAction': '2. cart page', 'eventLabel': ''})
setCookie('previous_page', 'cart');
}
}
if ({{Page Path}} == '/moyobasket/proceed.html') {
if ({{funnel_type}} == 'main' && {{previous_page}} == 'cart') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '5. checkout page', 'eventLabel': ''})
setCookie('previous_page', 'checkout');
}
if ({{funnel_type}} == 'category/search' && {{previous_page}} == 'cart') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_cat_search', 'eventAction': '4. checkout page', 'eventLabel': ''})
setCookie('previous_page', 'checkout');
}
if ({{funnel_type}} == 'product' && {{previous_page}} == 'cart') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_product', 'eventAction': '3. checkout page', 'eventLabel': ''})
setCookie('previous_page', 'checkout');
}
}
if ({{pageType}} == 'ThankYouPage') {
if ({{funnel_type}} == 'main' && {{previous_page}} == 'checkout') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_main', 'eventAction': '6. thank you page', 'eventLabel': ''})
}
if ({{funnel_type}} == 'category/search' && {{previous_page}} == 'checkout') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_cat_search', 'eventAction': '5. thank you page', 'eventLabel': ''})
}
if ({{funnel_type}} == 'product' && {{previous_page}} == 'checkout') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'funnel_product', 'eventAction': '4. thank you page', 'eventLabel': ''})
}
}
//if user exits from funnel
if ({{pageType}} != 'ThankYouPage' && {{Page Path}} != '/moyobasket/proceed.html' && {{Page Path}} != '/moyobasket/basket.html' &&{{pageType}} != 'Product' && {{pageType}} != 'Category' && {{Page Path}} != '/search/new' && {{previous_page}} != 'other') {
dataLayer.push({'event': 'autoEvent', 'eventCategory': 'exitfunnel_'+{{funnel_type}}, 'eventAction': {{Page Path}}, 'eventLabel': {{previous_page}}})
setCookie('previous_page', 'other');
}
//increment, count of pages
setCookie('start_session', parseInt({{start_session}}) + 1);
}
</script>