Last active
June 22, 2018 11:26
-
-
Save matthew6688/a29dedc7d38f56ec83e811c54abc9a9a to your computer and use it in GitHub Desktop.
Connect Shopify with Google Tag Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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-MFNSHGD'; | |
//======================================================= | |
// 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 }}' | |
}] | |
} | |
}; | |
{% 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