Skip to content

Instantly share code, notes, and snippets.

@milesw
Created April 4, 2019 06:06
Show Gist options
  • Save milesw/c05f9b1d14a6603d6f40ff47a2885d49 to your computer and use it in GitHub Desktop.
Save milesw/c05f9b1d14a6603d6f40ff47a2885d49 to your computer and use it in GitHub Desktop.
{%- comment -%}
Google Tag Manager scripts for checkout.liquid
Note: This script must run after {{ checkout_scripts }}
{%- endcomment -%}
{% assign order_data = checkout.order %}
{% assign customer_data = checkout.customer %}
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
if (window.Shopify && Shopify.Checkout) {
dataLayer.push({
checkoutPage: Shopify.Checkout.page,
checkoutStep: Shopify.Checkout.step
});
}
{% if customer_data %}
dataLayer.push({
"customer": {
id: "{{ customer_data.id }}",
name: "{{ customer_data.name }}",
firstName: "{{ customer_data.first_name }}",
lastName: "{{ customer_data.last_name }}",
email: "{{ customer_data.email }}",
phone: "{{ customer_data.default_address.phone }}",
address: {
street: "{{ customer_data.default_address.street }}",
city: "{{ customer_data.default_address.city }}",
state: "{{ customer_data.default_address.province }}",
stateCode: "{{ customer_data.default_address.province_code }}",
postalCode: "{{ customer_data.default_address.zip }}",
country: "{{ customer_data.default_address.country }}",
countryCode: "{{ customer_data.default_address.country_code }}"
},
totalSpent: "{{ customer_data.total_spent }}",
allOrdersCount: "{{ customer_data.orders_count }}",
allOrderIds: [{% for order in customer_data.orders %}"{{ order.id }}",{% endfor %}],
tags: [{% for tag in customer_data.tags %} "{{ tag }}", {% endfor %}]
}
});
{% endif %}
{% if order_data %}
dataLayer.push({
"order": {
id: "{{ checkout.order_id }}",
number: "{{ order_data.order_number }}",
currency: "{{ currency.iso_code | default: 'USD' }}",
total: "{{ order_data.total_price | money_without_currency }}",
subtotal: "{{ order_data.subtotal_price | money_without_currency }}",
shipping: "{{ order_data.shipping_price | money_without_currency }}",
tax: "{{ order_data.tax_price | money_without_currency }}",
discount: "{{ order_data.total_discounts | money_without_currency }}",
discountCode: "{{ order_data.discounts.first.code }}",
items: [
{%- assign total_items = 0 -%}
{%- for line_item in order_data.line_items -%}
{
id: "{{ line_item.product_id }}",
variantId: "{{ line_item.variant.id }}",
sku: "{{ line_item.sku }}",
name: "{{ line_item.title }}",
variantName: "{{ line_item.variant.title }}",
type: "{{ line_item.product.type }}",
vendor: "{{ line_item.product.vendor }}",
price: "{{ line_item.price | money_without_currency }}",
total: "{{ line_item.line_price | money_without_currency }}",
quantity: "{{ line_item.quantity }}"
},
{%- assign total_items = total_items | plus: line_item.quantity -%}
{%- endfor -%}
],
itemCount: {{ total_items }},
}
});
{% endif %}
</script>
<script>(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', {{ shop.metafields["global"]["gtm_account"] | json }} );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment