diff --git a/core/tracking/__tests__/TrackingInitializer.test.tsx b/core/tracking/__tests__/TrackingInitializer.test.tsx index 8863a318ed1f61e3d79b8b0b77641b68b115b4a3..90495100e38b6800269a95b4b32f2e3a36201dbc 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 188b982bfa78952b73b3822f666a6511477a92dc..7ddb3221bd309c62d61b0eae3490f87448acd84f 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 39207dd6cb1d3e29c6039d99498ea640de34e947..d5e646e311ad2bab9fcf04bee3d6dff544561916 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 {