-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtymber.ts
More file actions
125 lines (113 loc) · 4.15 KB
/
tymber.ts
File metadata and controls
125 lines (113 loc) · 4.15 KB
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
import observable from "src/shared/utils/create-events-observable";
import { datalayerSource } from "../sources/google-datalayer-source";
import { isTrackerLoaded } from "../sources/utils/is-tracker-loaded";
import { xhrResponseSource } from "../sources/xhr-response-source";
import { TransactionCartItem } from "../types";
const tymberDataSource = () => {
let success = false;
datalayerSource((data: any) => {
if (data.event === "addToCart") {
const products = data.ecommerce.add.products;
const currency = data.ecommerce.currency;
const { brand, category, id, name, price, quantity } = products[0];
observable.notify({
addToCartEvent: {
sku: id.toString(),
name: (name || "N/A").toString(),
category: (category || "N/A").toString(),
unitPrice: parseFloat(price || 0),
quantity: parseInt(quantity || 1),
currency: (currency || "USD").toString(),
},
});
}
if (data.event === "removeFromCart") {
const products = data.ecommerce.remove.products;
const currency = data.ecommerce.currency;
const { brand, category, id, name, price, quantity } = products[0];
observable.notify({
removeFromCartEvent: {
sku: id.toString(),
name: (name || "N/A").toString(),
category: (category || "N/A").toString(),
unitPrice: parseFloat(price || 0),
quantity: parseInt(quantity || 1),
currency: (currency || "USD").toString(),
},
});
}
if (data.event === "purchase") {
try {
const transaction = data.ecommerce.actionField;
const products = data.ecommerce.products;
const { id, revenue, tax, coupon } = transaction;
observable.notify({
transactionEvent: {
id: id.toString(),
total: parseFloat(revenue),
tax: parseFloat(tax),
shipping: 0,
city: "N/A",
couponCode: coupon || "N/A",
discount: 0,
state: "N/A",
country: "N/A",
currency: "USD",
items: products.map((item) => {
return {
orderId: transaction.id.toString(),
sku: item.id.toString(),
name: (item.name || "N/A").toString(),
category: (item.category || "N/A").toString(),
unitPrice: parseFloat(item.price || 0),
quantity: parseInt(item.quantity || 1),
currency: "USD",
} as TransactionCartItem;
}),
},
});
success = true;
} catch (error) {
// window.tracker('trackError', JSON.stringify(error), 'TYMBER');
}
}
});
if (!success) {
const trackTransaction = (transaction) => {
observable.notify({
transactionEvent: {
id: transaction.orderId.toString(),
total: parseFloat(transaction.orderAmount),
tax: parseFloat(transaction.taxTotal) || 0,
shipping: parseFloat(transaction.shippingCostTotal) || 0,
city: (transaction.billingAddress.city || "N/A").toString(),
state: (transaction.billingAddress.stateOrProvinceCode || "N/A").toString(),
country: (transaction.billingAddress.countryCode || "N/A").toString(),
currency: "USD",
items: transaction.lineItems.physicalItems.map((item) => {
return {
orderId: transaction.orderId.toString(),
sku: item.sku.toString(),
name: (item.name || "N/A").toString(),
category: "N/A",
unitPrice: parseFloat(item.listPrice || 0),
quantity: parseInt(item.quantity || 1),
currency: "USD",
} as TransactionCartItem;
}),
},
});
};
xhrResponseSource((xhr) => {
try {
const transaction = JSON.parse(JSON.stringify(JSON.parse(xhr.responseText)));
if (transaction.status && transaction.orderAmount > 0) {
isTrackerLoaded(() => {
trackTransaction(transaction);
});
}
} catch (error) { }
});
}
};
export default tymberDataSource;