0% found this document useful (0 votes)
151 views7 pages

dataLayer-allPages 161122

Uploaded by

Melissa Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views7 pages

dataLayer-allPages 161122

Uploaded by

Melissa Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

<script>

/**********************
* 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.

* FORKED FROM: https://github.com/TechnicalWebAnalytics/dataLayer-shopify

* AUTHOR: Paolo Bietolini {


website: paolobietolini.xyz || analyticsitalia.it,
linkedin: /in/paolobietolini/,
github: paolobtl
}

* 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

* DataLayer Architecture: Shopify v1.2


* COPYRIGHT 2021
* LICENSES: MIT ( https://opensource.org/licenses/MIT )
*/

/**********************
* 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 = function(objectArray, outputObject){


for (var x in objectArray) {
var key = x;
var objs = objectArray[x];
values = [];
if(objs.length > 0){
values.push(objs);
if(key in outputObject){
values.push(outputObject[key]);
outputObject[key] = values.join(", ");
}else{
outputObject[key] = values.join(", ");
}
}
}
};

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
***********************/

window.dataLayer = window.dataLayer || []; // init data layer if doesn't


already exist

/**********************
* DATALAYER SECTIONS
***********************/

/** DATALAYER: Product List Page (Collections, Category)


* Fire on all product listing pages. */
{% if template contains 'collection' %}
var eventModel = {
'items': [
{% for product in collection.products %}{

'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}},

{% for product in collection.products %}


'ecomm_prodid' :
'shopify_BR_{{ product.id }}_{{ product.selected_or_first_available_variant.id }}',
'ecomm_totalvalue' :
{{product.selected_or_first_available_variant.price | money_without_currency |
replace: ',', '.' | json}}
,{% endfor %}

'ecomm_pagetype' : 'Collection',
'pageType' : 'Collection',
'event' : 'view_item_list',
eventModel
});
{% endif %}

/** DATALAYER: Product Page


* Fire on all Product View pages. */
{% if template contains 'product' %}
var eventModel = {
'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',
'items': [{

'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 %}

/** DATALAYER: Cart View


* Fire anytime a user views their cart (non-dynamic) */
{% if template contains 'cart' %}
var eventModel = {
'currency': {{cart.currency.iso_code | json}},
'value': {{cart.total_price | divided_by: 100.0 | json}},
'items':[{% for line_item in cart.items %}{
'id' : 'shopify_BR_{{line_item.product.id |
json}}_{{line_item.variant.id | json}}',
'Item_group_id' : '{{line_item.product.id | json}}',
'product_id_sp' : '{{line_item.product.id | json}}',
'item_variant' : {{line_item.variant.title | json}},
'variant_id' : {{line_item.variant.id | json}},
'name' : {{line_item.product.title | json}},
'price' : {{line_item.product.price |
money_without_currency | replace: ',', '.' | json}},
'item_brand' : {{line_item.product.vendor | json}},
'category' : {{line_item.product.type | json}},
'item_list_name' : {{line_item.collection.title | json}},
'quantity' : {{line_item.quantity | json}},
'discount' : {{discount.code | json}},
'google_business_vertical': 'retail'
},{% endfor %}],
};
dataLayer.push({
'pageType' : 'Cart',

{% for line_item in cart.items %}


'ecomm_prodid' : 'shopify_BR_{{line_item.product.id |
json}}_{{line_item.variant.id | json}}',
'ecomm_totalvalue' : {{line_item.product.price |
money_without_currency | replace: ',', '.' | json}}
,{% endfor %}
'ecomm_pagetype' : 'Cart',
'event' : 'view_cart',
eventModel
});
{% endif %}

/** DOM Ready **/


$(document).ready(function() {

/** DATALAYER: Search Results */


var searchPage = new RegExp(__DL__.searchPage, "g");
if(document.location.pathname.match(searchPage)){
var eventModel = {
items :[{% for product in search.results %}{
'id' : '{{product.id | json}}',
'product_id_sp' : '{{product.id | json}}',
'item_variant' : {{product.variant.title | 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' : {{line_item.variant.id | json}},
},{% endfor %}],
};
dataLayer.push({
'pageType' : "Search",
'ecomm_prodid' : '{{product.id | json}}',
'ecomm_totalvalue' : {{product.price | money_without_currency
| replace: ',', '.' | json}},
'ecomm_pagetype' : "Search",
'search_term' : __DL__.searchTermQuery,

'event' : "search",
'item_list_name' : {{line_item.collection.title | json}},
eventModel
});
}

/** DATALAYER: Remove From Cart **/


{% if template contains 'cart' %}
var eventModel = {
'items':[{% for line_item in cart.items %}{
'id' : '{{line_item.product.id | json}}',
'product_id_sp' : '{{line_item.product.id | json}}',
'item_variant' : {{line_item.variant.title | json}},

'name' : {{line_item.product.title | json}},


'price' : {{line_item.product.price |
money_without_currency | replace: ',', '.' | json}},
'item_brand' : {{line_item.product.vendor | json}},
'category' : {{line_item.product.type | json}},
'item_list_name' : {{line_item.collection.title | json}},
'quantity' : {{line_item.quantity | json}},
'discount' : {{discount.code | json}}
},{% endfor %}],
};

$(__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 %}

}); // document ready


}
}, 500);
</script>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy