mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-09 14:11:51 +02:00
🐛 Fix posthog bootstrap code
This commit is contained in:
parent
9a0a3c4a8d
commit
831f85db5f
1 changed files with 20 additions and 24 deletions
|
@ -1,12 +1,24 @@
|
||||||
import { trpc, UserSession } from "@rallly/backend";
|
import { trpc, UserSession } from "@rallly/backend";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { useTranslation } from "next-i18next";
|
import { useTranslation } from "next-i18next";
|
||||||
import posthog, { PostHog } from "posthog-js";
|
import posthog from "posthog-js";
|
||||||
import { PostHogProvider } from "posthog-js/react";
|
import { PostHogProvider } from "posthog-js/react";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { useRequiredContext } from "./use-required-context";
|
import { useRequiredContext } from "./use-required-context";
|
||||||
|
|
||||||
|
if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
|
||||||
|
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
|
||||||
|
api_host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
|
||||||
|
opt_out_capturing_by_default: false,
|
||||||
|
capture_pageview: false,
|
||||||
|
persistence: "memory",
|
||||||
|
capture_pageleave: false,
|
||||||
|
autocapture: false,
|
||||||
|
opt_in_site_apps: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export const UserContext = React.createContext<{
|
export const UserContext = React.createContext<{
|
||||||
user: UserSession & { shortName: string };
|
user: UserSession & { shortName: string };
|
||||||
refresh: () => void;
|
refresh: () => void;
|
||||||
|
@ -61,33 +73,17 @@ export const UserProvider = (props: {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const [posthogClient, setPostHogClient] = React.useState<PostHog>();
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY || !user) {
|
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY || !user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
|
|
||||||
api_host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
|
|
||||||
opt_out_capturing_by_default: false,
|
|
||||||
capture_pageview: false,
|
|
||||||
capture_pageleave: false,
|
|
||||||
autocapture: false,
|
|
||||||
persistence: "memory",
|
|
||||||
bootstrap: {
|
|
||||||
distinctID: user.id,
|
|
||||||
},
|
|
||||||
loaded: (posthog) => {
|
|
||||||
posthog.identify(
|
posthog.identify(
|
||||||
user.id,
|
user.id,
|
||||||
!user.isGuest
|
!user.isGuest
|
||||||
? { email: user.email, name: user.name }
|
? { email: user.email, name: user.name }
|
||||||
: { name: user.id },
|
: { name: user.id },
|
||||||
);
|
);
|
||||||
setPostHogClient(posthog);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}, [user]);
|
}, [user]);
|
||||||
|
|
||||||
const shortName = user
|
const shortName = user
|
||||||
|
@ -101,7 +97,7 @@ export const UserProvider = (props: {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PostHogProvider client={posthogClient}>
|
<PostHogProvider client={posthog}>
|
||||||
<UserContext.Provider
|
<UserContext.Provider
|
||||||
value={{
|
value={{
|
||||||
user: { ...user, shortName },
|
user: { ...user, shortName },
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue