Skip to content

Commit dadfcbb

Browse files
authored
databeat forte events (#539)
* added event for forte payments * improved event tracking logic * simplified logix related to onSuccess event and tracking * set analytics options explicitly to true * undefined analytics object fix * imports cleanup * purchase currency symbol field
1 parent f2ce7fc commit dadfcbb

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

packages/checkout/src/components/SequenceCheckoutProvider/ForteController.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import { useAnalyticsContext } from '@0xsequence/connect'
12
import { useConfig } from '@0xsequence/hooks'
23
import { useEffect, useState } from 'react'
34

45
import { fetchFortePaymentStatus } from '../../api/data.js'
6+
import { EVENT_SOURCE } from '../../constants/index.js'
57
import { FortePaymentControllerProvider, useEnvironmentContext, type FortePaymentData } from '../../contexts/index.js'
68

79
const POLLING_TIME = 10 * 1000
810

911
export const ForteController = ({ children }: { children: React.ReactNode }) => {
12+
const { analytics } = useAnalyticsContext()
1013
const [fortePaymentData, setFortePaymentData] = useState<FortePaymentData>()
1114
const { forteWidgetUrl } = useEnvironmentContext()
1215
const [isSuccess, setIsSuccess] = useState(false)
@@ -56,6 +59,34 @@ export const ForteController = ({ children }: { children: React.ReactNode }) =>
5659
}
5760
}, [widgetInitialized, fortePaymentData])
5861

62+
const trackSuccessEvent = () => {
63+
const creditCardCheckout = fortePaymentData?.creditCardCheckout
64+
65+
if (!creditCardCheckout || !analytics) {
66+
return
67+
}
68+
69+
analytics?.track({
70+
event: 'SEND_TRANSACTION_REQUEST',
71+
props: {
72+
...creditCardCheckout.supplementaryAnalyticsInfo,
73+
type: 'credit_card',
74+
provider: 'forte',
75+
source: EVENT_SOURCE,
76+
chainId: String(creditCardCheckout.chainId),
77+
purchasedCurrencySymbol: creditCardCheckout.currencySymbol,
78+
purchasedCurrency: creditCardCheckout.currencyAddress,
79+
origin: window.location.origin,
80+
from: creditCardCheckout.recipientAddress,
81+
to: creditCardCheckout.contractAddress,
82+
item_ids: JSON.stringify([creditCardCheckout.nftId]),
83+
item_quantities: JSON.stringify([JSON.stringify([creditCardCheckout.nftQuantity])]),
84+
txHash: 'unavailable',
85+
paymentIntentId: fortePaymentData?.paymentIntentId || 'unavailable'
86+
}
87+
})
88+
}
89+
5990
useEffect(() => {
6091
let interval: NodeJS.Timeout | undefined
6192
let eventFortePaymentsWidgetClosedListener: (e: Event) => void
@@ -77,13 +108,11 @@ export const ForteController = ({ children }: { children: React.ReactNode }) =>
77108
eventFortePaymentsBuyNftMintSuccessListener = (e: Event) => {
78109
fortePaymentData?.creditCardCheckout?.forteConfig?.onFortePaymentsBuyNftMintSuccess?.(e)
79110
fortePaymentData.creditCardCheckout?.onClose?.()
80-
setIsSuccess(true)
81111
}
82112

83113
eventFortePaymentsBuyNftSuccessListener = (e: Event) => {
84114
fortePaymentData?.creditCardCheckout?.forteConfig?.onFortePaymentsBuyNftSuccess?.(e)
85115
fortePaymentData.creditCardCheckout?.onClose?.()
86-
setIsSuccess(true)
87116
}
88117

89118
// Note: these events are mutually exclusive. ie they won't trigger at the same time
@@ -135,6 +164,7 @@ export const ForteController = ({ children }: { children: React.ReactNode }) =>
135164

136165
if (status === 'Approved' && !isSuccess) {
137166
fortePaymentData.creditCardCheckout?.onSuccess?.()
167+
trackSuccessEvent()
138168
setIsSuccess(true)
139169
}
140170

packages/checkout/src/views/PendingCreditCardTransaction.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export const PendingCreditCardTransactionTransak = ({ skipOnCloseCallback }: Pen
151151
provider: 'transak',
152152
source: EVENT_SOURCE,
153153
chainId: String(creditCardCheckout.chainId),
154-
listedCurrency: creditCardCheckout.currencyAddress,
154+
purchasedCurrencySymbol: creditCardCheckout.currencySymbol,
155155
purchasedCurrency: creditCardCheckout.currencyAddress,
156156
origin: window.location.origin,
157157
from: creditCardCheckout.recipientAddress,
@@ -330,7 +330,7 @@ export const PendingCreditCardTransactionSardine = ({ skipOnCloseCallback }: Pen
330330
provider: 'sardine',
331331
source: EVENT_SOURCE,
332332
chainId: String(creditCardCheckout.chainId),
333-
listedCurrency: creditCardCheckout.currencyAddress,
333+
purchasedCurrencySymbol: creditCardCheckout.currencySymbol,
334334
purchasedCurrency: creditCardCheckout.currencyAddress,
335335
origin: window.location.origin,
336336
from: creditCardCheckout.recipientAddress,

packages/connect/src/components/SequenceConnectProvider/SequenceConnectProvider.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use client'
22

3-
import { sequence } from '0xsequence'
43
import { Button, Card, Modal, ModalPrimitive, Text, type Theme } from '@0xsequence/design-system'
54
import { SequenceHooksProvider } from '@0xsequence/hooks'
65
import { ChainId } from '@0xsequence/network'
7-
import { SequenceClient } from '@0xsequence/provider'
6+
import { SequenceClient, setupAnalytics, type Analytics } from '@0xsequence/provider'
87
import { GoogleOAuthProvider } from '@react-oauth/google'
98
import { AnimatePresence } from 'motion/react'
109
import React, { useEffect, useState } from 'react'
@@ -102,25 +101,23 @@ export const SequenceConnectProvider = (props: SequenceConnectProviderProps) =>
102101
) as ExtendedConnector | undefined
103102
const googleClientId: string = (googleWaasConnector as any)?.params?.googleClientId || ''
104103

105-
const setupAnalytics = (projectAccessKey: string) => {
106-
const s = sequence.initWallet(projectAccessKey)
107-
const sequenceAnalytics = s.client.analytics
108-
109-
if (sequenceAnalytics) {
110-
type TrackArgs = Parameters<typeof sequenceAnalytics.track>
111-
const originalTrack = sequenceAnalytics.track.bind(sequenceAnalytics)
112-
113-
sequenceAnalytics.track = (...args: TrackArgs) => {
114-
const [event] = args
115-
if (event && typeof event === 'object' && 'props' in event) {
116-
event.props = {
117-
...event.props,
118-
sdkType: 'sequence web sdk',
119-
version: WEB_SDK_VERSION
120-
}
104+
const getAnalyticsClient = (projectAccessKey: string) => {
105+
// @ts-ignore-next-line
106+
const sequenceAnalytics = setupAnalytics(projectAccessKey) as Analytics
107+
108+
type TrackArgs = Parameters<Analytics['track']>
109+
const originalTrack = sequenceAnalytics.track.bind(sequenceAnalytics)
110+
111+
sequenceAnalytics.track = (...args: TrackArgs) => {
112+
const [event] = args
113+
if (event && typeof event === 'object' && 'props' in event) {
114+
event.props = {
115+
...event.props,
116+
sdkType: 'sequence web sdk',
117+
version: WEB_SDK_VERSION
121118
}
122-
return originalTrack?.(...args)
123119
}
120+
return originalTrack?.(...args)
124121
}
125122
setAnalytics(sequenceAnalytics)
126123
}
@@ -138,7 +135,7 @@ export const SequenceConnectProvider = (props: SequenceConnectProviderProps) =>
138135

139136
useEffect(() => {
140137
if (!disableAnalytics) {
141-
setupAnalytics(config.projectAccessKey)
138+
getAnalyticsClient(config.projectAccessKey)
142139
}
143140
}, [])
144141

0 commit comments

Comments
 (0)