-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-matomo.html
153 lines (131 loc) · 6.9 KB
/
tag-rocket-matomo.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
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Matomo [v1.6]
Matomo Analytics
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2022 Web Site Advantage
*/
!function(w,t){
// Update settings to match your setup
var matomoTrackingDomain = 'mymatomosubdomain.matomo.cloud';
var matomoSiteId = '1';
// create manually triggered goals for "Contact", "Account Created", "Subscribe" and "Add To Wish List", then set their id here:
var matomoGoalContactId = 0;
var matomoGoalAccountCreatedId = 0;
var matomoGoalSubscribeId = 0;
var matomoGoalAddToWishListId = 0;
// create custom dimensions for "Customer Group Name" (Visit) and "Page Type" (Action) then set their id here:
var matomoCustomDimensionCustomerGroupNameId = 0;
var matomoCustomDimensionPageTypeId = 0;
// how products are identified
var matomoProductIdFormat = "[[psku]]||P[[pid]]"; // [[psku]] = Product SKU, [[pid]] = Internal Product ID. || is fallback if psku is not set.
// End of settings --------
// https://developer.matomo.org/guides/tracking-javascript-guide
// https://matomo.org/faq/reports/advanced-manually-tracking-ecommerce-actions-in-matomo/
// https://developer.matomo.org/api-reference/tracking-javascript
var productIdentifier = function(item) {return T.getProductId('Matomo', 'ItemGroupId', item, matomoProductIdFormat)};
matomoTrackingDomain = matomoTrackingDomain.replace(/(^\w+:|^)\/\//, '').replace(/\/*$/, ''); // remove protocol and trailing slash
w._paq=w._paq||[];
var PaqPush = function(args) {
// console.log('PaqPush('+ args.join(', ')+')', args);
w._paq.push(args);
}
PaqPush(['setTrackerUrl', '//'+matomoTrackingDomain+'/matomo.php']);
PaqPush(['setSiteId', matomoSiteId]);
PaqPush(['enableHeartBeatTimer']);
{{#if customer.id}}PaqPush(['setUserId', '{{customer.id}}']);{{/if}}
{{#if customer.customer_group_name}}if (matomoCustomDimensionCustomerGroupNameId) PaqPush(['setCustomDimension', matomoCustomDimensionCustomerGroupNameId, {{{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() {
T.addPreconnectTag('https://'+matomoTrackingDomain);
T.addScriptTag('//'+matomoTrackingDomain+'/matomo.js');
if (matomoCustomDimensionPageTypeId) PaqPush(['setCustomDimension', matomoCustomDimensionPageTypeId, T.pageType]);
{{#if settings.shopper_consent_tracking_enabled}}
if (!T.consent.analytics) PaqPush(['requireConsent']);
{{/if}}
{{#if page_type '===' 'category'}}
PaqPush(['setEcommerceView',
false, // Product SKU is not applicable for a category view.
false, // Product name is not applicable for a category view.
{{{json category.name}}}, // (Optional) Product category, or array of up to 5 categories
]);
{{/if}}
{{#if page_type '===' 'product'}}
// trackPageView done later so after setEcommerceView
{{else}}
PaqPush(['trackPageView']);
PaqPush(['enableLinkTracking']);
{{/if}}
T.on('.*',function(data, eventName){
switch(eventName) {
{{#if settings.shopper_consent_tracking_enabled}}
case "ConsentChanged":
if (T.consent.analytics) PaqPush(['setConsentGiven']);
break;
{{/if}}
case "QuickSearch":
case "SearchPage":
PaqPush(['trackSiteSearch', data.term, false, false]);
break;
case "ProductPage":
PaqPush(['setEcommerceView',
productIdentifier(data), // (Required) productSKU
data.name, // (Optional) productName
data.category&&data.category.name, // (Optional) categoryName
data.price // (Optional) price
]);
PaqPush(['trackPageView']); // setEcommerceView requires this to send the data
PaqPush(['enableLinkTracking']);
break;
case "CartItemChanged":
PaqPush(['clearEcommerceCart']);
for(var i =0; i < data.cart.items.length; i++){
var item = data.cart.items[i];
PaqPush(['addEcommerceItem',
productIdentifier(item), // (Required) productSKU
item.name, // (Optional) productName
item.category&&item.category.name, // (Optional) productCategory
item.price, // (Recommended) price
item.quantity // (Optional, defaults to 1) quantity
]);
}
PaqPush(['trackEcommerceCartUpdate', data.cart.grandTotal || data.cart.cartAmount]);
break;
case "CheckoutStep5OrderCompleted":
PaqPush(['clearEcommerceCart']);
for(var i =0; i < data.items.length; i++) {
var item = data.items[i];
PaqPush(['addEcommerceItem',
productIdentifier(item), // (Required) productSKU
item.name, // (Optional) productName
item.category&&item.category.name, // (Optional) productCategory
item.price, // (Recommended) price
item.quantity // (Optional, defaults to 1) quantity
]);
}
PaqPush(['trackEcommerceOrder',
data.orderId, // (Required) orderId
data.orderAmount, // (Required) grandTotal (revenue)
// 9.99, // (Optional) subTotal
// 1.5, // (optional) tax
// 1, // (optional) shipping
// false // (optional) discount
]);
break;
case "AccountCreated":
if (matomoGoalAccountCreatedId) PaqPush(['trackGoal', matomoGoalAccountCreatedId]);
break;
case "ContactSuccess":
if (matomoGoalContactId) PaqPush(['trackGoal', matomoGoalContactId]);
break;
case "SubscribeSuccess":
if (matomoGoalSubscribeId) PaqPush(['trackGoal', matomoGoalSubscribeId]);
break;
case "AddToWishList":
if (matomoGoalAddToWishListId) PaqPush(['trackGoal', matomoGoalAddToWishListId]);
break;
}
});
})
}(window,"TagRocket")
</script>