From e0e814aee3d86158def9294e5a26a3f61aaa0acf Mon Sep 17 00:00:00 2001 From: Stephanie Freitag <stephanie.freitag@holi.team> Date: Fri, 14 Feb 2025 16:42:19 +0100 Subject: [PATCH] HOLI-10936: remove dependency on consent from useTracking hook --- .../__tests__/TrackingInitializer.test.tsx | 7 +---- core/tracking/__tests__/useTracking.test.ts | 30 ++++++++----------- core/tracking/hooks/useTracking.ts | 7 ++--- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/core/tracking/__tests__/TrackingInitializer.test.tsx b/core/tracking/__tests__/TrackingInitializer.test.tsx index 8863a318ed..90495100e3 100644 --- a/core/tracking/__tests__/TrackingInitializer.test.tsx +++ b/core/tracking/__tests__/TrackingInitializer.test.tsx @@ -640,12 +640,7 @@ describe('TrackingInitializer', () => { expect(posthogCrossPlatformMock.identify).toHaveBeenCalledTimes(1) const [consentStateAfter] = trackingInitializerTesting.consentStateVar() expect(consentStateAfter).toEqual(ConsentState.unknown) - expect(mockTrack).not.toHaveBeenCalledWith({ - name: 'loginCompleted', - event_version__major: 1, - event_version__minor: 0, - event_version__patch: 0, - }) + expect(mockTrack).not.toHaveBeenCalled() }) }) }) diff --git a/core/tracking/__tests__/useTracking.test.ts b/core/tracking/__tests__/useTracking.test.ts index 188b982bfa..7ddb3221bd 100644 --- a/core/tracking/__tests__/useTracking.test.ts +++ b/core/tracking/__tests__/useTracking.test.ts @@ -1,9 +1,9 @@ import { jest } from '@jest/globals' import { usePosthogCrossPlatform } from '@holi/core/tracking/PosthogCrossPlatform' -import { useConsentState } from '@holi/core/tracking/TrackingInitializer' +import { getConsentState } from '@holi/core/tracking/TrackingInitializer' import useTracking from '@holi/core/tracking/hooks/useTracking' -import { Consent, PosthogCrossPlatform, TrackingPurpose } from '@holi/core/tracking/types' +import { Consent, type PosthogCrossPlatform, TrackingPurpose } from '@holi/core/tracking/types' import { renderHook } from '@testing-library/react-hooks' jest.mock('@holi/core/helpers/config', () => ({ @@ -12,7 +12,7 @@ jest.mock('@holi/core/helpers/config', () => ({ })) jest.mock('@holi/core/tracking/TrackingInitializer') -const useConsentStateMock = useConsentState as jest.Mock +const getConsentStateMock = getConsentState as jest.Mock jest.mock('@holi/core/tracking/PosthogCrossPlatform') const posthogCrossPlatformMock = { @@ -32,7 +32,7 @@ jest.unmock('@holi/core/tracking/hooks/useTracking') beforeEach(() => { usePosthogCrossPlatformMock.mockReset() - useConsentStateMock.mockReset() + getConsentStateMock.mockReset() }) describe('useTracking', () => { @@ -44,13 +44,10 @@ describe('useTracking', () => { it('does add trackingConsentPersonalization=true as an event property', async () => { // GIVEN usePosthogCrossPlatformMock.mockReturnValue(posthogCrossPlatformMock) - useConsentStateMock.mockReturnValue([ - { - [TrackingPurpose.Analytics]: Consent.Given, - [TrackingPurpose.Personalization]: Consent.Given, - }, - { loading: false }, - ]) + getConsentStateMock.mockReturnValue({ + [TrackingPurpose.Analytics]: Consent.Given, + [TrackingPurpose.Personalization]: Consent.Given, + }) // WHEN const { result } = renderHook(() => useTracking()) @@ -76,13 +73,10 @@ describe('useTracking', () => { it('does add trackingConsentPersonalization=false as an event property', async () => { // GIVEN usePosthogCrossPlatformMock.mockReturnValue(posthogCrossPlatformMock) - useConsentStateMock.mockReturnValue([ - { - [TrackingPurpose.Analytics]: Consent.Given, - [TrackingPurpose.Personalization]: Consent.Denied, - }, - { loading: false }, - ]) + getConsentStateMock.mockReturnValue({ + [TrackingPurpose.Analytics]: Consent.Given, + [TrackingPurpose.Personalization]: Consent.Denied, + }) // WHEN const { result } = renderHook(() => useTracking()) diff --git a/core/tracking/hooks/useTracking.ts b/core/tracking/hooks/useTracking.ts index 39207dd6cb..d5e646e311 100644 --- a/core/tracking/hooks/useTracking.ts +++ b/core/tracking/hooks/useTracking.ts @@ -1,6 +1,6 @@ import { getLogger } from '@holi/core/helpers/logging' import { usePosthogCrossPlatform } from '@holi/core/tracking/PosthogCrossPlatform' -import { useConsentState } from '@holi/core/tracking/TrackingInitializer' +import { getConsentState } from '@holi/core/tracking/TrackingInitializer' import type { TrackingEvent } from '@holi/core/tracking/events' import { ConsentState, type TrackingHook } from '@holi/core/tracking/types' import { useCallback } from 'react' @@ -9,14 +9,13 @@ const logger = getLogger('Tracking') const useTracking = (): TrackingHook => { const posthog = usePosthogCrossPlatform() - const [consent] = useConsentState() const track = useCallback( (event: TrackingEvent) => { logger.debug('trackEvent', `tracking "${event.name}" event`, event) try { posthog.capture(event.name, { ...event.properties, - ...ConsentState.toEventProperties(consent), + ...ConsentState.toEventProperties(getConsentState()), event_version__major: event.event_version__major, event_version__minor: event.event_version__minor, event_version__patch: event.event_version__patch, @@ -27,7 +26,7 @@ const useTracking = (): TrackingHook => { console.error('Error during evaluation of onTrack callback', e) } }, - [consent, posthog] + [posthog] ) return { -- GitLab