Skip to content

Commit

Permalink
improv(SWA-144): update the FA tracking implementation due to the new…
Browse files Browse the repository at this point in the history
… API (#1908)
  • Loading branch information
ElRodrigote authored Feb 7, 2024
1 parent 8b6b84f commit eca3f60
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/analytics/fathom/fathom.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Fathom {
enableTrackingForMe(): void
setSite(siteId: string): void
trackGoal(goalId: string, data: any): void
trackEvent(eventId: string, payload: Record<string, any>): void
trackEvent(eventName: string, opt?: { _site_id?: string; _value?: number }): void
trackPageview(params?: Record<string, any>): void
}

Expand Down
12 changes: 3 additions & 9 deletions src/analytics/hooks/useSimpleAnalyticsEvent.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { useAnalytics } from './analytics.hooks'

export const useSimpleAnalyticsEvent = () => {
const { site, fathom } = useAnalytics()
const { site } = useAnalytics()

const trackEvent = (eventName: string) => {
const trackEvent = (eventName: string, opt?: { _site_id: string; _value: number }) => {
if (!site || !window.fathom) {
return console.error('Fathom site not found', { site, fathom: window.fathom })
}

const eventId = site.events.find(({ name }) => name === eventName)?.id

if (!eventId) {
return console.error(`Event ID for (${eventName}) not found in site (${site.siteId})`)
}

fathom.trackGoal(eventId, 0)
window.fathom.trackEvent(eventName, opt)
}

return trackEvent
Expand Down
10 changes: 4 additions & 6 deletions src/analytics/trackers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ export async function trackEcoRouterTradeVolume(
})
}

const tradeUSDValueInCents = (parseFloat(parseFloat(tradeUSDValue).toFixed(2)) * 100).toString() // convert to cents because fathom requires it
const tradeUSDValueInCents = parseFloat(parseFloat(tradeUSDValue).toFixed(2)) * 100
const networkName = getNetworkNameByChainId(trade.chainId as number)
const chartOptionString = chartOptionToString[chartOption]
const eventName = getEcoRouterVolumeUSDEventName(networkName, trade.chainId, trade.platform.name, chartOptionString)
const eventId = site.events.find(event => event.name === eventName)?.id

if (!eventId) {
throw new Error(`Event ID for (${eventName}) not found in site (${site.siteId})`)
}
window.fathom.trackGoal(eventId, tradeUSDValueInCents)
window.fathom.trackEvent(eventName, {
_value: tradeUSDValueInCents,
})
}

/**
Expand Down

0 comments on commit eca3f60

Please sign in to comment.