-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqubit-events.liquid
196 lines (181 loc) · 7.87 KB
/
qubit-events.liquid
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
<!-- Qubit Events start -->
<script>
!function(){function n(){function n(n){p.level=n}function e(n,e){p.info(n,"event emitted"),e=c(e||{}),e.meta=e.meta||{},e.meta.type=n,u.push(e),r(),v.listeners=f(v.listeners,function(n){return!n.disposed})}function o(n,e,o){function r(){return p.info("Replaying events"),t(function(){s(v.events,function(t){c.disposed||l(n,t.meta.type)&&e.call(o,t)})}),f}function i(){return p.info("Disposing event handler"),c.disposed=!0,f}p.info("Attaching event handler for",n);var c={type:n,callback:e,disposed:!1,context:o||window};v.listeners.push(c);var f={replay:r,dispose:i};return f}function t(n){p.info("Calling event handlers"),a++;try{n()}catch(n){p.error("UV API Error",n.stack)}a--,r()}function r(){if(0===u.length&&p.info("No more events to process"),u.length>0&&a>0&&p.info("Event will be processed later"),u.length>0&&0===a){p.info("Processing event");var n=u.shift();v.events.push(n),t(function(){s(v.listeners,function(e){if(!e.disposed&&l(e.type,n.meta.type))try{e.callback.call(e.context,n)}catch(n){p.error("Error emitting UV event",n.stack)}})})}}function i(n,e,o){var t=v.on(n,function(){e.apply(o||window,arguments),t.dispose()});return t}function s(n,e){for(var o=n.length,t=0;t<o;t++)e(n[t],t)}function c(n){var e={};for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o]);return e}function l(n,e){return"string"==typeof n?n===e:n.test(e)}function f(n,e){for(var o=n.length,t=[],r=0;r<o;r++)e(n[r])&&t.push(n[r]);return t}var u=[],a=0,p={info:function(){p.level>n.INFO||console&&console.info&&console.info.apply(console,arguments)},error:function(){p.level>n.ERROR||console&&console.error&&console.error.apply(console,arguments)}};n.ALL=0,n.INFO=1,n.ERROR=2,n.OFF=3,n(n.ERROR);var v={on:o,emit:e,once:i,events:[],listeners:[],logLevel:n};return v}"object"==typeof module&&module.exports?module.exports=n:window&&void 0===window.uv&&(window.uv=n())}();
</script>
<!-- TODO: Update below path with the one provided to you by Qubit -->
<script src="//static.goqubit.com/smartserve-XXXX.js" async defer></script>
<script>
(function () {
var config = {
eventNamespace: false,
template: {{ template | json }}
}
if(config.eventNamespace) {
config.ns = config.eventNamespace + "."
} else {
config.ns=""
}
var getPageType = function () {
switch (config.template) {
case "index":
return "home";
break;
case "page":
return "content";
break;
case "article":
return "content";
break;
case "blog":
return "content";
break;
case "product":
return "product";
break;
case "search":
return "search";
break;
case "collection":
return "category";
break;
case "cart":
return "basket";
break;
default:
return config.template;
}
};
{% comment %}
Page
{% endcomment %}
uv.emit(config.ns + 'ecView', {
type: getPageType(),
currency: {{ shop.currency | json }},
language: {{ shop.locale | json }}
})
{% comment %}
User - https://help.shopify.com/themes/liquid/objects/customer
{% endcomment %}
{% if customer %}
uv.emit(config.ns + 'ecUser', {
user: {
id: "{{ customer.id | json }}",
email: {{ customer.email | json }},
firstName: {{ customer.first_name | json }},
lastName: {{ customer.last_name | json }}
}
})
{% endif %}
{% comment %}
Product - https://help.shopify.com/themes/liquid/objects/product
{% endcomment %}
{% if product %}
uv.emit(config.ns + 'ecProduct', {
eventType: 'detail',
product: {
productId: "{{ product.id | json }}",
sku: {{ product.selected_or_first_available_variant.sku | default: product.selected_or_first_available_variant.id | json }},
name: {{ product.title | json }},
manufacturer: {{ product.vendor | json }},
stock: {{ product.selected_or_first_available_variant.inventory_quantity }},
price: {
currency: {{ shop.currency | json }},
value: {{ product.price | money_without_currency | remove: ',' }}
},
url: window.location.protocol + "//" + window.location.hostname + "{{ product.url }}",
description: {{ product.description | strip_html | strip_newlines | truncate: 2048 | json }},
categories: ["{{ product.type}}"],
images: [ window.location.protocol + "{{ product | img_url: '400x' }}" ]
}
})
{% endif %}
{% comment %}
Listing - collection pages - https://help.shopify.com/themes/liquid/objects/collection
{% endcomment %}
{% if collection %}
{% for product in collection.products %}
uv.emit(config.ns + 'ecProduct', {
eventType: 'listing',
product: {
productId: "{{ product.id | json }}",
sku: {{ product.selected_or_first_available_variant.sku | default: product.selected_or_first_available_variant.id | json }},
name: {{ product.title | json }},
manufacturer: {{ product.vendor | json }},
stock: {{ product.selected_or_first_available_variant.inventory_quantity }},
price: {
currency: {{ shop.currency | json }},
value: {{ product.price | money_without_currency | remove: ',' }}
},
url: window.location.protocol + "//" + window.location.hostname + "{{ product.url }}",
categories: ["{{ product.type}}"]
}
})
{% endfor %}
{% endif %}
{% comment %}
Listing - search pages - https://help.shopify.com/themes/liquid/objects/search
{% endcomment %}
{% if search.terms %}
{% for product in search.results %}
uv.emit(config.ns + 'ecProduct', {
eventType: 'listing',
product: {
productId: "{{ product.id | json}}",
sku: {{ product.selected_or_first_available_variant.sku | default: product.selected_or_first_available_variant.id | json }},
name: {{ product.title | json }},
manufacturer: {{ product.vendor | json }},
stock: {{ product.selected_or_first_available_variant.inventory_quantity }},
price: {
currency: {{ shop.currency | json }},
value: {{ product.price | money_without_currency | remove: ',' }}
},
url: window.location.protocol + "//" + window.location.hostname + "{{ product.url }}",
categories: ["{{ product.type}}"]
}
})
{% endfor %}
{% endif %}
{% comment %}
Basket - https://help.shopify.com/themes/liquid/objects/cart
{% endcomment %}
{% if cart.item_count > 0 %}
var basketSummary = {
subtotal: {
currency: {{ shop.currency | json }},
value: {{ cart.items_subtotal_price | money_without_currency | remove: ',' }}
},
total: {
currency: {{ shop.currency | json }},
value: {{ cart.total_price | money_without_currency | remove: ',' }}
},
quantity: {{ cart.item_count }}
}
uv.emit('ecBasketSummary', {
basket: basketSummary
})
{% for item in cart.items %}
uv.emit('ecBasketItem', {
basket: basketSummary,
product: {
productId: "{{ item.product.id | json}}",
sku: {{ item.sku | default: item.variant_id | json }},
name: {{ item.product.title | json }},
manufacturer: {{ item.product.vendor | json }},
price: {
currency: {{ shop.currency | json }},
value: {{ item.product.price | money_without_currency | remove: ',' }}
},
url: window.location.protocol + "//" + window.location.hostname + "{{ item.product.url }}",
categories: ["{{ item.product.type}}"]
},
quantity: {{ item.quantity }},
subtotal: {
currency: {{ shop.currency | json }},
value: {{ item.line_price | money_without_currency | remove: ',' }}
}
})
{% endfor %}
{% endif %}
}());
</script>
<!-- Qubit Events end -->