-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-search-spring.html
145 lines (118 loc) · 5.37 KB
/
tag-rocket-search-spring.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
<script data-cfasync="false">
{{#or (if page_type '===' 'product') (if page_type '===' 'cart') (if page_type '===' 'orderconfirmation') }}
/*
Web Site Advantage: Tag Rocket Search Spring [v1.0]
Product, Cart and Order Confirmation events for Search Spring
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2023 Web Site Advantage
*/
!function(w,t){
var siteId = 'SITEID';
var itemGroupIdFormat = "[[psku]]||P[[pid]]"; // sku - needs to match core field sku in the catalog
var itemIdProductFormat = "[[psku]]||P[[pid]]"; // childSku
var itemIdVariantFormat = "[[vsku]]"; // childSku
var debug = false;
// End of settings --------
// https://help.searchspring.net/hc/en-us/articles/360056186252
// https://docs.searchspring.com/reference/intellisuggest_tracking
// https://help.searchspring.net/hc/en-us/articles/201185129-Adding-IntelliSuggest-Tracking
var tagName = 'SearchSpring';
var consentRequired = 'analytics';
var getProductId = function(item, type) {
if (type == 'ItemId' && itemIdProductFormat && itemIdVariantFormat){
return T.getProductId(tagName, 'ItemId', item, itemIdProductFormat, itemIdVariantFormat);
}
else {
return T.getProductId(tagName, 'ItemGroupId', item, itemGroupIdFormat)
}
};
var callQueue = [];
var callIntelliSuggest = function(functionName, options) {
if(debug) console.log(tagName, functionName, options);
if (IntelliSuggest) {
trackIntelliSuggest();
IntelliSuggest[functionName](options);
}
else {
callQueue.push({functionName: functionName, options: options});
}
}
var trackIntelliSuggest = function() {
callQueue.forEach(function(call) {
IntelliSuggest[call.functionName](call.options);
});
callQueue = [];
}
var cartEvents = function(eventData) {
var seed = eventData.items.map(function(item){
return getProductId(item, 'ItemGroupId');
});
callIntelliSuggest("init", {"siteId" : siteId, "context" : "Basket", "seed" : seed});
{{#if customer.id}}callIntelliSuggest("setShopperId", '{{customer.id}}');{{/if}}
for(var i = 0; i < eventData.items.length; i++) {
var item = eventData.items[i];
var sku = getProductId(item, 'ItemGroupId');
var childSku = getProductId(item, 'ItemId');
var newItem = {
"sku" : getProductId(item, 'ItemGroupId'),
"qty" : item.quantity,
"price" : item.price
};
if (childSku && childSku != sku) {
newItem.childSku = childSku;
};
callIntelliSuggest("haveItem",newItem);
}
{{#if page_type '===' 'cart'}}callIntelliSuggest("inBasket",{});{{/if}}
{{#if page_type '===' 'orderconfirmation'}}
var options = {
orderId: eventData.orderId, // The Order ID (optional)
total: eventData.orderAmount, // The total order amount (optional)
};
if (eventData.consignments && eventData.consignments.shipping && eventData.consignments.shipping.length > 0) {
options.city = eventData.consignments.shipping[0].city; // Customer"s shipping address City (optional)
options.state = eventData.consignments.shipping[0].stateOrProvince; // Customer"s shipping address State (optional)
options.country = eventData.consignments.shipping[0].countryCode; // Customer"s shipping address 2-letter Country Code (optional)
}
callIntelliSuggest("inSale",options);
{{/if}}
}
// 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://cdn.searchspring.net');
T.addScriptTag('https://cdn.searchspring.net/intellisuggest/is.min.js',{}, trackIntelliSuggest);
}
}
if (T.consent[consentRequired]) {
addScriptTag();
}
else {
T.on('ConsentChanged',function(data){
if (T.consent[consentRequired]) addScriptTag();
});
}
T.on('.*',function(eventData, eventName){
switch(eventName) {
case "ProductPage":
var productSku = getProductId(eventData, 'ItemGroupId');
callIntelliSuggest("init", {"siteId" : siteId, "context" : "Product/" + productSku, "seed" : [productSku]});
{{#if customer.id}}callIntelliSuggest("setShopperId", '{{customer.id}}');{{/if}}
callIntelliSuggest("viewItem", {"sku" : productSku});
break;
case "CartPage":
case "CheckoutStep5OrderCompleted":
cartEvents(eventData);
break;
case "ProductClicked": // https://help.searchspring.net/hc/en-us/articles/201185129-Adding-IntelliSuggest-Tracking#productclicks
break;
}
});
})
}(window,"TagRocket")
{{/or}}
</script>