This repository was archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path3wh-gtm.liquid
More file actions
154 lines (140 loc) · 5.85 KB
/
3wh-gtm.liquid
File metadata and controls
154 lines (140 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<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) -->