-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-taboola.html
238 lines (196 loc) · 8.86 KB
/
tag-rocket-taboola.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Taboola [v2.4]
Taboola base pixel, conversion events and ecommevents
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tracking-with-taboola-on-bigcommerce/
Copyright (C) 2021 Web Site Advantage
*/
!function(w,t){
var taboolaAccountId = 0; // REPLACE_WITH_YOUR_ID; If you have a DCO account, use the associated SC accounts ID here.
var useEcommEvents = true; // For DCO (Dynamic Creative Optimization) accounts
var campaignIds = ""; // An optional parameter containing a list of campaign id these events are relevant to
var itemGroupIdFormat = "[[psku]]||P[[pid]]";
var debug = false;
// End of settings --------
var tagName = 'Taboola';
var consentRequired = 'targetingAdvertising';
// Event based conversions need creating to get access to the events this tracks:
// https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tracking-with-taboola-on-bigcommerce/
// https://help.taboola.com/hc/en-us/articles/360003484314-Defining-and-Creating-Conversions
// ecevents were reverse engineered from the Taboola ecommerce pixel
// https://tagmanager.google.com/gallery/#/owners/taboola/templates/taboola-ecomm-pixel
var getProductId = function(item,) {
return T.getProductId(tagName, 'ItemGroupId', item, itemGroupIdFormat);
};
var productInformation = function(item, change) {
var product = {
productId: getProductId(item)
};
if (item.price || item.price == 0) product.price = item.price;
if (change) product.quantity = change;
else if (item.quantity) product.quantity = item.quantity;
return product;
}
var cartInformation = function(cart) {
var tagData = {
value: cart.orderAmount,
currency: cart.currency.code,
cartDetails: []
};
if (cart.orderId) tagData.orderId = '' + cart.orderId;
for(var i =0; i < cart.items.length; i++) {
var item = cart.items[i];
tagData.cartDetails.push(productInformation(item));
}
return tagData;
};
w._tfa = w._tfa||[];
var CallTag = function(eventData) {
eventData.id = taboolaAccountId;
if (campaignIds) eventData.campaignIds = campaignIds;
if (!taboolaAccountId) {
console.warn(tagName, "Missing taboolaAccountId", eventData);
return;
}
if(debug) console.log(tagName, eventData);
w._tfa.push(eventData);
}
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://cdn.taboola.com');
T.addScriptTag('//cdn.taboola.com/libtrc/unip/'+taboolaAccountId+'/tfa.js',{id:'tb_tfa_script'});
}
}
CallTag({notify: 'event', name: 'page_view'}); // I think name is case sensitive, with events lower case and ecevents upper case
if (T.consent[consentRequired]) {
addScriptTag();
}
else {
T.on('ConsentChanged',function(data){
if (T.consent[consentRequired]) addScriptTag();
});
}
{{#if settings.request.absolute_path '===' '/'}}
CallTag({notify: 'ecevent', name: 'HOME_PAGE_VISIT'});
{{/if}}
T.on('.*',function(eventData, eventName){
switch(eventName) {
case "QuickSearch":
case "SearchPage":
CallTag({notify: 'event', name: 'search'});
break;
case "ProductPage":
if (useEcommEvents) {
CallTag({
notify: 'ecevent',
name: 'PRODUCT_VIEW',
productIds: [getProductId(eventData)]
});
}
break;
case "ProductsVisible":
if (useEcommEvents) {
var tagData = {
notify: 'ecevent',
name: 'CATEGORY_VIEW',
productIds: eventData.list.items.map(function(item){
return getProductId(item);
})
}
if (eventData.list.listName) {
tagData.category = eventData.list.listName;
}
if (eventData.list.id) {
tagData.categoryId = eventData.list.id;
}
if (eventData.search) {
tagData.name = 'SEARCH';
tagData.searchTerm = eventData.search;
}
CallTag(tagData);
}
break;
case "CartItemChanged":
if (eventData.change > 0) CallTag({notify: 'event', name: 'add_to_cart', quantity: eventData.change});
if (useEcommEvents) {
if (eventData.change > 0) {
CallTag({
notify: 'ecevent',
name: 'ADD_TO_CART',
productIds: [getProductId(eventData.item)]
});
}
else {
CallTag({
notify: 'ecevent',
name: 'REMOVE_FROM_CART',
productIds: [getProductId(eventData.item)]
});
}
}
break;
case "CheckoutStep1CustomerStarted":
CallTag({notify: 'event', name: 'start_checkout'});
if (useEcommEvents) {
CallTag({
notify: 'ecevent',
name: 'CHECKOUT',
productIds: eventData.items.map(function(item){
return getProductId(item);
})
});
}
break;
case "CheckoutStep5OrderCompleted":
CallTag({notify: 'event', name: 'add_payment_info'});
CallTag({
notify: 'event',
name: 'make_purchase',
revenue: eventData.orderAmount,
currency: eventData.currency.code,
orderid: ''+eventData.orderId,
quantity: eventData.items.reduce(function(p, c){return p + c.quantity}, 0)
});
if (useEcommEvents) {
var tagData = cartInformation(eventData);
tagData.notify = 'ecevent';
tagData.name = 'PURCHASE';
// from the tag code. custType is valid and should be a boolean, however the code rejects it and cancels the event
// if (eventData.new_customer === true) {
// tagData.custType = true;
// }
// if (eventData.new_customer === false) {
// tagData.custType = false;
// }
CallTag(tagData);
}
break;
case "AccountCreated":
CallTag({notify: 'event', name: 'complete_registration'});
break;
case "ContactSuccess":
CallTag({notify: 'event', name: 'contact'}); //was lead_contact
break;
case "SubscribeSuccess":
CallTag({notify: 'event', name: 'subscribe'}); //was lead_subscribe
break;
case "AddToWishList":
CallTag({notify: 'event', name: 'add_to_wishlist'});
if (useEcommEvents) {
CallTag({
notify: 'ecevent',
name: 'ADD_TO_WISH_LIST',
productIds: [getProductId(eventData)]
});
}
break;
// REMOVE_FROM_WISH_LIST
}
});
})
}(window,"TagRocket")
</script>