-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart-checkout.js
157 lines (136 loc) · 4.7 KB
/
cart-checkout.js
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
'use strict';
(function ($) {
var shopAnalytics = document.shopAnalytics;
var $productsInCart = $(shopAnalytics.cart.elements.product);
var pageLoad = true;
$(onLoad);
$(document.body)
.on('updated_cart_totals', onUpdatedCartTotals)
.on('updated_shipping_method', onShippingMethodUpdated)
.on('payment_method_selected', onPaymentMethodSelected)
.on('change', shopAnalytics.checkout.elements.billingAddressFields, onBillingAddressUpdate)
.on('change', shopAnalytics.checkout.elements.shippingAddressFields, onShippingAddressUpdate);
$('form.checkout')
.on('change', shopAnalytics.checkout.elements.shippingMethods, onShippingMethodUpdated)
.on('click', shopAnalytics.checkout.elements.checkoutPlaceOrderButton, onCheckoutPlaceOrder);
function onLoad() {
if (!$productsInCart.length) {
return;
}
var event_data = shopAnalytics.checkout.dataInit;
event_data.ecommerce.checkout.actionField.step = 1;
event_data.ecommerce.checkout.products = shopAnalytics.getProductsData($productsInCart);
localStorage.setItem('productsInCartData', JSON.stringify(shopAnalytics.getProductsData($productsInCart)));
shopAnalytics.postToDataLayer(event_data);
}
/**
* Reacts to the update of quantity of product items in the cart.
*/
function onUpdatedCartTotals() {
shopAnalytics.updateCartItemsQuantity($productsInCart);
localStorage.setItem('productsInCartData', JSON.stringify(shopAnalytics.getProductsData($productsInCart)));
}
/**
* Reacts to selection of shipping method.
*/
function onShippingMethodUpdated() {
var $selected = $(shopAnalytics.cart.elements.shippingMethodSelected);
if (!$selected) {
return;
}
var selectedId = '';
var selectedValue = '';
if ($selected.is('select')) {
selectedValue = $selected.find('option:selected').text();
}
else if ($selected.attr('type') === 'radio') {
selectedId = $selected.attr('id');
if (!selectedId) {
return;
}
selectedValue = $('label[for="' + selectedId + '"]').text();
}
else {
return;
}
registerCheckoutStepEvent(shop_analytics_checkout_steps.order, selectedValue);
}
/**
* Reacts to place order button click.
*/
function onCheckoutPlaceOrder() {
// Clears list of previously tracked orders.
localStorage.setItem('shop-analytics-tracked-orders', JSON.stringify([]));
}
/**
* Reacts to selection of payment method.
*/
function onPaymentMethodSelected() {
if (pageLoad) {
pageLoad = false;
return;
}
var optionId = $(shopAnalytics.checkout.elements.paymentMethodSelected).attr('id');
if (!optionId) {
return;
}
registerCheckoutStepEvent(shop_analytics_checkout_steps.order, $('label[for="' + optionId + '"]').text());
}
/**
* Reacts to billing address fields update.
*/
function onBillingAddressUpdate() {
var productsData = JSON.parse(localStorage.getItem('productsInCartData'));
if (!productsData) {
return;
}
if (fieldsAreFilled($(shopAnalytics.checkout.elements.billingAddressFieldsRequired))) {
registerCheckoutStepEvent(shop_analytics_checkout_steps.order);
}
}
/**
* Reacts to billing address fields update.
*/
function onShippingAddressUpdate() {
var productsData = JSON.parse(localStorage.getItem('productsInCartData'));
if (!productsData) {
return;
}
if (fieldsAreFilled($(shopAnalytics.checkout.elements.shippingAddressFieldsRequired))) {
var optionLabel = $(shopAnalytics.checkout.elements.shippingAddressToggle).is(':checked') ? shopAnalytics.checkout.messages.shipToDifferentAddress : shopAnalytics.checkout.messages.shipToSameAddress;
registerCheckoutStepEvent(shop_analytics_checkout_steps.order, optionLabel);
}
}
/**
* Checks if given fields have content.
*/
function fieldsAreFilled($fields) {
var filled = true;
if ($fields.length === 0) {
return filled;
}
$fields.each(function() {
var fieldValue = $(this).val() || '';
if (!fieldValue.trim().length) {
filled = false;
}
});
return filled;
}
/**
* Pushes checkout step data to Google Analytics data layer.
*/
function registerCheckoutStepEvent(step, optionLabel) {
var productsData = JSON.parse(localStorage.getItem('productsInCartData'));
if (!productsData) {
return;
}
var event_data = shopAnalytics.checkout.dataInit;
event_data.ecommerce.checkout.actionField.step = step;
if (optionLabel) {
event_data.ecommerce.checkout.actionField.option = optionLabel.trim();
}
event_data.ecommerce.checkout.products = productsData;
shopAnalytics.postToDataLayer(event_data);
}
})(jQuery);