-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-klaviyo.html
355 lines (278 loc) · 14.5 KB
/
tag-rocket-klaviyo.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Klaviyo [v1.1]
Supports consent, identify, Added to Cart, Viewed Product, Viewed Category, Searched Site, Clicked Banner and more
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2024 Web Site Advantage
*/
!function(w,t){
// Ensure your BigCommerce Integration has the Automatic Onsite tracking switched off
// How to find the public API key: https://help.klaviyo.com/hc/en-us/articles/115005062267
var publicApiKey = 'PUBLIC_API_KEY'; // TODO: change to your public API key
var compareAtPrice = "{{product.price.rrp_without_tax.value}}";
var debug = false;
// End of settings --------
// links to documentation
// https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object#supported-methods
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration
var tagName = 'Klaviyo';
var consentRequired = 'targetingAdvertising';
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#viewed-product-tracking-snippet
var productInformation = function(item) {
var product = {
ProductID: item.productId,
ProductName: item.name
};
// https://developers.klaviyo.com/en/docs/custom_event_tracking#track-a-metric
if (item.categories) {
product.Categories = item.categories.map(function(category){
return category.name;
});
}
if (item.url) product.URL = item.url;
if (item.defaultImage && item.defaultImage.url) product.ImageURL = item.defaultImage.url;
if (item.brand) product.Brand = item.brand;
if (item.price || item.price == 0) product.Price = item.price;
if (compareAtPrice) product.CompareAtPrice = compareAtPrice;
if (item.productSku) product.SKU = item.productSku;
if (item.quantity) product.Quantity = item.quantity;
return product;
}
var addItemInformation = function(item) {
var addItem = {
"AddedItemProductName": item.name,
"AddedItemProductID": item.productId
}
if (item.price || item.price == 0) addItem.AddedItemPrice = item.price;
if (item.productSku) addItem.AddedItemSKU = item.productSku;
if (item.defaultImage && item.defaultImage.url) addItem.AddedItemURL = item.defaultImage.url;
if (item.url) addItem.AddedItemURL = item.url;
if (item.categories) {
addItem.AddedItemCategories = item.categories.map(function(category){
return category.name;
});
}
// addToCartData.AddedItemDescription - unknown
// addToCartData.AddedItemTags
return addItem;
}
// https://help.klaviyo.com/hc/en-us/articles/360030732832#added-to-cart2
var addToCartInformation = function(eventData) {
// data on the new item, and then a full list of cart items
var item = eventData.item;
var cart = eventData.cart;
var addToCartData = addItemInformation(item);
addToCartData.AddedItemQuantity = eventData.change;
// cart information
addToCartData.CheckoutURL = '{{settings.base_url}}{{urls.checkout.single_address}}';
if (cart.cartAmount || cart.cartAmount == 0) addToCartData["$value"] = cart.cartAmount; // Total cart value
addToCartData["$currency_code"] = eventData.currency;
addToCartData.ItemNames = cart.items.map(function(item){
return item.name;
});
var allCategories = [];
cart.items.forEach(function(item){
if (item.categories) {
item.categories.forEach(function(category){
if (!allCategories.includes(category.name)) {
allCategories.push(category.name);
}
});
}
});
addToCartData.Categories = allCategories;
var itemCount = 0;
addToCartData.Items = cart.items.map(function(item){
var product = {
"ProductID": item.productId,
"ProductName": item.name,
"Quantity": item.quantity,
"ItemPrice": item.price,
"RowTotal": item.price * item.quantity,
"ProductURL": item.url
};
if (item.productSku) product.SKU = item.productSku;
itemCount += item.quantity;
if (item.defaultImage && item.defaultImage.url) product.ImageURL = item.defaultImage.url;
if (item.categories) {
product.ProductCategories = item.categories.map(function(category){
return category.name;
});
}
return product;
});
addToCartData.ItemCount = itemCount;
// addToCartData.Tags
return addToCartData;
}
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#viewed-product-tracking-snippet
var itemInformation = function(item) {
var klaviyoItem = {
ItemId: item.productId,
Title: item.name,
Metadata: {}
};
// https://developers.klaviyo.com/en/docs/custom_event_tracking#track-a-metric
if (item.categories) {
klaviyoItem.Categories = item.categories.map(function(category){
return category.name || category;
});
}
if (item.url) klaviyoItem.Url = item.url;
if (item.defaultImage && item.defaultImage.url) klaviyoItem.ImageUrl = item.defaultImage.url;
if (item.brand) klaviyoItem.Metadata.Brand = item.brand;
if (item.price || item.price == 0) klaviyoItem.Metadata.Price = item.price;
if (compareAtPrice) klaviyoItem.Metadata.CompareAtPrice = compareAtPrice;
return klaviyoItem;
}
// https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object
!function(){if(!window.klaviyo){window._klOnsite=window._klOnsite||[];try{window.klaviyo=new Proxy({},{get:function(n,i){return"push"===i?function(){var n;(n=window._klOnsite).push.apply(n,arguments)}:function(){for(var n=arguments.length,o=new Array(n),w=0;w<n;w++)o[w]=arguments[w];var t="function"==typeof o[o.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push([i].concat(o,[function(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo||[],window.klaviyo.push=function(){var n;(n=window._klOnsite).push.apply(n,arguments)}}}}();
var KlaviyoPush = function(arg1) {
if(debug) console.log(tagName, arguments);
w.klaviyo.push(arg1);
}
// if we have no user data, then Klaviyo does not send events. https://developers.klaviyo.com/en/docs/javascript_api#track-events-and-actions
// https://developers.klaviyo.com/en/docs/javascript_api#identify-people
// https://developers.klaviyo.com/en/docs/custom_integration_faqs#should-i-use-emailphone-or-external-id-as-my-unique-identifier
var KalviyoIdentify = function() {
var userData = T.userData;
var identify = {};
if(userData.email) identify['$email'] = userData.email;
if(userData.phone) identify['$phone_number'] = userData.phone;
if(userData.first_name) identify['$first_name'] = userData.first_name;
if(userData.last_name) identify['$last_name'] = userData.last_name;
if(userData.customer_group_name) identify.CustomerGroupName = userData.customer_group_name;
// Klaviyos own events use customer id, but I beleive that means they can't identify guests
if(userData.id) identify["$id"] = userData.id;
if (T.consent.current) {
if (T.consent.current.ad_personalization) identify.ad_personalization = T.consent.current.ad_personalization;
if (T.consent.current.ad_user_data) identify.ad_user_data = T.consent.current.ad_user_data;
}
if (identify) {
KlaviyoPush(['identify', identify]);
}
}
// Establish the TagRocket tag
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://static.klaviyo.com');
T.addScriptTag('https://static.klaviyo.com/onsite/js/klaviyo.js?company_id='+publicApiKey);
}
}
// How Klaviyo does consent. They Support Shopify
// https://help.klaviyo.com/hc/en-us/articles/360034666712
// https://help.klaviyo.com/hc/en-us/articles/115005076767
// Their __kla_id cookie identifies visitors.
// we will do basic consent for now. i.e. no script until consent is given
if (T.consent[consentRequired]) {
addScriptTag();
}
else {
T.on('ConsentChanged',function(data){
KalviyoIdentify(); // updates consent parameters
if (T.consent[consentRequired]) addScriptTag();
});
}
KalviyoIdentify();
{{#if page_type '===' 'category'}}
// TODO: Viewed Category - https://developers.klaviyo.com/en/docs/guide_to_setting_up_api_based_website_activity_events#viewed-category
var viewedCategory = {
"CategoryName": {{{json category.name}}},
"CategoryID": "{{category.id}}",
"URL": {{{json category.url}}}
};
{{#if category.image.data}}viewedCategory.ImageURL = {{{json category.image.data}}};{{/if}}
KlaviyoPush(["track", "Viewed Category", viewedCategory]);
{{/if}}
// TODO: remove or update to send the correct data to the tag based on each event
// put TagRocket in debug mode to see the eventData provided in each event
T.on('.*',function(eventData, eventName){
switch(eventName) {
case "UserDataUpdated":
KalviyoIdentify();
break;
case "QuickSearch":
// https://developers.klaviyo.com/en/docs/guide_to_setting_up_api_based_website_activity_events#searched-site
KlaviyoPush(["track", "Searched Site", {
"SearchTerm": eventData.term
// "SearchTerm (autocorrected)": "Fantasy Books",
// "ReturnedResults": 54
}]);
break;
case "SearchPage":
// https://developers.klaviyo.com/en/docs/guide_to_setting_up_api_based_website_activity_events#searched-site
KlaviyoPush(["track", "Searched Site", {
"SearchTerm": eventData.term
// "SearchTerm (autocorrected)": "Fantasy Books",
// "ReturnedResults": 54
}]);
break;
case "ProductPage":
// https://developers.klaviyo.com/en/docs/custom_event_tracking#track-a-metric
KlaviyoPush(["track", "Viewed Product", productInformation(eventData)]);
KlaviyoPush(["trackViewedItem", itemInformation(eventData)]);
break;
case "ProductsVisible":
break;
case "CartItemChanged":
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#added-to-cart-tracking-snippet
if(eventData.change > 0) {
KlaviyoPush(["track", 'Added to Cart', addToCartInformation(eventData)]);
}
break;
case "CartPage":
KlaviyoPush(["track", 'Viewed Cart']);
break;
case "CheckoutPage": // Start checkout
// Started Checkout: Handled by Klaviyo server side
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#started-checkout
break;
case "CheckoutStep1CustomerCompleted":
KlaviyoPush(["track", 'Checkout Customer Completed']);
break;
case "CheckoutStep2ShippingCompleted":
KlaviyoPush(["track", 'Checkout Shipping Completed']);
break;
case "CheckoutStep3BillingCompleted":
KlaviyoPush(["track", 'Checkout Billing Completed']);
break;
case "CheckoutStep5OrderCompleted":
// Place Order: Handled by Klaviyo server side
// https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#placed-order
break;
case "AccountCreated":
KlaviyoPush(["track", 'Account Created']);
break;
case "ContactSuccess":
KlaviyoPush(["track", 'Contacted via Website Form']);
break;
case "SubscribeSuccess":
KlaviyoPush(["track", 'Subscribed to Newsletter']);
break;
case "AddToWishList":
KlaviyoPush(["track", 'Added to Wish List', addItemInformation(eventData)]);
break;
case "ProductClicked":
break;
case "PromotionClicked":
// Clicked Banner - https://developers.klaviyo.com/en/docs/guide_to_setting_up_api_based_website_activity_events#clicked-banner
KlaviyoPush(["track", "Clicked Banner", {
"SourceURL": '' + document.location,
// "DestinationURL": "https://www.example.com/black-friday-deals",
"BannerTitle": eventData.name
}]);
break;
case "PromotionsVisible":
case "SiteLogin":
case "CheckoutLogin":
case "LinkClick":
break;
}
});
})
}(window,"TagRocket")
</script>