-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filip(feat): replace matomo with posthog (#186)
* filip(feat): replace matomo with posthog * filip(chore): abstract posthog variables with env vars
- Loading branch information
1 parent
fff44ff
commit fda93a4
Showing
7 changed files
with
189 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { | ||
PropsWithChildren, | ||
createContext, | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import { PostHog, PostHogProvider } from "posthog-js/react"; | ||
import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; | ||
import { posthog } from "posthog-js"; | ||
import dayjs from "dayjs"; | ||
import TrackRoute from "./TrackRoute"; | ||
|
||
export const AnalyticsContext = createContext< | ||
(eventName: string, eventProps?: Record<string, unknown>) => void | ||
>(() => {}); | ||
|
||
export function AnalyticsProvider({ children }: PropsWithChildren) { | ||
const { | ||
siteConfig: { customFields }, | ||
} = useDocusaurusContext(); | ||
const [client, setClient] = useState<PostHog | undefined>(); | ||
|
||
const baseEventProps = useCallback( | ||
() => ({ | ||
sent_at_local: dayjs().format(), | ||
posthog_project_id: customFields.posthogProjectId, | ||
}), | ||
[customFields.posthogProjectId] | ||
); | ||
|
||
const capture = useCallback( | ||
( | ||
eventName: string, | ||
eventProperties: Record<string | number, unknown> = {} | ||
) => { | ||
client?.capture(eventName, { | ||
...baseEventProps(), | ||
...eventProperties, | ||
}); | ||
}, | ||
[client, baseEventProps] | ||
); | ||
|
||
useEffect(() => { | ||
const { posthogApiKey, posthogApiHost } = customFields; | ||
const turnOn = | ||
typeof posthogApiKey === "string" && | ||
posthogApiKey && | ||
typeof posthogApiHost === "string" && | ||
posthogApiHost; | ||
|
||
setClient((oldClient) => { | ||
if (turnOn) { | ||
const client = | ||
oldClient ?? | ||
(posthog.init(posthogApiKey ?? "", { | ||
api_host: posthogApiHost, | ||
capture_pageleave: false, | ||
capture_pageview: false, | ||
}) as PostHog); | ||
|
||
// clear localStorage state that might have been set by previous clients | ||
client.clear_opt_in_out_capturing(); | ||
|
||
// we got consent, start capturing | ||
client.opt_in_capturing({ | ||
capture_properties: baseEventProps(), | ||
}); | ||
// calling a private function as a fix for bug https://github.com/PostHog/posthog-js/issues/336 | ||
client._start_queue_if_opted_in(); | ||
|
||
return client; | ||
} else { | ||
oldClient?.opt_out_capturing(); | ||
return undefined; | ||
} | ||
}); | ||
}, [customFields.posthogApiKey, customFields.posthogApiHost, baseEventProps]); | ||
|
||
return ( | ||
<PostHogProvider client={client}> | ||
<AnalyticsContext.Provider value={capture}> | ||
<TrackRoute /> | ||
{children} | ||
</AnalyticsContext.Provider> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useLocation } from "@docusaurus/router"; | ||
import { useContext, useEffect } from "react"; | ||
import { AnalyticsContext } from "./AnalyticsContext"; | ||
|
||
export default function TrackRoute() { | ||
const location = useLocation(); | ||
const capture = useContext(AnalyticsContext); | ||
|
||
useEffect(() => { | ||
capture("$pageview"); | ||
}, [capture, location.pathname, location.search]); | ||
|
||
return 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,10 +1,6 @@ | ||
import React from "react"; | ||
import { MatomoContext } from "@site/src/context/MatomoContext"; | ||
import React, { PropsWithChildren } from "react"; | ||
import { AnalyticsProvider } from "../analytics/AnalyticsContext"; | ||
|
||
export default function Root({ children }) { | ||
return ( | ||
<MatomoContext> | ||
{children} | ||
</MatomoContext> | ||
); | ||
export default function Root({ children }: PropsWithChildren) { | ||
return <AnalyticsProvider>{children}</AnalyticsProvider>; | ||
} |
Oops, something went wrong.
fda93a4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for marlowe-doc ready!
✅ Preview
https://marlowe-45n02zcbz-iog.vercel.app
https://docs.marlowe.iohk.io
https://marlowe-doc.vercel.app
Built with commit fda93a4.
This pull request is being automatically deployed with vercel-action