Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
erikrakuscek opened this issue Mar 26, 2024 · 1 comment
Assignees
Labels
launch refactor / internal an internal improvement
Milestone

Comments

@erikrakuscek
Copy link
Contributor

erikrakuscek commented Mar 26, 2024

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)

// ...
@erikrakuscek erikrakuscek added this to the Launch milestone Mar 26, 2024
@artemis-prime artemis-prime added refactor / internal an internal improvement launch labels Apr 2, 2024
@artemis-prime artemis-prime removed their assignment Apr 2, 2024
@erikrakuscek
Copy link
Contributor Author

Discussed possible approaches for this refactor with @artemis-prime. We want to clean up the code and reduce analytics boilerplate inside UI components. So we are proposing the following approach:
Create an analytics folder in root of hanzo/commerce. In this folder add send.ts file:

declare global {
  interface Window {
    fbq: Function;
    gtag: Function;
  }
}

// https://developers.facebook.com/docs/meta-pixel/reference
const sendFBEvent = (name: string, options = {}) => {
  window.fbq('track', name, options);
}

// https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag
const sendGAEvent = (name: string, options = {}) => {
  window.gtag('event', name, options);
}

export {
  sendFBEvent,
  sendGAEvent
}

in index.ts export a separate function for each event type, for example trackPurchase, trackAddToCart, trackAddShippingInfo... Separate the code for each event type into it's own file.

Do the same inside hanzo/auth for trackLogin.

Keep analytics initialization scripts in hanzo/common.

We considered creating a new module hanzo/analytics, however to avoid circular dependencies between hanzo/analytics and hanzo/commerce we decided against it.

@zeekay zeekay assigned zeekay and unassigned erikrakuscek May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
launch refactor / internal an internal improvement
Projects
None yet
Development

No branches or pull requests

3 participants