mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-06 20:51:48 +02:00
🍪 Stop using posthog cookie (#665)
This commit is contained in:
parent
7e69dc5c44
commit
4c1e8e8c8e
2 changed files with 54 additions and 54 deletions
|
@ -1,10 +1,10 @@
|
||||||
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 { PostHogProvider } from "posthog-js/react";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { usePostHog } from "@/utils/posthog";
|
|
||||||
|
|
||||||
import { useRequiredContext } from "./use-required-context";
|
import { useRequiredContext } from "./use-required-context";
|
||||||
|
|
||||||
export const UserContext = React.createContext<{
|
export const UserContext = React.createContext<{
|
||||||
|
@ -61,18 +61,34 @@ export const UserProvider = (props: {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const posthog = usePostHog();
|
const [posthogClient, setPostHogClient] = React.useState<PostHog>();
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (user && posthog?.__loaded && posthog?.get_distinct_id() !== user.id) {
|
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY || !user) {
|
||||||
posthog?.identify(
|
return;
|
||||||
user.id,
|
|
||||||
!user.isGuest
|
|
||||||
? { email: user.email, name: user.name }
|
|
||||||
: { name: user.id },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}, [posthog, user]);
|
|
||||||
|
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(
|
||||||
|
user.id,
|
||||||
|
!user.isGuest
|
||||||
|
? { email: user.email, name: user.name }
|
||||||
|
: { name: user.id },
|
||||||
|
);
|
||||||
|
setPostHogClient(posthog);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}, [user]);
|
||||||
|
|
||||||
const shortName = user
|
const shortName = user
|
||||||
? user.isGuest === false
|
? user.isGuest === false
|
||||||
|
@ -85,28 +101,30 @@ export const UserProvider = (props: {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider
|
<PostHogProvider client={posthogClient}>
|
||||||
value={{
|
<UserContext.Provider
|
||||||
user: { ...user, shortName },
|
value={{
|
||||||
refresh: () => {
|
user: { ...user, shortName },
|
||||||
return queryClient.whoami.invalidate();
|
refresh: () => {
|
||||||
},
|
return queryClient.whoami.invalidate();
|
||||||
ownsObject: ({ userId }) => {
|
},
|
||||||
if (
|
ownsObject: ({ userId }) => {
|
||||||
(userId && user.id === userId) ||
|
if (
|
||||||
(props.forceUserId && props.forceUserId === userId)
|
(userId && user.id === userId) ||
|
||||||
) {
|
(props.forceUserId && props.forceUserId === userId)
|
||||||
return true;
|
) {
|
||||||
}
|
return true;
|
||||||
return false;
|
}
|
||||||
},
|
return false;
|
||||||
logout: () => {
|
},
|
||||||
logout.mutate();
|
logout: () => {
|
||||||
},
|
logout.mutate();
|
||||||
}}
|
},
|
||||||
>
|
}}
|
||||||
{props.children}
|
>
|
||||||
</UserContext.Provider>
|
{props.children}
|
||||||
|
</UserContext.Provider>
|
||||||
|
</PostHogProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,6 @@ import { Inter } from "next/font/google";
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { appWithTranslation } from "next-i18next";
|
import { appWithTranslation } from "next-i18next";
|
||||||
import { DefaultSeo } from "next-seo";
|
import { DefaultSeo } from "next-seo";
|
||||||
import posthog from "posthog-js";
|
|
||||||
import { PostHogProvider } from "posthog-js/react";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import Maintenance from "@/components/maintenance";
|
import Maintenance from "@/components/maintenance";
|
||||||
|
@ -25,29 +23,13 @@ const inter = Inter({
|
||||||
});
|
});
|
||||||
|
|
||||||
type PageProps = {
|
type PageProps = {
|
||||||
user: UserSession;
|
user?: UserSession;
|
||||||
};
|
};
|
||||||
|
|
||||||
type AppPropsWithLayout = AppProps<PageProps> & {
|
type AppPropsWithLayout = AppProps<PageProps> & {
|
||||||
Component: NextPageWithLayout<PageProps>;
|
Component: NextPageWithLayout<PageProps>;
|
||||||
};
|
};
|
||||||
|
|
||||||
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,
|
|
||||||
capture_pageleave: false,
|
|
||||||
autocapture: false,
|
|
||||||
opt_in_site_apps: true,
|
|
||||||
loaded: (posthog) => {
|
|
||||||
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
|
|
||||||
posthog.opt_out_capturing();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
|
const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) {
|
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) {
|
||||||
|
@ -63,7 +45,7 @@ const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
|
||||||
const getLayout = Component.getLayout ?? ((page) => page);
|
const getLayout = Component.getLayout ?? ((page) => page);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PostHogProvider client={posthog}>
|
<>
|
||||||
<DefaultSeo
|
<DefaultSeo
|
||||||
openGraph={{
|
openGraph={{
|
||||||
siteName: "Rallly",
|
siteName: "Rallly",
|
||||||
|
@ -95,7 +77,7 @@ const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
|
||||||
}
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
{getLayout(<Component {...pageProps} />)}
|
{getLayout(<Component {...pageProps} />)}
|
||||||
</PostHogProvider>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue