-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-bcee-ecomm.html
121 lines (98 loc) · 3.74 KB
/
tag-rocket-bcee-ecomm.html
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
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket BCEE Ecomm [v1.0]
Add dynamic remarketing ecomm dimensions
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2022 Web Site Advantage
*/
!function(w,t){
w[t]=w[t]||{};var T=w[t];T.i=T.i||[];if(!T.init)T.init=function(f){T.i.push(f)};
var dimension_ecomm_pagetype = "dimension1";
var dimension_ecomm_prodid = "dimension2";
var dimension_ecomm_totalvalue = "dimension3";
var productIdentifier = function(product) {return ''+product.productId}; // productId, productSku
var ecomm_pagetype = '
{{~#if settings.request.absolute_path '===' '/'}}home
{{~else if page_type '===' 'category'}}category
{{~else if page_type '===' 'product'}}product
{{~else if page_type '===' 'cart'}}cart
{{~else if page_type '===' 'orderconfirmation'}}purchase
{{~else if page_type '===' 'search'}}searchresults
{{~else if page_type '===' 'checkout'}}other
{{~else}}other{{/if}}';
var ecomm_prodid = [];
var ecomm_totalvalue;
var delayPageview = false;
var pageview;
{{~#if page_type '===' 'category'}}
{{~#if category.products}}
{{#each category.products}}
ecomm_prodid.push(productIdentifier({productId:'{{id}}',productSku:'{{sku}}'}));
{{/each~}}
{{/if~}}
{{/if~}}
{{~#if page_type '===' 'product'}}
ecomm_totalvalue = {{~#if product.price.with_tax.value}}{{product.price.with_tax.value}}{{~else if product.price.without_tax.value}}{{product.price.without_tax.value}}{{~else}}0{{/if~}};
ecomm_prodid.push(productIdentifier({productId:'{{product.id}}',productSku:'{{product.sku}}'}));
{{/if~}}
{{#if page_type '===' 'search'}}
{{~#if product_results.products}}
{{#each product_results.products}}
ecomm_prodid.push(productIdentifier({productId:'{{id}}',productSku:'{{sku}}'}));
{{/each~}}
{{/if~}}
{{/if~}}
{{~#if page_type '===' 'cart'}}
{{~#each cart.items}}
{{~#if type '===' 'Item'}}
ecomm_prodid.push(productIdentifier({productId:'{{product_id}}',productSku:'{{sku}}'}));
{{/if~}}
{{/each~}}
ecomm_totalvalue = {{cart.sub_total.value}};
{{/if~}}
{{~#if page_type '===' 'orderconfirmation'}}
delayPageview = true;
setTimeout(function() {
delayPageview = false;
if (pageview) {
ga.apply(null,pageview);
pageview = null;
}
}, 1000);
fetch('/api/storefront/order/{{checkout.order.id}}', {credentials: 'include'})
.then(function(response) {
return response.json();
})
.then(function(order) {
for (var i = 0; i < order.lineItems.physicalItems.length; i++) {
ecomm_prodid.push(productIdentifier({productId:order.lineItems.physicalItems[i].productId,productSku:order.lineItems.physicalItems[i].sku}));
}
ecomm_totalvalue = order.orderAmount;
delayPageview = false;
ga('set', dimension_ecomm_pagetype, ecomm_pagetype);
ga('set', dimension_ecomm_prodid, ecomm_prodid.join());
ga('set', dimension_ecomm_totalvalue, ecomm_totalvalue ? ecomm_totalvalue.toString() : '');
console.log('pageview', pageview);
if (pageview) {
ga.apply(null,pageview);
pageview = null;
}
});
{{/if~}}
T.init(function() {
T.on('GaCommandPending',function(data, eventName){
if (data.arguments[0] === 'send' && data.arguments[1] === 'pageview'){
if (!delayPageview) {
ga('set', dimension_ecomm_pagetype, ecomm_pagetype);
ga('set', dimension_ecomm_prodid, ecomm_prodid.join());
ga('set', dimension_ecomm_totalvalue, ecomm_totalvalue ? ecomm_totalvalue.toString() : '');
}
else {
pageview = data.arguments;
data.cancel = true;
}
}
});
})
}(window,"TagRocket")
</script>