Skip to content

Instantly share code, notes, and snippets.

@matthew6688
Last active July 30, 2018 12:32
Show Gist options
  • Save matthew6688/a40f7d43d954db01f10625d4fa312165 to your computer and use it in GitHub Desktop.
Save matthew6688/a40f7d43d954db01f10625d4fa312165 to your computer and use it in GitHub Desktop.
No Shopping cart is created equal
<script>
//=======================================================
//
// More Awesome scripts and tricks about shopify on www.connectdots.co
//=======================================================
// IMPORTANT: Change this value to whatever the ID of your
// GTM container is.
const GTM_CONTAINER_ID = 'GTM-XXXXXXX';
//=======================================================
// Only customise the below if you know what you're doing
//=======================================================
// Initialise data layer object
var dataLayer = [{
page: {
type: '{{ template }}',
currency:'{{shop.currency}}',
title: '{{ page_title }}'
},
}];
{% if template contains 'product' %}
//=======================================================
// Product pages
//=======================================================
dataLayer[0].page.breadcrumb = '{{ collection.title }}';
dataLayer[0].ecommerce = {
'detail': {
'actionField': {'list':'{{collection.title}}'},
'products': [{
'name': '{{ product.title }}',
'id': '{{ product.id }}',
'price': ({{ product.price }} / 100).toString(),
'brand': '{{ product.vendor }}',
'category': '{{ product.type }}'
}]
}
};
{% elsif template contains 'cart' %}
//=======================================================
// Cart page
//=======================================================
var items = []
{% for line_item in cart.items %}
item = {};
item.id = '{{line_item.product_id}}';
item.name = '{{line_item.product.title}}';
item.category = '{{line_item.product.type}}';
item.price = {{line_item.line_price | times: 0.01}};
item.quantity = {{line_item.quantity | default: 0}};
items.push(item);
{% endfor %}
var contents = []
{% for line_item in cart.items %}
content = {};
content.id = '{{line_item.product_id}}';
content.price = {{line_item.line_price | times: 0.01}};
content.quantity = {{line_item.quantity | default: 0}};
contents.push(content);
{% endfor %}
dataLayer.push({
'event': 'Viewed Shopping Cart',
'items' : '{{ cart.item_count }}',
'cartPrice' : '{{ cart.total_price | times: 0.01}}',
'currency': "{{shop.currency}}",
'product': items,
'content': contents
});
{% endif %}
//=======================================================
// GTM container tag
//=======================================================
<!-- Google Tag Manager -->
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer', GTM_CONTAINER_ID);
<!-- End Google Tag Manager -->
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment