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: send deviceType to GTM #2430

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 14 additions & 6 deletions src/services/analytics/gtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
GOOGLE_TAG_MANAGER_DEVELOPMENT_AUTH,
} from '@/config/constants'
import type { AnalyticsEvent, EventLabel, SafeAppSDKEvent } from './types'
import { EventType } from './types'
import { EventType, DeviceType } from './types'
import { SAFE_APPS_SDK_CATEGORY } from './events'
import { getAbTest } from '../tracking/abTesting'
import type { AbTest } from '../tracking/abTesting'
Expand All @@ -40,10 +40,17 @@ const GTM_ENV_AUTH: Record<GTMEnvironment, GTMEnvironmentArgs> = {
},
}

let _chainId: string = ''
const commonEventParams = {
chainId: '',
deviceType: DeviceType.DESKTOP,
}

export const gtmSetChainId = (chainId: string): void => {
_chainId = chainId
commonEventParams.chainId = chainId
}

export const gtmSetDeviceType = (type: DeviceType): void => {
commonEventParams.deviceType = type
}

export const gtmInit = (): void => {
Expand All @@ -66,6 +73,7 @@ export const gtmDisableCookies = TagManager.disableCookies
type GtmEvent = {
event: EventType
chainId: string
deviceType: DeviceType
abTest?: AbTest
}

Expand All @@ -92,8 +100,8 @@ const gtmSend = TagManager.dataLayer

export const gtmTrack = (eventData: AnalyticsEvent): void => {
const gtmEvent: ActionGtmEvent = {
...commonEventParams,
event: eventData.event || EventType.CLICK,
chainId: _chainId,
eventCategory: eventData.category,
eventAction: eventData.action,
}
Expand Down Expand Up @@ -122,8 +130,8 @@ export const gtmTrack = (eventData: AnalyticsEvent): void => {

export const gtmTrackPageview = (pagePath: string): void => {
const gtmEvent: PageviewGtmEvent = {
...commonEventParams,
event: EventType.PAGEVIEW,
chainId: _chainId,
pageLocation: `${location.origin}${pagePath}`,
pagePath,
}
Expand All @@ -142,8 +150,8 @@ export const normalizeAppName = (appName?: string): string => {

export const gtmTrackSafeApp = (eventData: AnalyticsEvent, appName?: string, sdkEventData?: SafeAppSDKEvent): void => {
const safeAppGtmEvent: SafeAppGtmEvent = {
...commonEventParams,
event: EventType.SAFE_APP,
chainId: _chainId,
eventCategory: eventData.category,
eventAction: eventData.action,
safeAppName: normalizeAppName(appName),
Expand Down
6 changes: 6 additions & 0 deletions src/services/analytics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ export type SafeAppSDKEvent = {
ethMethod: string
version: string
}

export enum DeviceType {
DESKTOP = 'desktop',
MOBILE = 'mobile',
TABLET = 'tablet',
}
23 changes: 21 additions & 2 deletions src/services/analytics/useGtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@
* The hook needs to be called when the app starts.
*/
import { useEffect, useState } from 'react'
import { gtmInit, gtmTrackPageview, gtmSetChainId, gtmEnableCookies, gtmDisableCookies } from '@/services/analytics/gtm'
import { useTheme } from '@mui/material/styles'
import {
gtmInit,
gtmTrackPageview,
gtmSetChainId,
gtmEnableCookies,
gtmDisableCookies,
gtmSetDeviceType,
} from '@/services/analytics/gtm'
import { useAppSelector } from '@/store'
import { CookieType, selectCookies } from '@/store/cookiesSlice'
import useChainId from '@/hooks/useChainId'
import { useRouter } from 'next/router'
import { AppRoutes } from '@/config/routes'
import useMetaEvents from './useMetaEvents'
import { useMediaQuery } from '@mui/material'
import { DeviceType } from './types'

const useGtm = () => {
const chainId = useChainId()
const cookies = useAppSelector(selectCookies)
const isAnalyticsEnabled = cookies[CookieType.ANALYTICS] || false
const [, setPrevAnalytics] = useState(isAnalyticsEnabled)
const router = useRouter()
const theme = useTheme()
const isMobile = useMediaQuery(theme.breakpoints.down('sm'))
const isTablet = useMediaQuery(theme.breakpoints.down('md'))
const deviceType = isMobile ? DeviceType.MOBILE : isTablet ? DeviceType.TABLET : DeviceType.DESKTOP

// Initialize GTM
useEffect(() => {
Expand All @@ -39,11 +53,16 @@ const useGtm = () => {
})
}, [isAnalyticsEnabled])

// Set the chain ID for GTM
// Set the chain ID for all GTM events
useEffect(() => {
gtmSetChainId(chainId)
}, [chainId])

// Set device type for all GTM events
useEffect(() => {
gtmSetDeviceType(deviceType)
}, [deviceType])

// Track page views – anononimized by default.
// Sensitive info, like the safe address or tx id, is always in the query string, which we DO NOT track.
useEffect(() => {
Expand Down