-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
119 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 @@ | ||
import {PostHogProvider} from 'posthog-js/react'; | ||
import Env from 'utils/Env'; | ||
import Content from './Content'; | ||
|
||
const posthogKey = Env.get('posthogKey'); | ||
|
||
const options = { | ||
api_host: 'https://app.posthog.com', | ||
opt_out_capturing_by_default: true, | ||
}; | ||
|
||
interface IProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const CaptureWrapper = ({children}: IProps) => ( | ||
<PostHogProvider apiKey={posthogKey} options={options}> | ||
<Content>{children}</Content> | ||
</PostHogProvider> | ||
); | ||
|
||
export default CaptureWrapper; |
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,43 @@ | ||
import {usePostHog} from 'posthog-js/react'; | ||
import CaptureProvider from 'providers/Capture'; | ||
import {useCallback, useMemo} from 'react'; | ||
import Env from 'utils/Env'; | ||
|
||
const appVersion = Env.get('appVersion'); | ||
const env = Env.get('env'); | ||
const serverID = Env.get('serverID'); | ||
const isAnalyticsEnabled = () => Env.get('analyticsEnabled') && !Env.get('isTracetestDev'); | ||
|
||
interface IProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const Content = ({children}: IProps) => { | ||
const posthog = usePostHog(); | ||
|
||
const load = useCallback(() => { | ||
if (!isAnalyticsEnabled()) { | ||
if (posthog?.has_opted_in_capturing()) { | ||
posthog?.opt_out_capturing(); | ||
} | ||
return; | ||
} | ||
|
||
posthog?.opt_in_capturing(); | ||
posthog?._start_queue_if_opted_in(); | ||
posthog?.identify(serverID, { | ||
appVersion, | ||
env, | ||
}); | ||
}, [posthog]); | ||
|
||
const pageView = useCallback(() => { | ||
posthog?.capture('$pageview'); | ||
}, [posthog]); | ||
|
||
const captureProviderValue = useMemo(() => ({load, pageView}), [load, pageView]); | ||
|
||
return <CaptureProvider value={captureProviderValue}>{children}</CaptureProvider>; | ||
}; | ||
|
||
export default Content; |
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,2 @@ | ||
// eslint-disable-next-line no-restricted-exports | ||
export {default} from './CaptureWrapper'; |
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,25 @@ | ||
import noop from 'lodash/noop'; | ||
import {createContext, useContext} from 'react'; | ||
|
||
interface IContext { | ||
load(): void; | ||
pageView(): void; | ||
} | ||
|
||
export const Context = createContext<IContext>({ | ||
load: noop, | ||
pageView: noop, | ||
}); | ||
|
||
export const useCapture = () => useContext(Context); | ||
|
||
interface IProps { | ||
children: React.ReactNode; | ||
value: IContext; | ||
} | ||
|
||
const CaptureProvider = ({children, value}: IProps) => { | ||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
}; | ||
|
||
export default CaptureProvider; |
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,2 @@ | ||
// eslint-disable-next-line no-restricted-exports | ||
export {default, useCapture} from './Capture.provider'; |
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