From 4c1e8e8c8e255b7bafd0bbfc146214287f7ef0e2 Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Wed, 19 Apr 2023 09:33:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=AA=20Stop=20using=20posthog=20cookie?= =?UTF-8?q?=20(#665)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/components/user-provider.tsx | 84 ++++++++++++++--------- apps/web/src/pages/_app.tsx | 24 +------ 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/apps/web/src/components/user-provider.tsx b/apps/web/src/components/user-provider.tsx index f296ffb71..6ff4054b0 100644 --- a/apps/web/src/components/user-provider.tsx +++ b/apps/web/src/components/user-provider.tsx @@ -1,10 +1,10 @@ import { trpc, UserSession } from "@rallly/backend"; import { useRouter } from "next/router"; import { useTranslation } from "next-i18next"; +import posthog, { PostHog } from "posthog-js"; +import { PostHogProvider } from "posthog-js/react"; import React from "react"; -import { usePostHog } from "@/utils/posthog"; - import { useRequiredContext } from "./use-required-context"; export const UserContext = React.createContext<{ @@ -61,18 +61,34 @@ export const UserProvider = (props: { }, }); - const posthog = usePostHog(); + const [posthogClient, setPostHogClient] = React.useState(); React.useEffect(() => { - if (user && posthog?.__loaded && posthog?.get_distinct_id() !== user.id) { - posthog?.identify( - user.id, - !user.isGuest - ? { email: user.email, name: user.name } - : { name: user.id }, - ); + if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY || !user) { + return; } - }, [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 ? user.isGuest === false @@ -85,28 +101,30 @@ export const UserProvider = (props: { } return ( - { - return queryClient.whoami.invalidate(); - }, - ownsObject: ({ userId }) => { - if ( - (userId && user.id === userId) || - (props.forceUserId && props.forceUserId === userId) - ) { - return true; - } - return false; - }, - logout: () => { - logout.mutate(); - }, - }} - > - {props.children} - + + { + return queryClient.whoami.invalidate(); + }, + ownsObject: ({ userId }) => { + if ( + (userId && user.id === userId) || + (props.forceUserId && props.forceUserId === userId) + ) { + return true; + } + return false; + }, + logout: () => { + logout.mutate(); + }, + }} + > + {props.children} + + ); }; diff --git a/apps/web/src/pages/_app.tsx b/apps/web/src/pages/_app.tsx index f3133cced..5c9e49cf6 100644 --- a/apps/web/src/pages/_app.tsx +++ b/apps/web/src/pages/_app.tsx @@ -10,8 +10,6 @@ import { Inter } from "next/font/google"; import Head from "next/head"; import { appWithTranslation } from "next-i18next"; import { DefaultSeo } from "next-seo"; -import posthog from "posthog-js"; -import { PostHogProvider } from "posthog-js/react"; import React from "react"; import Maintenance from "@/components/maintenance"; @@ -25,29 +23,13 @@ const inter = Inter({ }); type PageProps = { - user: UserSession; + user?: UserSession; }; type AppPropsWithLayout = AppProps & { Component: NextPageWithLayout; }; -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 = ({ Component, pageProps }) => { React.useEffect(() => { if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) { @@ -63,7 +45,7 @@ const MyApp: NextPage = ({ Component, pageProps }) => { const getLayout = Component.getLayout ?? ((page) => page); return ( - + <> = ({ Component, pageProps }) => { } `} {getLayout()} - + ); };