Skip to content

Commerce, Auth: Refactor analytics util into a service with wrappers for each event #119

@erikrakuscek

Description

@erikrakuscek

Create analytics service in hanzo/commerce with handlers for events:

  • add_to_cart (GA4), AddToCart (Pixel)
  • remove_from_cart (GA4)
  • begin_checkout (GA4), InitiateCheckout (Pixel)
  • add_shipping_info (GA4)
  • add_payment_info (GA4), AddPaymentInfo (Pixel)
  • purchase (GA4), Purchase (Pixel)
  • view_cart (GA4)

Create an analytics service in hanzo/auth with handlers for events:

  • login (GA4)

For example, instead of this:

sendGAEvent('purchase', {
  transaction_id: res.payment?.id,
  value: res.payment?.amountMoney?.amount,
  currency: res.payment?.amountMoney?.currency,
  items: cmmc.cartItems.map((item) => ({
    item_id: item.sku,
    item_name: item.title,
    item_category: item.categoryId,
    price: item.price,
    quantity: item.quantity
  })),
})
sendFBEvent('Purchase', {
  content_ids: cmmc.cartItems.map((item) => item.sku),
  contents: cmmc.cartItems.map(item => ({
    id: item.sku,
    quantity: item.quantity
  })),
  num_items: cmmc.cartItems.length,
  value: cmmc.cartTotal,
  currency: 'USD',
})

we will have this:

import analytics from '@hanzo/commerce'

// ...

const cmmc = useCommerce()

analytics.signalPurchase(cmmc, payment)

// ...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions