Skip to content

Commit

Permalink
Feat: track app version in all events
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Nov 21, 2023
1 parent 09a88f6 commit e7af3ef
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
132 changes: 127 additions & 5 deletions src/services/analytics/__tests__/gtm.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,140 @@
import { normalizeAppName } from '../gtm'
import * as gtm from '../gtm'
import TagManager from '../TagManager'
import { EventType, DeviceType } from '../types'

// Mock dependencies
jest.mock('../TagManager', () => ({
initialize: jest.fn(),
dataLayer: jest.fn(),
enableCookies: jest.fn(),
disableCookies: jest.fn(),
setUserProperty: jest.fn(),
}))

const FAKE_SAFE_APP_NAME = 'Safe App'
const FAKE_DOMAIN = 'http://domain.crypto'

describe('gtm', () => {
// Reset mocks before each test
beforeEach(() => {
jest.clearAllMocks()
})
describe('normalizeAppName', () => {
it('should return the app name if is not an URL', () => {
expect(normalizeAppName(FAKE_SAFE_APP_NAME)).toBe(FAKE_SAFE_APP_NAME)
expect(gtm.normalizeAppName(FAKE_SAFE_APP_NAME)).toBe(FAKE_SAFE_APP_NAME)
})

it('should strip the querystring or hash when is an URL', () => {
expect(normalizeAppName(FAKE_DOMAIN)).toBe(FAKE_DOMAIN)
expect(normalizeAppName(`${FAKE_DOMAIN}?q1=query1&q2=query2`)).toBe(FAKE_DOMAIN)
expect(normalizeAppName(`${FAKE_DOMAIN}#hash`)).toBe(FAKE_DOMAIN)
expect(gtm.normalizeAppName(FAKE_DOMAIN)).toBe(FAKE_DOMAIN)
expect(gtm.normalizeAppName(`${FAKE_DOMAIN}?q1=query1&q2=query2`)).toBe(FAKE_DOMAIN)
expect(gtm.normalizeAppName(`${FAKE_DOMAIN}#hash`)).toBe(FAKE_DOMAIN)
})
})

describe('gtmInit', () => {
gtm.gtmInit()

expect(TagManager.initialize).toHaveBeenCalledWith({
auth: expect.any(String),
gtmId: expect.any(String),
preview: 'env-3',
})
})

describe('gtmTrack', () => {
it('should send correct data to the dataLayer', () => {
const mockEventData = {
event: EventType.CLICK,
category: 'testCategory',
action: 'testAction',
chainId: '1234',
label: 'testLabel',
}

gtm.gtmTrack(mockEventData)

expect(TagManager.dataLayer).toHaveBeenCalledWith(
expect.objectContaining({
event: mockEventData.event,
eventCategory: mockEventData.category,
eventAction: mockEventData.action,
chainId: mockEventData.chainId,
eventLabel: mockEventData.label,
appVersion: expect.any(String),
deviceType: DeviceType.DESKTOP,
}),
)
})

it('should set the chain ID correctly', () => {
const testChainId = '1234'
gtm.gtmSetChainId(testChainId)

const mockEventData = {
event: EventType.CLICK,
category: 'testCategory',
action: 'testAction',
label: 'testLabel',
}

gtm.gtmTrack(mockEventData)

expect(TagManager.dataLayer).toHaveBeenCalledWith(
expect.objectContaining({
event: mockEventData.event,
eventCategory: mockEventData.category,
eventAction: mockEventData.action,
chainId: testChainId,
eventLabel: mockEventData.label,
appVersion: expect.any(String),
deviceType: DeviceType.DESKTOP,
}),
)
})
})

describe('gtmTrackSafeApp', () => {
it('should send correct data to the dataLayer for a Safe App event', () => {
Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: '/apps',
},
})

const mockEventData = {
event: EventType.SAFE_APP,
category: 'testCategory',
action: 'testAction',
label: 'testLabel',
chainId: '1234',
}

const mockAppName = 'Test App'
const mockSdkEventData = {
method: 'testMethod',
ethMethod: 'testEthMethod',
version: '1.0.0',
}

gtm.gtmTrackSafeApp(mockEventData, mockAppName, mockSdkEventData)

expect(TagManager.dataLayer).toHaveBeenCalledWith(
expect.objectContaining({
appVersion: expect.any(String),
chainId: expect.any(String),
deviceType: DeviceType.DESKTOP,
event: EventType.SAFE_APP,
eventAction: 'testAction',
eventCategory: 'testCategory',
eventLabel: 'testLabel',
safeAddress: '',
safeAppEthMethod: '',
safeAppMethod: '',
safeAppName: 'Test App',
safeAppSDKVersion: '',
}),
)
})
})
})
2 changes: 2 additions & 0 deletions src/services/analytics/gtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SAFE_APPS_SDK_CATEGORY } from './events'
import { getAbTest } from '../tracking/abTesting'
import type { AbTest } from '../tracking/abTesting'
import { AppRoutes } from '@/config/routes'
import packageJson from '../../../package.json'

type GTMEnvironment = 'LIVE' | 'LATEST' | 'DEVELOPMENT'
type GTMEnvironmentArgs = Required<Pick<TagManagerArgs, 'auth' | 'preview'>>
Expand All @@ -42,6 +43,7 @@ const GTM_ENV_AUTH: Record<GTMEnvironment, GTMEnvironmentArgs> = {
}

const commonEventParams = {
appVersion: packageJson.version,
chainId: '',
deviceType: DeviceType.DESKTOP,
safeAddress: '',
Expand Down

0 comments on commit e7af3ef

Please sign in to comment.