Skip to content

Commit

Permalink
fix: move user properties to types.ts and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Oct 25, 2023
1 parent 108adb8 commit a81a348
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/services/analytics/__tests__/TagManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Cookies from 'js-cookie'

import * as gtm from '../TagManager'
import { AnalyticsUserProperties } from '../types'

const { default: TagManager } = gtm

Expand Down Expand Up @@ -135,4 +136,28 @@ describe('TagManager', () => {
expect(global.location.reload).toHaveBeenCalled()
})
})

describe('TagManager.setUserProperty', () => {
it('should push new user properties to dataLayer', () => {
expect(window.dataLayer).toBeUndefined()

TagManager.initialize({
gtmId: MOCK_ID,
auth: MOCK_AUTH,
preview: MOCK_PREVIEW,
})

expect(window.dataLayer).toHaveLength(3)

TagManager.setUserProperty(AnalyticsUserProperties.WALLET_LABEL, 'Safe{Wallet}')

expect(window.dataLayer).toHaveLength(4)

expect(Array.from(window.dataLayer?.[3])).toEqual([
'set',
'user_properties',
{ [AnalyticsUserProperties.WALLET_LABEL]: 'Safe{Wallet}' },
])
})
})
})
6 changes: 6 additions & 0 deletions src/services/analytics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ export enum DeviceType {
MOBILE = 'mobile',
TABLET = 'tablet',
}

export enum AnalyticsUserProperties {
WALLET_LABEL = 'walletLabel',
}

export const WALLET_LABEL_NONE = 'NONE'
10 changes: 2 additions & 8 deletions src/services/analytics/useGtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ import { useRouter } from 'next/router'
import { AppRoutes } from '@/config/routes'
import useMetaEvents from './useMetaEvents'
import { useMediaQuery } from '@mui/material'
import { DeviceType } from './types'
import { AnalyticsUserProperties, DeviceType, WALLET_LABEL_NONE } from './types'
import useSafeAddress from '@/hooks/useSafeAddress'
import useWallet from '@/hooks/wallets/useWallet'

enum GA_USER_PROPERTIES {
WALLET_LABEL = 'walletLabel',
}

const WALLET_LABEL_NONE = 'NONE'

const useGtm = () => {
const chainId = useChainId()
const cookies = useAppSelector(selectCookies)
Expand Down Expand Up @@ -89,7 +83,7 @@ const useGtm = () => {
}, [router.pathname])

useEffect(() => {
gtmSetUserProperty(GA_USER_PROPERTIES.WALLET_LABEL, walletLabel ?? WALLET_LABEL_NONE)
gtmSetUserProperty(AnalyticsUserProperties.WALLET_LABEL, walletLabel ?? WALLET_LABEL_NONE)
}, [walletLabel])

// Track meta events on app load
Expand Down

0 comments on commit a81a348

Please sign in to comment.