Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
14 lines (12 sloc) 4.86 KB
<script> var dataLayerData = {'page': {'template': {{ template | json }}, 'searchTerm': {{ search.terms | json }}, 'searchResults': {{ search.results_count | json }}, 'currentPage': {{ current_page | json }}, 'currency': {{ shop.currency | json }}, 'collection': {{ collection.title | json }}}, 'product': {}, 'customer': {}, 'collection': {}, 'cart': { }}; {%- if collection and template != "product" -%} dataLayerData.collection = {'title': {{ collection.title | json }}, 'productCount': {{ collection.products_count | json }}, 'products': []}; {%- for product in collection.products -%} {%- assign variantsAvailable = 0 -%} {%- assign variantStock = 0 -%} {%- assign stockNotManaged = false -%} {%- for variant in product.variants -%} {%- if variant.available == true -%} {%- assign variantsAvailable = variantsAvailable | plus: 1 -%} {%- if variant.inventory_management == null -%} {%- assign stockNotManaged = true -%} {%- else -%} {%- assign variantStock = variantStock | plus: variant.inventory_quantity -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- if stockNotManaged == true -%}{%- assign variantStock = null -%}{%- endif -%} dataLayerData.collection.products.push({'id': {{ product.id | json }}, 'sku': {{ product.sku | json }}, 'title': {{ product.title | json }}, 'price': {{ product.price | money_without_currency | json }}, 'type': {{ product.type | json }}, 'tags': {{ product.tags | json }}, 'brand': {{ product.vendor | json }}, 'inStock': {{ product.available | json }}, 'stockQty': {{ variantStock | json }}, 'hasNoVariants': {{ product.has_only_default_variant | json }}, 'numVariants': {{ product.variants.size | json }}, 'variantsInStock': {{ variantsAvailable | json }}, 'variants': [ {%- for variant in product.variants -%} {'name': {{ variant.title | json }}, 'inStock': {{ variant.available }}, 'price': {{ variant.price | money_without_currency | json }}}, {%- endfor-%} ]}); {%- endfor -%} {%- endif -%} {%- if product -%} {%- assign variantsAvailable = 0 -%} {%- assign variantStock = 0 -%} {%- assign stockNotManaged = false -%} {%- for variant in product.variants -%} {%- if variant.available == true -%} {%- assign variantsAvailable = variantsAvailable | plus: 1 -%} {%- if variant.inventory_management == null -%} {%- assign stockNotManaged = true -%} {%- else -%} {%- assign variantStock = variantStock | plus: variant.inventory_quantity -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- if stockNotManaged == true -%}{%- assign variantStock = null -%}{%- endif -%} dataLayerData.product = {'id': {{ product.id | json }}, 'sku': {{ product.sku | json }}, 'title': {{ product.title | json }}, 'price': {{ product.price | money_without_currency | json }}, 'type': {{ product.type | json }}, 'tags': {{ product.tags | json }}, 'brand': {{ product.vendor | json }}, 'inStock': {{ product.available | json }}, 'stockQty': {{ variantStock | json }}, 'hasNoVariants': {{ product.has_only_default_variant | json }}, 'numVariants': {{ product.variants.size | json }}, 'variantsInStock': {{ variantsAvailable | json }}, 'variants': [ {%- for variant in product.variants -%} {'name': {{ variant.title | json }}, 'inStock': {{ variant.available }}, 'price': {{ variant.price | money_without_currency | json }}}, {%- endfor -%} ]}; {%- endif -%} {%- if customer -%} dataLayerData.customer = {'id': {{ customer.id | json }}, 'firstName': {{ customer.first_name | json }}, 'surname': {{ customer.last_name | json }}, 'email': {{ customer.email | json }}, 'ordersCount': {{ customer.orders_count | json }}, 'totalSpent': {{ customer.total_spent | money_without_currency | json }}, 'acceptsMarketing': {{ customer.accepts_marketing | json }}, 'tags': {{ customer.tags | json }}}; {%- endif -%} {%- if cart -%} dataLayerData.cart.total = {{ cart.total_price | money_without_currency | json }}; dataLayerData.cart.count = {{ cart.item_count | json }}; dataLayerData.cart.products = []; {%- for item in cart.items -%} dataLayerData.cart.products.push({'name': {{ item.title | json }}, 'product_id': {{ item.product.id | json }}, 'variant_id': {{ item.variant.id | json }}, 'quantity': {{ item.quantity | json }}, 'price': {{ item.line_price | money_without_currency | json }}}); {%- endfor -%} {%- endif -%} window.dataLayer = window.dataLayer || []; window.dataLayer.push(dataLayerData); </script>
<!-- Google Tag Manager -->
<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','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
You can’t perform that action at this time.