-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-blotout-edgetag.html
446 lines (387 loc) · 19.3 KB
/
tag-rocket-blotout-edgetag.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Blotout Edgetag [v1.5]
Blotout Edgetag
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2023 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 tagUrl = 'https://d.mysite.com'; // replace tagUrl with the tag url you get from app.edgetag.io when onboarding is completed
var subscriptionValue = 1;
var subscriptionValueCurrency = "USD";
var contactValue = 1;
var contactValueCurrency = "USD";
var debug = false;
// all is the default options if a specified provider does not define a property, or if the provider is not specified
// consent required uses BigCommerce terminology: targetingAdvertising, analytics or functional
// add properties to a provider to override how the function behaves for that provider
// should always leave one provider using the all settings
var providers = {
all: {
// default Data Feed Watch ID formats
consentRequired: "targetingAdvertising",
// This defines how the id is set
itemGroupIdFormat: '[[pid]]', // format to use when sending product group level IDs
// These define how the variantId is set. Note, it could be a product level ID
// We do that as variantId may be used as the Content ID in facebook, which may be a product identifier
itemIdProductFormat: '[[pid]]', // format to use when sending item level IDs for a product
itemIdVariantFormat: '[[pid]]-[[vid]]' // format to use when sending item level IDs for a variant
},
facebook: {
// this is how BigCommerce structures their Meta feed
itemGroupIdFormat: '[[pid]]', // product_group format to use.
itemIdProductFormat: '[[psku]]||P[[pid]]', // product format to use for products.
itemIdVariantFormat: '[[vsku]]', // product format to use for variants.
sendVariantIdInsteadOfProductIdSet: false // a setting in the Meta provider admin. If true we will send product and not product_group IDs where possible.
},
googleAnalytics4: {
consentRequired: "analytics"
}
// googleAds
};
// End of settings --------
// https://app.edgetag.io/assets/script-templates/big-commerce/big-commerce.txt
// https://app.edgetag.io/docs/standard-events
// https://app.edgetag.io/docs/coding-shop
// https://app.edgetag.io/docs/onboarding
// https://app.edgetag.io/docs/step-by-step
w.edgetag=w.edgetag||function(){(edgetag.stubs=edgetag.stubs||[]).push(arguments)};
// wrapper so we can log events
var EdgeTag = function() {
if (debug) console.log('EdgeTag', arguments);
edgetag.apply(edgetag, arguments);
}
// complete providers
var defaultProvider = providers.all;
if (!defaultProvider.productCategory) {
defaultProvider.productCategory = function(product) {
if (product.category && product.category.name) return product.category.name
};
}
defaultProvider.consentRequired = defaultProvider.consentRequired || "targetingAdvertising";
if (defaultProvider.sendVariantIdInsteadOfProductIdSet === undefined) defaultProvider.sendVariantIdInsteadOfProductIdSet = false;
var modifiedProviders = {};
for (const providerName in providers) {
var provider = providers[providerName];
provider.name = providerName;
provider.modified = provider.itemGroupIdFormat || provider.itemIdProductFormat || provider.itemIdVariantFormat || provider.productCategory;
if (provider.modified) {
modifiedProviders[providerName] = provider;
}
if (providerName != 'all') {
provider.consentRequired = provider.consentRequired || defaultProvider.consentRequired;
provider.productCategory = provider.productCategory || defaultProvider.productCategory;
if (provider.itemGroupIdFormat === undefined) provider.itemGroupIdFormat = defaultProvider.itemGroupIdFormat;
if (provider.itemIdProductFormat === undefined) provider.itemIdProductFormat = defaultProvider.itemIdProductFormat;
if (provider.itemIdVariantFormat === undefined) provider.itemIdVariantFormat = defaultProvider.itemIdVariantFormat;
if (provider.sendVariantIdInsteadOfProductIdSet === undefined) provider.sendVariantIdInsteadOfProductIdSet = defaultProvider.sendVariantIdInsteadOfProductIdSet;
}
}
// if an event uses any of the provider functions for product information then this lets us send it to each modified provider
var forEachModifiedProvider = function(doFunction) {
for (const providerName in modifiedProviders) {
doFunction(providers[providerName]);
}
}
// this returns the destinations object for one of the modified providers so that the modified event only goes to them
// all is a special case where we send it to all but the modified providers
var buildDestinationObject = function(provider) {
var destinations = {};
if (provider.name == "all") {
// do all but modified providers
for (const providerName in modifiedProviders) {
destinations[providerName] = false;
}
destinations.all = true;
}
else {
// do only the provider
destinations.all = false;
destinations[provider.name] = true;
}
return destinations;
}
// Consent
// https://app.edgetag.io/docs/step-by-step#consent e.g. { all: true, facebook: false }
var GetConsentObject = function() {
if (!T.consent.enabled) return {all: true};
var consent = {all: T.consent[defaultProvider.consentRequired || 'targetingAdvertising']}; // un specified providers use the default
for (const providerName in providers) {
if (providerName != 'all') {
consent[providerName] = T.consent[providers[providerName].consentRequired || 'targetingAdvertising'] || false;
}
}
return consent;
}
// Some BigCommerce specific thing from the example BigCommerce implementation
var sendBigCommerceEmail = function (email) {
if (email) {
EdgeTag(
'tag',
'map_id',
{
map_id: email,
map_provider: 'bigcommerce_email',
},
{ blotoutCloud: true }
);
}
};
var buildProduct = function(item, provider, preferedType, change) {
// facebook and tiktok use the product | product_group
// settings in admin decide if variantId is used
var type = preferedType;
if (!type || !provider.sendVariantIdInsteadOfProductIdSet) {
type = 'product_group';
}
var product = {
quantity: change || item.quantity || 1,
item_price: item.price,
title: item.name,
type: type
};
product.id = T.getProductId('Blotout', 'ItemGroupId', item, provider.itemGroupIdFormat);
{{#if product.main_image.data}}
product.image = {{{json product.main_image.data}}}.replace('{:size}', '500x659'); // random size, or to fit its use?
{{/if}}
{{#if product.url}}
product.url = {{{json product.url}}};
{{/if}}
if (type == 'product' || !provider.sendVariantIdInsteadOfProductIdSet) {
// for product we need the variant id. But if it is product_group we want to make sure it is not set as it will be sent
if (provider.itemIdProductFormat && provider.itemIdVariantFormat) {
product.variantId = T.getProductId('Blotout', 'ItemId', item, provider.itemIdProductFormat, provider.itemIdVariantFormat);
}
}
var category = provider.productCategory(item);
if (category)product.category = category;
if (item.brand) product.brand = item.brand;
// description - we don't have that. maybe sometimes from Stencil
return product;
};
T.init(function() {
{{#if settings.shopper_consent_tracking_enabled}}
EdgeTag('init', {
edgeURL: tagUrl
});
EdgeTag('consent', GetConsentObject());
{{else}}
EdgeTag('init', {
edgeURL: tagUrl,
disableConsentCheck: true
});
{{/if}}
// User data
var userData = {};
{{#if customer.email}}
userData.email = {{{json (trim customer.email)}}}.toLowerCase();
{{/if}}
{{#if customer.phone}}
userData.phoneNumber = {{{json customer.phone}}};
{{/if}}
{{#if customer.name}}
var names = {{{json (trim customer.name)}}}.split(' ');
userData.firstName = names[0].trim();
if (names.length > 1) userData.lastName = names[names.length-1].trim();
{{/if}}
{{#if customer.shipping_address.first_name}}
userData.firstName = {{{json (trim customer.shipping_address.first_name)}}};
{{/if}}
{{#if customer.shipping_address.last_name}}
userData.lastName = {{{json (trim customer.shipping_address.last_name)}}};
{{/if}}
{{#if customer.shipping_address.address1}}
userData.address = {{{json (trim customer.shipping_address.address1)}}};
{{/if}}
{{#if customer.shipping_address.address2}}
if (userData.address) {
userData.address += ', ' + {{{json (trim customer.shipping_address.address2)}}};
}
else {
userData.address = {{{json (trim customer.shipping_address.address2)}}};
}
{{/if}}
{{#if customer.shipping_address.city}}
userData.city = {{{json (trim customer.shipping_address.city)}}};
{{/if}}
{{#if customer.shipping_address.state}}
userData.state = {{{json (trim customer.shipping_address.state)}}};
{{/if}}
// country = don't have access to ISO 2 code country from here, but the order does
if (userData && Object.keys(userData).length > 0) EdgeTag('data', userData); // I'm assuming data can be updated and at any time before events
if (userData.email) sendBigCommerceEmail(userData.email);
T.addScriptTag(tagUrl+'/load');
EdgeTag('tag', 'PageView');
T.on('.*',function(data, eventName){
switch(eventName) {
{{#if settings.shopper_consent_tracking_enabled}}
case "ConsentChanged":
EdgeTag('consent', GetConsentObject());
break;
{{/if}}
case "QuickSearch":
case "SearchPage":
EdgeTag('tag', 'Search', {
search: data.term,
}, {}, { method: 'beacon' });
break;
{{#if page_type '===' 'product'}}
case "ProductPage":
forEachModifiedProvider(function(provider) {
EdgeTag('tag', 'ViewContent', {
currency: data.currency,
value: data.price,
contents: [buildProduct(data, provider, 'product_group')],
}, buildDestinationObject(provider));
});
break;
{{/if}}
case "ProductsVisible":
forEachModifiedProvider(function(provider) {
var destination = buildDestinationObject(provider);
// Sending ViewContent to GA4 causes a view_item which is wrong. it should be a view_item_list
destination.googleAnalytics4 = false;
var payload = {
contents: data.list.items.map(function(item){
return buildProduct(item, provider, 'product_group');
})
};
if (data.search) payload.search = data.search;
EdgeTag('tag', {{~#if page_type '===' 'category'}}'ViewCategory'{{~else if page_type '===' 'search'}}'Search'{{~else}}'ViewContent'{{/if~}}, payload, destination);
});
break;
case "CartItemChanged":
if (data.change > 0) {
forEachModifiedProvider(function(provider) {
EdgeTag('tag', 'AddToCart', {
currency: data.currency,
value: data.price * data.change,
contents: [buildProduct(data.item, provider, 'product', data.change)],
}, buildDestinationObject(provider), { method: 'beacon' });
});
}
break;
case "CheckoutStep1CustomerStarted":
forEachModifiedProvider(function(provider) {
var contents = data.items.map(function(item){
return buildProduct(item, provider, 'product');
});
EdgeTag('tag', 'InitiateCheckout', {
contents: contents,
currency: data.currency.code,
value: data.grandTotal || data.cartAmount
}, buildDestinationObject(provider));
});
break;
case "CheckoutStep2ShippingCompleted":
if (data.email && userData.email != data.email) {
userData.email = data.email;
EdgeTag('data', userData);
}
forEachModifiedProvider(function(provider) {
var contents = data.items.map(function(item){
return buildProduct(item, provider, 'product');
});
EdgeTag('tag', 'AddShippingInfo', {
contents: contents,
currency: data.currency.code,
value: data.grandTotal || data.cartAmount
}, buildDestinationObject(provider));
});
break;
case "CheckoutStep3BillingCompleted":
forEachModifiedProvider(function(provider) {
var contents = data.items.map(function(item){
return buildProduct(item, provider, 'product');
});
EdgeTag('tag', 'AddPaymentInfo', {
contents: contents,
currency: data.currency.code,
value: data.grandTotal || data.cartAmount
}, buildDestinationObject(provider));
});
break;
case "CheckoutStep5OrderCompleted":
// user data
var shipping = (data.consignments && data.consignments.shipping && data.consignments.shipping.length && data.consignments.shipping[0]) || {}
var billing = data.billingAddress || {}
var newEmail = false;
var orderEmail = shipping.email || billing.email;
if (userData.email != orderEmail) {
userData.email = orderEmail;
newEmail = true;
};
userData.phoneNumber = shipping.phone || billing.phone;
userData.firstName = shipping.firstName || billing.firstName;
userData.lastName = shipping.lastName || billing.lastName;
var addressSource = billing;
if (shipping.address1 || shipping.address2 || !billing) {
addressSource = shipping;
}
userData.address = addressSource.address1;
if (addressSource.address2) {
if (userData.address) {
userData.address += ', ' + addressSource.address2;
}
else {
userData.address = addressSource.address2;
}
}
userData.city = addressSource.city;
userData.state = addressSource.stateOrProvince;
userData.country = addressSource.countryCode;
EdgeTag('data', userData);
if (newEmail) sendBigCommerceEmail(userData.email);
forEachModifiedProvider(function(provider) {
var contents = data.items.map(function(item){
return buildProduct(item, provider, 'product');
});
EdgeTag('tag', 'Purchase', {
contents: contents,
currency: data.currency.code,
value: data.orderAmount,
eventId: data.orderId,
orderId: data.orderId
}, buildDestinationObject(provider));
});
break;
case "AccountCreated":
EdgeTag('tag', 'CompleteRegistration', {
status: 'AccountCreated'
// name - The name of the page or product associated with the event.
// currency
// value
});
break;
case "ContactSuccess":
EdgeTag('tag', 'Contact', {value: contactValue, currency: contactValueCurrency}); // custom event
userData.email = data.email;
EdgeTag('data', userData);
if (data.email) sendBigCommerceEmail(data.email);
break;
case "SubscribeSuccess":
userData.email = data.email;
EdgeTag('data', userData);
if (data.email) sendBigCommerceEmail(data.email);
EdgeTag('tag', 'Lead', {category: "Newsletter Subscription", name: "Newsletter Subscription", value: subscriptionValue, currency: subscriptionValueCurrency});
break;
case "AddToWishList":
forEachModifiedProvider(function(provider) {
EdgeTag('tag', 'AddToWishList', {
name: data.name,
category: data.category.name,
contents: [buildProduct(data, provider, 'product_group')],
currency: data.currency,
value: data.price
});
});
break;
// EdgeTag('tag', 'Subscribe', {}, {}, {method: 'beacon'}); // this is for a paid subscription
// EdgeTag('tag', 'SignUp'); - pinterest
// EdgeTag('tag', 'CustomizeProduct'); - facebook
}
});
})
}(window,"TagRocket")
</script>