1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-15 01:16:22 +02:00
unleash.unleash/frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx
Tymoteusz Czech 10eb500360
Custom event tracking (#2151)
* add plausible custom event tracking

* refactor: better comments for analytics tracking
2022-10-10 14:06:44 +02:00

43 lines
1.4 KiB
TypeScript

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>
);
};