-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 integrate google analytics query into the prototype (#86)
* Add basic event tracking * Add tracked events
- Loading branch information
1 parent
e1088d1
commit 8532087
Showing
6 changed files
with
219 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { createContext } from 'preact'; | ||
import { PropsWithChildren, useCallback, useContext, useEffect, useRef, useState } from 'preact/compat'; | ||
import { useAccount } from 'wagmi'; | ||
import { INPUT_TOKEN_CONFIG, OUTPUT_TOKEN_CONFIG } from '../constants/tokenConfig'; | ||
import { OfframpingState } from '../services/offrampingFlow'; | ||
|
||
declare global { | ||
interface Window { | ||
dataLayer: Record<string, any>[]; | ||
} | ||
} | ||
|
||
const UNIQUE_EVENT_TYPES = ['amount_type', 'click_details', 'click_support']; | ||
|
||
export interface AmountTypeEvent { | ||
event: `amount_type`; | ||
} | ||
|
||
export interface ClickDetailsEvent { | ||
event: 'click_details'; | ||
} | ||
|
||
export interface WalletConnectEvent { | ||
event: 'wallet_connect'; | ||
wallet_action: 'connect' | 'disconnect' | 'change'; | ||
} | ||
|
||
export interface TransactionEvent { | ||
event: 'transaction_confirmation' | 'kyc_completed' | 'transaction_success' | 'transaction_failure'; | ||
from_asset: string; | ||
to_asset: string; | ||
from_amount: string; | ||
to_amount: string; | ||
} | ||
|
||
export interface ClickSupportEvent { | ||
event: 'click_support'; | ||
transaction_status: 'success' | 'failure'; | ||
} | ||
|
||
export type TrackableEvent = | ||
| AmountTypeEvent | ||
| ClickDetailsEvent | ||
| WalletConnectEvent | ||
| TransactionEvent | ||
| ClickSupportEvent; | ||
|
||
type EventType = TrackableEvent['event']; | ||
|
||
type UseEventsContext = ReturnType<typeof useEvents>; | ||
const useEvents = () => { | ||
const [_, setTrackedEventTypes] = useState<Set<EventType>>(new Set()); | ||
|
||
const previousAddress = useRef<`0x${string}` | undefined>(undefined); | ||
const { address } = useAccount(); | ||
|
||
const trackEvent = useCallback( | ||
(event: TrackableEvent) => { | ||
setTrackedEventTypes((trackedEventTypes) => { | ||
if (UNIQUE_EVENT_TYPES.includes(event.event)) { | ||
if (trackedEventTypes.has(event.event)) { | ||
return trackedEventTypes; | ||
} else { | ||
trackedEventTypes = new Set(trackedEventTypes); | ||
trackedEventTypes.add(event.event); | ||
} | ||
} | ||
console.log('Push data layer', event); | ||
|
||
window.dataLayer.push(event); | ||
|
||
return trackedEventTypes; | ||
}); | ||
}, | ||
[setTrackedEventTypes], | ||
); | ||
|
||
useEffect(() => { | ||
const wasConnected = previousAddress.current !== undefined; | ||
const isConnected = address !== undefined; | ||
|
||
if (!isConnected) { | ||
trackEvent({ event: 'wallet_connect', wallet_action: 'disconnect' }); | ||
} else { | ||
trackEvent({ event: 'wallet_connect', wallet_action: wasConnected ? 'change' : 'connect' }); | ||
} | ||
|
||
previousAddress.current = address; | ||
}, [address]); | ||
|
||
return { | ||
trackEvent, | ||
}; | ||
}; | ||
|
||
const Context = createContext<UseEventsContext | undefined>(undefined); | ||
|
||
export const useEventsContext = () => { | ||
const contextValue = useContext(Context); | ||
if (contextValue === undefined) { | ||
throw new Error('Context must be inside a Provider'); | ||
} | ||
|
||
return contextValue; | ||
}; | ||
|
||
export function EventsProvider({ children }: PropsWithChildren) { | ||
const useEventsResult = useEvents(); | ||
|
||
return <Context.Provider value={useEventsResult}>{children}</Context.Provider>; | ||
} | ||
|
||
export function createTransactionEvent(type: TransactionEvent['event'], state: OfframpingState) { | ||
return { | ||
event: type, | ||
from_asset: INPUT_TOKEN_CONFIG[state.inputTokenType].assetSymbol, | ||
to_asset: OUTPUT_TOKEN_CONFIG[state.outputTokenType].stellarAsset.code.string, | ||
from_amount: state.inputAmount.units, | ||
to_amount: state.outputAmount.units, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters