-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (73 loc) · 2.18 KB
/
index.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
class ECommerceHelper {
constructor(options = {}) {
this.debug = options.debug || false;
window.dataLayer = window.dataLayer || [];
}
push(event, data) {
const object = {
event: event,
ecommerce: data,
};
dataLayer.push({ ecommerce: null });
dataLayer.push(object);
if (this.debug) console.debug(object);
}
formatData(data) {
data = { ...data };
if (data.hasOwnProperty('price')) data.price = parseFloat(data.price) || 0;
if (data.hasOwnProperty('index')) data.index = parseInt(data.index) || 1;
if (data.hasOwnProperty('quantity')) data.quantity = parseInt(data.quantity) || 1;
return data;
}
viewList(items) {
this.push('view_item_list', {
items: items.map(item => this.formatData(item)),
});
}
clickProduct(item) {
this.push('select_item', {
items: [ this.formatData(item) ],
});
}
viewProduct(item) {
this.push('view_item', {
items: [ this.formatData(item) ],
});
}
addProduct(item) {
this.push('add_to_cart', {
items: [ this.formatData(item) ],
});
}
removeProduct(item) {
this.push('remove_from_cart', {
items: [ this.formatData(item) ],
});
}
checkout(items) {
this.push('begin_checkout', {
items: items.map(item => this.formatData(item)),
});
}
purchase(data, items) {
const object = this.formatData(data);
object.items = items.map(item => this.formatData(item));
this.push('purchase', object);
}
refund(data, items) {
const object = this.formatData(data);
if (items) object.items = items.map(item => this.formatData(item));
this.push('refund', object);
}
viewPromotion(items) {
this.push('view_promotion', {
items: items.map(item => this.formatData(item)),
});
}
clickPromotion(item) {
this.push('select_promotion', {
items: [ this.formatData(item) ],
});
}
}
const EC = new ECommerceHelper({ debug: true });