-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(posthog): disable autocapture and track pageview on location change
- Disable autocapture to not send any event anymore. - Disable autocapture of pageview in favor of TrackerPageView, which sends a $pageview event each time the location change. - Move the TrackerProvider under the Router in order to receive updates on the location. - Keep autocapture of pageleave enable. - Configure PostHog client to use POSTHOG_API_HOST env variable if set, and fallback to stat.zextrsa.tools. This configuration allows using a different PostHog instance in development. Be aware that the host env is not set in Jenkins, meaning that for the packages build through it only the key will be set, while the host will always use the fallback. - Move each component/hook in a specific file, under the tracker folder. Refs: SHELL-251 (#529)
- Loading branch information
Showing
16 changed files
with
179 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import React from 'react'; | ||
|
||
import { Link } from 'react-router-dom'; | ||
|
||
import { TrackerPageView } from './page-view'; | ||
import * as useTracker from './tracker'; | ||
import type { Tracker } from './tracker'; | ||
import { screen, setup } from '../tests/utils'; | ||
|
||
describe('TrackerPageView', () => { | ||
it('should capture pageview event when pathname change', async () => { | ||
const tracker: Tracker = { | ||
capture: jest.fn(), | ||
enableTracker: jest.fn(), | ||
reset: jest.fn() | ||
}; | ||
jest.spyOn(useTracker, 'useTracker').mockReturnValue(tracker); | ||
const { user } = setup( | ||
<> | ||
<TrackerPageView /> | ||
<Link to={'/different-path'}>Go to different path</Link> | ||
</>, | ||
{ initialRouterEntries: ['/initial-path'] } | ||
); | ||
await user.click(screen.getByRole('link')); | ||
expect(tracker.capture).toHaveBeenLastCalledWith('$pageview', { | ||
$current_url: `${window.origin}/different-path` | ||
}); | ||
}); | ||
|
||
it('should capture pageview event when search params change', async () => { | ||
const tracker: Tracker = { | ||
capture: jest.fn(), | ||
enableTracker: jest.fn(), | ||
reset: jest.fn() | ||
}; | ||
jest.spyOn(useTracker, 'useTracker').mockReturnValue(tracker); | ||
const { user } = setup( | ||
<> | ||
<TrackerPageView /> | ||
<Link to={'/initial-path?param=2'}>Go to different path</Link> | ||
</>, | ||
{ initialRouterEntries: ['/initial-path?param=1'] } | ||
); | ||
await user.click(screen.getByRole('link')); | ||
expect(tracker.capture).toHaveBeenLastCalledWith('$pageview', { | ||
$current_url: `${window.origin}/initial-path?param=2` | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import { useEffect } from 'react'; | ||
|
||
import { useLocation } from 'react-router-dom'; | ||
|
||
import { useTracker } from './tracker'; | ||
|
||
export const TrackerPageView = (): null => { | ||
const tracker = useTracker(); | ||
const { pathname, search } = useLocation(); | ||
useEffect(() => { | ||
tracker.capture('$pageview', { | ||
$current_url: window.origin + pathname + search | ||
}); | ||
}, [pathname, search, tracker]); | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import * as posthogJsReact from 'posthog-js/react'; | ||
import type * as PostHogReact from 'posthog-js/react'; | ||
|
||
import { TrackerProvider } from './provider'; | ||
import { setup } from '../tests/utils'; | ||
import * as utils from '../utils/utils'; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(utils, 'getCurrentLocationHost').mockReturnValue('differentHost'); | ||
}); | ||
|
||
describe('TrackerProvider', () => { | ||
it('should invoke tracker provider with trackers disabled by default', () => { | ||
const mockProvider = jest.spyOn(posthogJsReact, 'PostHogProvider'); | ||
setup(<TrackerProvider />); | ||
type PostHogProviderProps = React.ComponentPropsWithoutRef< | ||
(typeof PostHogReact)['PostHogProvider'] | ||
>; | ||
expect(mockProvider).toHaveBeenLastCalledWith( | ||
expect.objectContaining<PostHogProviderProps>({ | ||
options: expect.objectContaining<NonNullable<PostHogProviderProps['options']>>({ | ||
opt_out_capturing_by_default: true, | ||
disable_session_recording: true, | ||
disable_surveys: true | ||
}) | ||
}), | ||
expect.anything() | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import React, { useMemo } from 'react'; | ||
|
||
import type { PostHogConfig } from 'posthog-js'; | ||
import { PostHogProvider } from 'posthog-js/react'; | ||
|
||
import { TrackerPageView } from './page-view'; | ||
|
||
export const TrackerProvider = ({ | ||
children | ||
}: React.PropsWithChildren<Record<never, never>>): React.JSX.Element => { | ||
const options = useMemo( | ||
(): Partial<PostHogConfig> => ({ | ||
api_host: POSTHOG_API_HOST || 'https://stats.zextras.tools', | ||
person_profiles: 'identified_only', | ||
opt_out_capturing_by_default: true, | ||
disable_session_recording: true, | ||
mask_all_text: true, | ||
disable_surveys: true, | ||
capture_pageview: false, | ||
capture_pageleave: true, | ||
autocapture: false | ||
}), | ||
[] | ||
); | ||
return ( | ||
<PostHogProvider apiKey={POSTHOG_API_KEY} options={options}> | ||
{children} | ||
<TrackerPageView /> | ||
</PostHogProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.