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

feat(widget): google analytics dimension for injected widget app id #3272

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/cowswap-frontend/src/common/hooks/useAnalyticsReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import ReactGA from 'react-ga4'
import { useLocation } from 'react-router-dom'
import { getCLS, getFCP, getFID, getLCP, Metric } from 'web-vitals'

import { useInjectedWidgetMetaData } from 'modules/injectedWidget'

import { useGetMarketDimension } from './useGetMarketDimension'

export function sendTiming(timingCategory: any, timingVar: any, timingValue: any, timingLabel: any) {
Expand Down Expand Up @@ -58,6 +60,7 @@ export function useAnalyticsReporter() {
const { connector } = useWeb3React()
const { chainId, account } = useWalletInfo()
const { walletName: _walletName } = useWalletDetails()
const injectedWidgetMetaData = useInjectedWidgetMetaData()
const prevAccount = usePrevious(account)

const marketDimension = useGetMarketDimension()
Expand Down Expand Up @@ -93,6 +96,15 @@ export function useAnalyticsReporter() {
googleAnalytics.setDimension(Dimensions.market, marketDimension)
}, [marketDimension])

useEffect(() => {
// Custom dimension 6 - injected widget app id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add it also in Google Analytics side?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was your idea at the last meeting 😄

const injectedWidgetAppId = injectedWidgetMetaData
? `${injectedWidgetMetaData.appKey}:${injectedWidgetMetaData.url}`
: ''

googleAnalytics.setDimension(Dimensions.injectedWidgetAppId, injectedWidgetAppId)
}, [injectedWidgetMetaData])
shoom3301 marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
googleAnalytics.pageview(`${pathname}${search}`)
}, [pathname, search])
Expand Down
6 changes: 3 additions & 3 deletions apps/cowswap-frontend/src/modules/appData/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { DEFAULT_APP_CODE, SAFE_APP_CODE } from '@cowprotocol/common-const'
import { isInjectedWidget } from '@cowprotocol/common-utils'
import { useIsSafeApp } from '@cowprotocol/wallet'

import { useInjectedWidgetMetaData } from 'modules/injectedWidget'

import { addAppDataToUploadQueueAtom, appDataHooksAtom, appDataInfoAtom } from './state/atoms'
import { AppDataInfo } from './types'

import { injectedWidgetMetaDataAtom } from '../injectedWidget/state/injectedWidgetMetaDataAtom'

const APP_CODE = process.env.REACT_APP_APP_CODE

export function useAppData(): AppDataInfo | null {
return useAtomValue(appDataInfoAtom)
}

export function useAppCode(): string | null {
const injectedWidgetMetaData = useAtomValue(injectedWidgetMetaDataAtom)
const injectedWidgetMetaData = useInjectedWidgetMetaData()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick

not comming from this PR, but in principle not sure we want to make all modules aware there's an injected mode and instead is the client of this module informing one time whats the appKey

This way, in injected mode, it will inject the appData, otherwise it will get it from the env.

Otherwise, we leak a lot of CoW Swap logic here. Imagine we want to promote this module to a lib and use for other projects, then the injected mode would be bothering us.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I also feel it's not the best solution. However, I don't see any better ways

const isSafeApp = useIsSafeApp()

return useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useAtomValue } from 'jotai'

import { InjectedWidgetMetaData, injectedWidgetMetaDataAtom } from '../state/injectedWidgetMetaDataAtom'

export function useInjectedWidgetMetaData(): InjectedWidgetMetaData | null {
return useAtomValue(injectedWidgetMetaDataAtom)
}
5 changes: 3 additions & 2 deletions apps/cowswap-frontend/src/modules/injectedWidget/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './updaters/InjectedWidgetUpdater'
export * from './hooks/useInjectedWidgetParams'
export { InjectedWidgetUpdater } from './updaters/InjectedWidgetUpdater'
export { useInjectedWidgetParams } from './hooks/useInjectedWidgetParams'
export { useInjectedWidgetMetaData } from './hooks/useInjectedWidgetMetaData'
2 changes: 2 additions & 0 deletions libs/analytics/src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DIMENSION_MAP = {
[Dimensions.customBrowserType]: 'dimension3',
[Dimensions.userAddress]: 'dimension4',
[Dimensions.market]: 'dimension5',
[Dimensions.injectedWidgetAppId]: 'dimension6',
}

type DimensionKey = keyof typeof DIMENSION_MAP
Expand All @@ -30,6 +31,7 @@ export class GAProvider {
customBrowserType: '',
userAddress: '',
market: '',
injectedWidgetAppId: '',
}
}

Expand Down
1 change: 1 addition & 0 deletions libs/analytics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum Dimensions {
customBrowserType = 'customBrowserType',
userAddress = 'userAddress',
market = 'market',
injectedWidgetAppId = 'injectedWidgetAppId',
}

export type AnalyticsOrderType = OrderClass | 'TWAP'
Loading