|
| 1 | +import firebase from "firebase/app"; |
| 2 | +import "firebase/analytics"; |
| 3 | + |
| 4 | +// [START analytics_ecommerce_items] |
| 5 | +// A pair of jeggings |
| 6 | +const item_jeggings = { |
| 7 | + item_id: 'SKU_123', |
| 8 | + item_name: 'jeggings', |
| 9 | + item_category: 'pants', |
| 10 | + item_variant: 'black', |
| 11 | + item_brand: 'Google', |
| 12 | + price: 9.99 |
| 13 | +}; |
| 14 | + |
| 15 | +// A pair of boots |
| 16 | +const item_boots = { |
| 17 | + item_id: 'SKU_456', |
| 18 | + item_name: 'boots', |
| 19 | + item_category: 'shoes', |
| 20 | + item_variant: 'brown', |
| 21 | + item_brand: 'Google', |
| 22 | + price: 24.99 |
| 23 | +}; |
| 24 | + |
| 25 | +// A pair of socks |
| 26 | +const item_socks = { |
| 27 | + item_id: 'SKU_789', |
| 28 | + item_name: 'ankle_socks', |
| 29 | + item_category: 'socks', |
| 30 | + item_variant: 'red', |
| 31 | + item_brand: 'Google', |
| 32 | + price: 5.99 |
| 33 | +}; |
| 34 | +// [END analytics_ecommerce_items] |
| 35 | + |
| 36 | +function ecommerceViewItemList() { |
| 37 | + // [START analytics_ecommerce_view_item_list] |
| 38 | + // Prepare ecommerce params |
| 39 | + const params1 = { |
| 40 | + item_list_id: 'L001', |
| 41 | + item_list_name: 'Related products', |
| 42 | + items: [item_jeggings, item_boots, item_socks] |
| 43 | + }; |
| 44 | + |
| 45 | + // Log event |
| 46 | + firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_ITEM_LIST, params1); |
| 47 | + // [END analytics_ecommerce_view_item_list] |
| 48 | +} |
| 49 | + |
| 50 | +function ecommerceSelectItem() { |
| 51 | + // [START analytics_ecommerce_select_item] |
| 52 | + // Prepare ecommerce event params |
| 53 | + const params2 = { |
| 54 | + item_list_id: 'L001', |
| 55 | + item_list_name: 'Related products', |
| 56 | + items: [item_jeggings] |
| 57 | + }; |
| 58 | + |
| 59 | + // Log event |
| 60 | + firebase.analytics().logEvent(firebase.analytics.EventName.SELECT_ITEM, params2); |
| 61 | + // [END analytics_ecommerce_select_item] |
| 62 | +} |
| 63 | + |
| 64 | +function ecommerceViewItemDetails() { |
| 65 | + // [START analytics_ecommerce_view_item_details] |
| 66 | + // Prepare ecommerce event params |
| 67 | + const params3 = { |
| 68 | + currency: 'USD', |
| 69 | + value: 9.99, |
| 70 | + items: [item_jeggings] |
| 71 | + }; |
| 72 | + |
| 73 | + // Log event |
| 74 | + firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_ITEM, params3); |
| 75 | + // [END analytics_ecommerce_view_item_details] |
| 76 | +} |
| 77 | + |
| 78 | +function ecommerceAddCart() { |
| 79 | + // [START analytics_ecommerce_add_cart] |
| 80 | + // Specify order quantity |
| 81 | + const item_jeggings_quantity = { |
| 82 | + ...item_jeggings, |
| 83 | + quantity: 2 |
| 84 | + }; |
| 85 | + |
| 86 | + // Prepare ecommerce bundle |
| 87 | + const params4 = { |
| 88 | + currency: 'USD', |
| 89 | + value: 19.98, |
| 90 | + items: [item_jeggings_quantity] |
| 91 | + }; |
| 92 | + |
| 93 | + // Log event when a product is added to a wishlist |
| 94 | + firebase.analytics().logEvent(firebase.analytics.EventName.ADD_TO_WISHLIST, params4); |
| 95 | + |
| 96 | + // Log event when a product is added to the cart |
| 97 | + firebase.analytics().logEvent(firebase.analytics.EventName.ADD_TO_CART, params4); |
| 98 | + // [END analytics_ecommerce_add_cart] |
| 99 | +} |
| 100 | + |
| 101 | +function ecommerceViewCart() { |
| 102 | + // [START analytics_ecommerce_view_cart] |
| 103 | + // Specify order quantity |
| 104 | + const item_jeggings_quantity = { |
| 105 | + ...item_jeggings, |
| 106 | + quantity: 2 |
| 107 | + }; |
| 108 | + |
| 109 | + const item_boots_quantity = { |
| 110 | + ...item_boots, |
| 111 | + quantity: 1 |
| 112 | + }; |
| 113 | + |
| 114 | + // Prepare ecommerce params |
| 115 | + const params5 = { |
| 116 | + currency: 'USD', |
| 117 | + value: 44.97, |
| 118 | + items: [item_jeggings_quantity, item_boots_quantity] |
| 119 | + }; |
| 120 | + |
| 121 | + // Log event when the cart is viewed |
| 122 | + firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_CART, params5); |
| 123 | + // [END analytics_ecommerce_view_cart] |
| 124 | +} |
| 125 | + |
| 126 | +function ecommerceRemoveCart() { |
| 127 | + // [START analytics_ecommerce_remove_cart] |
| 128 | + // Prepare ecommerce params |
| 129 | + const params6 = { |
| 130 | + currency: 'USD', |
| 131 | + value: 24.99, |
| 132 | + items: [item_jeggings] |
| 133 | + }; |
| 134 | + |
| 135 | + // Log event |
| 136 | + firebase.analytics().logEvent(firebase.analytics.EventName.REMOVE_FROM_CART, params6); |
| 137 | + // [END analytics_ecommerce_remove_cart] |
| 138 | +} |
| 139 | + |
| 140 | +function ecommerceCheckout() { |
| 141 | + // [START analytics_ecommerce_checkout] |
| 142 | + // Prepare ecommerce params |
| 143 | + const params7 = { |
| 144 | + currency: 'USD', |
| 145 | + value: 14.98, // Total Revenue |
| 146 | + coupon: 'SUMMER_FUN', |
| 147 | + items: [item_jeggings] |
| 148 | + }; |
| 149 | + |
| 150 | + // Log event |
| 151 | + firebase.analytics().logEvent(firebase.analytics.EventName.BEGIN_CHECKOUT, params7); |
| 152 | + // [END analytics_ecommerce_checkout] |
| 153 | +} |
| 154 | + |
| 155 | +function ecommerceShippingInfo() { |
| 156 | + // [START analytics_ecommerce_shipping_info] |
| 157 | + // Prepare ecommerce params |
| 158 | + const params8 = { |
| 159 | + currency: 'USD', |
| 160 | + value: 14.98, // Total Revenue |
| 161 | + coupon: 'SUMMER_FUN', |
| 162 | + shipping_tier: 'Ground', |
| 163 | + items: [item_jeggings] |
| 164 | + }; |
| 165 | + |
| 166 | + // Log event |
| 167 | + firebase.analytics().logEvent(firebase.analytics.EventName.ADD_SHIPPING_INFO, params8); |
| 168 | + // [END analytics_ecommerce_shipping_info] |
| 169 | +} |
| 170 | + |
| 171 | +function ecommercePaymentInfo() { |
| 172 | + // [START analytics_ecommerce_payment_info] |
| 173 | + // Prepare ecommerce params |
| 174 | + const params9 = { |
| 175 | + currency: 'USD', |
| 176 | + value: 14.98, // Total Revenue |
| 177 | + coupon: 'SUMMER_FUN', |
| 178 | + payment_type: 'Visa', |
| 179 | + items: [item_jeggings] |
| 180 | + }; |
| 181 | + |
| 182 | + // Log event |
| 183 | + firebase.analytics().logEvent(firebase.analytics.EventName.ADD_PAYMENT_INFO, params9); |
| 184 | + // [END analytics_ecommerce_payment_info] |
| 185 | +} |
| 186 | + |
| 187 | +function ecommercePurchase() { |
| 188 | + // [START analytics_ecommerce_purchase] |
| 189 | + // Prepare ecommerce bundle |
| 190 | + const params10 = { |
| 191 | + transaction_id: 'T12345', |
| 192 | + affiliation: 'Google Store', |
| 193 | + currency: 'USD', |
| 194 | + value: 14.98, // Total Revenue |
| 195 | + tax: 2.85, |
| 196 | + shipping: 5.34, |
| 197 | + coupon: 'SUMMER_FUN', |
| 198 | + items: [item_jeggings] |
| 199 | + }; |
| 200 | + |
| 201 | + // Log event |
| 202 | + firebase.analytics().logEvent(firebase.analytics.EventName.PURCHASE, params10); |
| 203 | + // [END analytics_ecommerce_purchase] |
| 204 | +} |
| 205 | + |
| 206 | +function ecommerceRefund() { |
| 207 | + // [START analytics_ecommerce_refund] |
| 208 | + // Prepare ecommerce params |
| 209 | + const params11 = { |
| 210 | + transaction_id: 'T12345', // Required |
| 211 | + affiliation: 'Google Store', |
| 212 | + currency: 'USD', |
| 213 | + value: 9.99, |
| 214 | + items: [] |
| 215 | + }; |
| 216 | + |
| 217 | + // (Optional) For partial refunds, define the item_id and quantity of refunded items |
| 218 | + const refundedProduct = { |
| 219 | + item_id: 'SKU_123', // Required |
| 220 | + quantity: 1 // Required |
| 221 | + }; |
| 222 | + |
| 223 | + params11.items.push(refundedProduct); |
| 224 | + |
| 225 | + // Log event |
| 226 | + firebase.analytics().logEvent(firebase.analytics.EventName.REFUND, params11); |
| 227 | + // [END analytics_ecommerce_refund] |
| 228 | +} |
| 229 | + |
| 230 | +function ecommercePromotions() { |
| 231 | + // [START analytics_ecommerce_promotions] |
| 232 | + // Prepare ecommerce params |
| 233 | + const params12 = { |
| 234 | + promotion_id: 'ABC123', |
| 235 | + promotion_name: 'Summer Sale', |
| 236 | + creative_name: 'summer2020_promo.jpg', |
| 237 | + creative_slot: 'featured_app_1', |
| 238 | + location_id: 'HERO_BANNER', |
| 239 | + items: [item_jeggings] |
| 240 | + }; |
| 241 | + |
| 242 | + // Log event when a promotion is displayed |
| 243 | + firebase.analytics().logEvent(firebase.analytics.EventName.VIEW_PROMOTION, params12); |
| 244 | + |
| 245 | + // Log event when a promotion is selected |
| 246 | + firebase.analytics().logEvent(firebase.analytics.EventName.SELECT_PROMOTION, params12); |
| 247 | + // [END analytics_ecommerce_promotions] |
| 248 | +} |
0 commit comments