forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecommerce.js
248 lines (214 loc) · 6.23 KB
/
ecommerce.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import firebase from "firebase/app";
import "firebase/analytics";
// [START analytics_ecommerce_items]
// A pair of jeggings
const item_jeggings = {
item_id: 'SKU_123',
item_name: 'jeggings',
item_category: 'pants',
item_variant: 'black',
item_brand: 'Google',
price: 9.99
};
// A pair of boots
const item_boots = {
item_id: 'SKU_456',
item_name: 'boots',
item_category: 'shoes',
item_variant: 'brown',
item_brand: 'Google',
price: 24.99
};
// A pair of socks
const item_socks = {
item_id: 'SKU_789',
item_name: 'ankle_socks',
item_category: 'socks',
item_variant: 'red',
item_brand: 'Google',
price: 5.99
};
// [END analytics_ecommerce_items]
function ecommerceViewItemList() {
// [START analytics_ecommerce_view_item_list]
// Prepare ecommerce params
const params1 = {
item_list_id: 'L001',
item_list_name: 'Related products',
items: [item_jeggings, item_boots, item_socks]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_ITEM_LIST, params1);
// [END analytics_ecommerce_view_item_list]
}
function ecommerceSelectItem() {
// [START analytics_ecommerce_select_item]
// Prepare ecommerce event params
const params2 = {
item_list_id: 'L001',
item_list_name: 'Related products',
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.SELECT_ITEM, params2);
// [END analytics_ecommerce_select_item]
}
function ecommerceViewItemDetails() {
// [START analytics_ecommerce_view_item_details]
// Prepare ecommerce event params
const params3 = {
currency: 'USD',
value: 9.99,
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_ITEM, params3);
// [END analytics_ecommerce_view_item_details]
}
function ecommerceAddCart() {
// [START analytics_ecommerce_add_cart]
// Specify order quantity
const item_jeggings_quantity = {
...item_jeggings,
quantity: 2
};
// Prepare ecommerce bundle
const params4 = {
currency: 'USD',
value: 19.98,
items: [item_jeggings_quantity]
};
// Log event when a product is added to a wishlist
firebase.analytics().logEvent(firebase.analytics.EventName.ADD_TO_WISHLIST, params4);
// Log event when a product is added to the cart
firebase.analytics().logEvent(firebase.analytics.EventName.ADD_TO_CART, params4);
// [END analytics_ecommerce_add_cart]
}
function ecommerceViewCart() {
// [START analytics_ecommerce_view_cart]
// Specify order quantity
const item_jeggings_quantity = {
...item_jeggings,
quantity: 2
};
const item_boots_quantity = {
...item_boots,
quantity: 1
};
// Prepare ecommerce params
const params5 = {
currency: 'USD',
value: 44.97,
items: [item_jeggings_quantity, item_boots_quantity]
};
// Log event when the cart is viewed
firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_CART, params5);
// [END analytics_ecommerce_view_cart]
}
function ecommerceRemoveCart() {
// [START analytics_ecommerce_remove_cart]
// Prepare ecommerce params
const params6 = {
currency: 'USD',
value: 24.99,
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.REMOVE_FROM_CART, params6);
// [END analytics_ecommerce_remove_cart]
}
function ecommerceCheckout() {
// [START analytics_ecommerce_checkout]
// Prepare ecommerce params
const params7 = {
currency: 'USD',
value: 14.98, // Total Revenue
coupon: 'SUMMER_FUN',
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.BEGIN_CHECKOUT, params7);
// [END analytics_ecommerce_checkout]
}
function ecommerceShippingInfo() {
// [START analytics_ecommerce_shipping_info]
// Prepare ecommerce params
const params8 = {
currency: 'USD',
value: 14.98, // Total Revenue
coupon: 'SUMMER_FUN',
shipping_tier: 'Ground',
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.ADD_SHIPPING_INFO, params8);
// [END analytics_ecommerce_shipping_info]
}
function ecommercePaymentInfo() {
// [START analytics_ecommerce_payment_info]
// Prepare ecommerce params
const params9 = {
currency: 'USD',
value: 14.98, // Total Revenue
coupon: 'SUMMER_FUN',
payment_type: 'Visa',
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.ADD_PAYMENT_INFO, params9);
// [END analytics_ecommerce_payment_info]
}
function ecommercePurchase() {
// [START analytics_ecommerce_purchase]
// Prepare ecommerce bundle
const params10 = {
transaction_id: 'T12345',
affiliation: 'Google Store',
currency: 'USD',
value: 14.98, // Total Revenue
tax: 2.85,
shipping: 5.34,
coupon: 'SUMMER_FUN',
items: [item_jeggings]
};
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.PURCHASE, params10);
// [END analytics_ecommerce_purchase]
}
function ecommerceRefund() {
// [START analytics_ecommerce_refund]
// Prepare ecommerce params
const params11 = {
transaction_id: 'T12345', // Required
affiliation: 'Google Store',
currency: 'USD',
value: 9.99,
items: []
};
// (Optional) For partial refunds, define the item_id and quantity of refunded items
const refundedProduct = {
item_id: 'SKU_123', // Required
quantity: 1 // Required
};
params11.items.push(refundedProduct);
// Log event
firebase.analytics().logEvent(firebase.analytics.EventName.REFUND, params11);
// [END analytics_ecommerce_refund]
}
function ecommercePromotions() {
// [START analytics_ecommerce_promotions]
// Prepare ecommerce params
const params12 = {
promotion_id: 'ABC123',
promotion_name: 'Summer Sale',
creative_name: 'summer2020_promo.jpg',
creative_slot: 'featured_app_1',
location_id: 'HERO_BANNER',
items: [item_jeggings]
};
// Log event when a promotion is displayed
firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_PROMOTION, params12);
// Log event when a promotion is selected
firebase.analytics().logEvent(firebase.analytics.EventName.SELECT_PROMOTION, params12);
// [END analytics_ecommerce_promotions]
}