-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-piwik-pro.html
207 lines (168 loc) · 7.83 KB
/
tag-rocket-piwik-pro.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Piwik Pro [v2.3]
Piwik Pro Analytics
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2022 Web Site Advantage
*/
!function(w,t){
var piwikInstanceName='your-instance-name'; // e.g. admin is https://your-instance-name.piwik.pro/ then the instance name is your-instance-name
var piwikSiteId = 'XXX-XXX-XXX-XXX-XXX'; // Menu->Administration->Sites & apps. Select site, copy ID
// Create manually tracked goals for "Contact", "Account Created", "Subscribe" and "Add To Wish List", then set their Goal id (integer or UUID) here:
var piwikGoalContactId = "";
var piwikGoalSubscribeId = "";
var piwikGoalAccountCreatedId = "";
var piwikGoalAddToWishListId = "";
// goal values can be set in admin
// Create manually tracked custom dimensions for "Customer Group Name" (Session dimensions) and "Page Type" (Event dimensions) then set their Dimension ID here:
var piwikCustomDimensionCustomerGroupNameId = 0;
var piwikCustomDimensionPageTypeId = 0;
var enableHeartBeatTimer = true;
var enableLinkTracking = true;
var enableJSErrorTracking = true;
var debug = false;
// Custom event actions to track shopping behaviour funnel. category, action, name, value:
// tagRocket, ecommerceProductListView, {type}, {name}
// tagRocket, ecommerceCartView
// tagRocket, ecommerceCheckoutStep1, Started
// tagRocket, ecommerceCheckoutStep2, Shipping Completed
// tagRocket, ecommerceCheckoutStep3, Billing Completed
// End of settings --------
var consentRequired = 'analytics';
var productInformation = function(item, change) {
var product = {
name: item.name
};
// product.sku = '' + (item.variantSku || item.productSku || item.productId); // would variant SKUs confuse things?
product.sku = '' + (item.productSku || item.productId);
if (item.price || item.price == 0) product.price = item.price;
if (item.brand) product.brand = item.brand;
if (item.variantName) product.variant = item.variantName;
if (item.category && item.category.breadcrumbs) product.category = item.category.breadcrumbs;
if (change) product.quantity = change;
else if (item.quantity) product.quantity = item.quantity;
return product;
}
var productListInformation = function(cart) {
return cart.items.map(function(item){
return productInformation(item);
})
};
var orderInformation = function(order) {
var orderInfo = {
subTotal: order.baseAmount,
tax: order.taxTotal,
shipping: order.shippingCostTotal,
discount: order.totalDiscount,
grandTotal: order.orderAmount
};
if (order.orderId) orderInfo.orderId = order.orderId;
return orderInfo;
};
// https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/installation.html
w._paq=w._paq||[];
var PaqPush = function(args) {
if (debug) console.log('PaqPush', args);
w._paq.push(args);
}
PaqPush(['setTrackerUrl', 'https://'+piwikInstanceName+'.piwik.pro/ppms.php']);
PaqPush(['setSiteId', piwikSiteId]);
// For details on commands see:
// https://developers.piwik.pro/en/latest/data_collection/web/javascript_tracking_client/api.html
if (enableHeartBeatTimer) {
PaqPush(['enableHeartBeatTimer']);
}
if (enableJSErrorTracking) {
PaqPush(["enableJSErrorTracking"]);
}
{{~#if customer.id}}PaqPush(['setUserId', '{{customer.id}}']);{{~/if}}
{{~#if customer.customer_group_name}}if(piwikCustomDimensionCustomerGroupNameId)PaqPush(['setCustomDimensionValue', piwikCustomDimensionCustomerGroupNameId, {{{json customer.customer_group_name}}}]);{{~/if}}
w[t]=w[t]||{};var T=w[t];T.i=T.i||[];if(!T.init)T.init=function(f){T.i.push(f)};
T.init(function() {
var scriptTagAdded = false;
function addScriptTag() {
if (!scriptTagAdded) {
scriptTagAdded = true;
T.addPreconnectTag('https://'+piwikInstanceName+'.piwik.pro/');
T.addScriptTag('https://'+piwikInstanceName+'.piwik.pro/ppms.js', {async:true, defer:true});
}
};
if (T.consent[consentRequired]) {
addScriptTag();
}
else {
T.on('ConsentChanged',function(data){
if (T.consent[consentRequired]) addScriptTag();
});
}
if (piwikCustomDimensionPageTypeId) {
PaqPush(['setCustomDimensionValue', piwikCustomDimensionPageTypeId, T.pageType]);
}
PaqPush(['trackPageView']);
if (enableLinkTracking) {
// enableLinkTracking should be called right after the first trackPageView or trackEvent.
PaqPush(['enableLinkTracking']);
}
T.on('.*',function(eventData, eventName){
switch(eventName) {
case "QuickSearch":
case "SearchPage":
PaqPush(['trackSiteSearch', eventData.term]);
break;
case "ProductPage":
PaqPush(['ecommerceProductDetailView',
[productInformation(eventData)]
]);
break;
case "ProductsVisible":
PaqPush(["trackEvent", 'productListView', eventData.list.type, eventData.list.name, eventData.list.items.length]);
break;
case "CartItemChanged":
if(eventData.change > 0) {
PaqPush(['ecommerceAddToCart',
[productInformation(eventData.item, eventData.change)]
]);
}
else {
PaqPush(['ecommerceRemoveFromCart',
[productInformation(eventData.item, -eventData.change)]
]);
}
break;
case "CartPage":
PaqPush(["trackEvent", 'checkout', 'cartView', 'Cart Viewed']);
break;
case "CheckoutStep1CustomerStarted":
PaqPush(["trackEvent", 'checkout', 'checkoutStep1', 'Started']);
break;
case "CheckoutStep2ShippingCompleted":
PaqPush(["trackEvent", 'checkout', 'checkoutStep2', 'Shipping Completed']);
break;
case "CheckoutStep3BillingCompleted":
PaqPush(["trackEvent", 'checkout', 'checkoutStep3', 'Billing Completed']);
break;
case "CheckoutStep5OrderCompleted":
PaqPush(['ecommerceOrder',
eventData.items.map(function(item){
return productInformation(item);
}),
orderInformation(eventData)
]);
break;
case "AccountCreated":
if (piwikGoalAccountCreatedId) PaqPush(['trackGoal', piwikGoalAccountCreatedId]);
break;
case "ContactSuccess":
if (piwikGoalContactId) PaqPush(['trackGoal', piwikGoalContactId]);
break;
case "SubscribeSuccess":
if (piwikGoalSubscribeId) PaqPush(['trackGoal', piwikGoalSubscribeId]);
break;
case "AddToWishList":
if (piwikGoalAddToWishListId) PaqPush(['trackGoal', piwikGoalAddToWishListId]);
break;
}
});
})
}(window,"TagRocket")
</script>