-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add plausible custom event tracking * refactor: better comments for analytics tracking
- Loading branch information
Showing
8 changed files
with
145 additions
and
86 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
42 changes: 42 additions & 0 deletions
42
frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx
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,42 @@ | ||
import { FC, useState, useEffect } from 'react'; | ||
import Plausible from 'plausible-tracker'; | ||
import { PlausibleContext } from 'contexts/PlausibleContext'; | ||
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; | ||
|
||
const PLAUSIBLE_UNLEASH_API_HOST = 'https://plausible.getunleash.io'; | ||
const PLAUSIBLE_UNLEASH_DOMAIN = 'app.unleash-hosted.com'; | ||
const LOCAL_TESTING = false; | ||
|
||
export const PlausibleProvider: FC = ({ children }) => { | ||
const [context, setContext] = useState<ReturnType<typeof Plausible> | null>( | ||
null | ||
); | ||
const { uiConfig } = useUiConfig(); | ||
const isEnabled = Boolean(uiConfig?.flags?.T || LOCAL_TESTING); | ||
|
||
useEffect(() => { | ||
if (isEnabled) { | ||
try { | ||
const plausible = Plausible({ | ||
domain: LOCAL_TESTING | ||
? undefined | ||
: PLAUSIBLE_UNLEASH_DOMAIN, | ||
apiHost: LOCAL_TESTING | ||
? 'http://localhost:8000' | ||
: PLAUSIBLE_UNLEASH_API_HOST, | ||
trackLocalhost: true, | ||
}); | ||
setContext(() => plausible); | ||
return plausible.enableAutoPageviews(); | ||
} catch (error) { | ||
console.warn(error); | ||
} | ||
} | ||
}, [isEnabled]); | ||
|
||
return ( | ||
<PlausibleContext.Provider value={context}> | ||
{children} | ||
</PlausibleContext.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createContext } from 'react'; | ||
import Plausible from 'plausible-tracker'; | ||
|
||
export const PlausibleContext = createContext<ReturnType< | ||
typeof Plausible | ||
> | null>(null); |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,34 @@ | ||
import Plausible from 'plausible-tracker'; | ||
import { useEffect } from 'react'; | ||
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; | ||
import { IFlags } from 'interfaces/uiConfig'; | ||
import { useCallback, useContext, useEffect } from 'react'; | ||
import { PlausibleContext } from 'contexts/PlausibleContext'; | ||
import { EventOptions, PlausibleOptions } from 'plausible-tracker'; | ||
|
||
const PLAUSIBLE_UNLEASH_API_HOST = 'https://plausible.getunleash.io'; | ||
const PLAUSIBLE_UNLEASH_DOMAIN = 'app.unleash-hosted.com'; | ||
/** | ||
* Allowed event names. Makes it easy to remove, since TS will complain. | ||
* Add those to Plausible as Custom event goals. | ||
* @see https://plausible.io/docs/custom-event-goals#2-create-a-custom-event-goal-in-your-plausible-analytics-account | ||
* @example `'download | 'invite' | 'signup'` | ||
**/ | ||
type CustomEvents = 'invite'; | ||
|
||
export const usePlausibleTracker = () => { | ||
const { uiConfig } = useUiConfig(); | ||
const enabled = enablePlausibleTracker(uiConfig.flags); | ||
const plausible = useContext(PlausibleContext); | ||
|
||
useEffect(() => { | ||
if (enabled) { | ||
try { | ||
return initPlausibleTracker(); | ||
} catch (error) { | ||
console.warn(error); | ||
const trackEvent = useCallback( | ||
( | ||
eventName: CustomEvents, | ||
options?: EventOptions | undefined, | ||
eventData?: PlausibleOptions | undefined | ||
) => { | ||
if (plausible?.trackEvent) { | ||
plausible.trackEvent(eventName, options, eventData); | ||
} else { | ||
if (options?.callback) { | ||
options.callback(); | ||
} | ||
} | ||
} | ||
}, [enabled]); | ||
}; | ||
|
||
const initPlausibleTracker = (): (() => void) => { | ||
const plausible = Plausible({ | ||
domain: PLAUSIBLE_UNLEASH_DOMAIN, | ||
apiHost: PLAUSIBLE_UNLEASH_API_HOST, | ||
trackLocalhost: true, | ||
}); | ||
|
||
return plausible.enableAutoPageviews(); | ||
}; | ||
}, | ||
[plausible] | ||
); | ||
|
||
// Enable Plausible if we're on the Unleash SaaS domain. | ||
export const enablePlausibleTracker = (flags: Partial<IFlags>): boolean => { | ||
return Boolean(flags.T); | ||
return { trackEvent }; | ||
}; |