dataLayer-allPages 161122
dataLayer-allPages 161122
/**********************
* DATALAYER ARCHITECTURE: SHOPIFY
* DEFINITION: A data layer helps you collect more accurate analytics data, that in
turn allows you to better understand what potential buyers are doing on your
website and where you can make improvements. It also reduces the time to implement
marketing tags on a website, and reduces the need for IT involvement, leaving them
to get on with implementing new features and fixing bugs.
* RESOURCES:
* http://www.datalayerdoctor.com/a-gentle-introduction-to-the-data-layer-for-
digital-marketers/
* http://www.simoahava.com/analytics/data-layer/
* EXTERNAL DEPENDENCIES:
* jQuery
* jQuery Cookie Plugin v1.4.1 - https://github.com/carhartl/jquery-cookie
* cartjs - https://github.com/discolabs/cartjs
/**********************
* PRELOADS
* load jquery if it doesn't exist
***********************/
if(!window.jQuery){
var jqueryScript = document.createElement('script');
jqueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/
3.6.0/jquery.min.js');
document.head.appendChild(jqueryScript);
}
__DL__jQueryinterval = setInterval(function(){
// wait for jQuery to load & run script after jQuery has loaded
if(window.jQuery){
// search parameters
getURLParams = function(name, url){
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
/**********************
* DYNAMIC DEPENDENCIES
***********************/
__DL__ = {
dynamicCart: false, // if cart is dynamic (meaning no refresh on cart
add) set to true
debug: false, // if true, console messages will be displayed
cart: null,
wishlist: null,
removeCart: null
};
customBindings = {
cartTriggers: [],
viewCart: [],
removeCartTrigger: [],
cartVisableSelector: [],
promoSubscriptionsSelectors: [],
promoSuccess: [],
ctaSelectors: [],
newsletterSelectors: [],
newsletterSuccess: [],
searchPage: [],
wishlistSelector: [],
removeWishlist: [],
wishlistPage: [],
searchTermQuery: [getURLParams('q')], // replace var with correct query
};
/* DO NOT EDIT */
defaultBindings = {
cartTriggers: ['form[action="/cart/add"] [type="submit"],.add-to-
cart,.cart-btn'],
viewCart: ['form[action="/cart"],.my-cart,.trigger-cart,#mobileCart'],
removeCartTrigger: ['[href*="/cart/change"]'],
cartVisableSelector: ['.inlinecart.is-active,.inline-cart.is-active'],
promoSubscriptionsSelectors: [],
promoSuccess: [],
ctaSelectors: [],
newsletterSelectors: ['input.contact_email'],
newsletterSuccess: ['.success_message'],
searchPage: ['search'],
wishlistSelector: [],
removeWishlist: [],
wishlistPage: []
};
// stitch bindings
objectArray = customBindings;
outputObject = __DL__;
applyBindings(customBindings, __DL__);
applyBindings(defaultBindings, __DL__);
/**********************
* PREREQUISITE LIBRARIES
***********************/
clearInterval(__DL__jQueryinterval);
// jquery-cookies.js
if(typeof $.cookie!==undefined){
(function(a){if(typeof define==='function'&&define.amd)
{define(['jquery'],a)}else if(typeof exports==='object')
{module.exports=a(require('jquery'))}else{a(jQuery)}}(function($){var
g=/\+/g;function encode(s){return h.raw?s:encodeURIComponent(s)}function decode(s)
{return h.raw?s:decodeURIComponent(s)}function stringifyCookieValue(a){return
encode(h.json?JSON.stringify(a):String(a))}function parseCookieValue(s)
{if(s.indexOf('"')===0){s=s.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/
g,'\\')}try{s=decodeURIComponent(s.replace(g,' '));return h.json?
JSON.parse(s):s}catch(e){}}function read(s,a){var b=h.raw?
s:parseCookieValue(s);return $.isFunction(a)?a(b):b}var h=$.cookie=function(a,b,c)
{if(arguments.length>1&&!$.isFunction(b)){c=$.extend({},h.defaults,c);if(typeof
c.expires==='number'){var d=c.expires,t=c.expires=new
Date();t.setMilliseconds(t.getMilliseconds()
+d*864e+5)}return(document.cookie=[encode(a),'=',stringifyCookieValue(b),c.expires?
'; expires='+c.expires.toUTCString():'',c.path?'; path='+c.path:'',c.domain?';
domain='+c.domain:'',c.secure?'; secure':''].join(''))}var e=a?undefined:
{},cookies=document.cookie?document.cookie.split('; '):
[],i=0,l=cookies.length;for(;i<l;i++){var
f=cookies[i].split('='),name=decode(f.shift()),cookie=f.join('=');if(a===name)
{e=read(cookie,b);break}if(!a&&(cookie=read(cookie))!==undefined)
{e[name]=cookie}}return e};h.defaults={};$.removeCookie=function(a,b)
{$.cookie(a,'',$.extend({},b,{expires:-1}));return!$.cookie(a)}}))}
/**********************
* Begin dataLayer Build
***********************/
/**********************
* DATALAYER SECTIONS
***********************/
'id' :
'shopify_BR_{{ product.id }}_{{ product.selected_or_first_available_variant.id }}',
'Item_group_id' : '{{product.id | json}}',
'product_id_sp' : '{{product.id | json}}',
'variant_id' :
{{product.selected_or_first_available_variant.id | json}},
'name' : {{product.title | json}},
'price' : {{product.price |
money_without_currency | replace: ',', '.' | json}},
'item_brand' : {{product.vendor | json}},
'category' : {{product.type | json}},
'item_list_name' : {{collection.title | json}},
'quantity' : '1',
},
{% endfor %}]
};
dataLayer.push({
'productList' : {{collection.title | json}},
'ecomm_pagetype' : 'Collection',
'pageType' : 'Collection',
'event' : 'view_item_list',
eventModel
});
{% endif %}
'id' :
'shopify_BR_{{ product.id }}_{{ product.selected_or_first_available_variant.id }}',
'Item_group_id' : '{{product.id | json}}',
'product_id_sp' : '{{product.id | json}}',
'ecomm_prodid' :
'shopify_BR_{{ product.id }}_{{ product.selected_or_first_available_variant.id }}',
'ecomm_totalvalue' : {{product.price | money_without_currency
| replace: ',', '.' | json}},
'ecomm_pagetype' : 'Product',
{% for variant in product.variants %}
'variant_id' : '{{variant.id | json}}',
'sku' : {{variant.sku | json}}
,{% endfor %}
'name' : {{product.title | json}},
'price' : {{product.price | money_without_currency |
replace: ',', '.' | json}},
'item_brand' : {{product.vendor | json}},
'category' : {{product.type | json}},
'google_business_vertical': 'retail',
'quantity' : '1',
}]
};
dataLayer.push({
'pageType' : 'Product',
'ecomm_prodid' :
'shopify_BR_{{ product.id }}_{{ product.selected_or_first_available_variant.id }}',
'ecomm_totalvalue' : {{product.price | money_without_currency |
replace: ',', '.' | json}},
'ecomm_pagetype' : 'Product',
'event' : 'view_item',
eventModel
});
$(__DL__.cartTriggers).click(function(){
dataLayer.push({
'ecomm_pagetype' : 'cart',
'event' : 'add_to_cart',
eventModel
});
});
{% endif %}
'event' : "search",
'item_list_name' : {{line_item.collection.title | json}},
eventModel
});
}
$(__DL__.removeCartTrigger).click(function(){
dataLayer.push({
'pageType' : "Remove from cart",
'ecomm_prodid' : '{{line_item.product.id | json}}',
'ecomm_totalvalue' : {{line_item.product.price |
money_without_currency | replace: ',', '.' | json}},
'ecomm_pagetype' : "Remove from cart",
'event' : 'remove_from_cart',
eventModel
});
});
{% endif %}