📈 Update posthog user profile

This commit is contained in:
Luke Vella 2024-09-18 09:00:18 +01:00
parent c4b9edf297
commit edf8e93808
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
2 changed files with 22 additions and 27 deletions

View file

@ -2,7 +2,6 @@
import { Session } from "next-auth";
import { useSession } from "next-auth/react";
import React from "react";
import { z } from "zod";
import { useTranslation } from "@/app/i18n/client";
import { Spinner } from "@/components/spinner";
@ -13,20 +12,21 @@ import { trpc } from "@/utils/trpc/client";
import { useRequiredContext } from "./use-required-context";
const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email().nullable(),
isGuest: z.boolean(),
tier: z.enum(["guest", "hobby", "pro"]),
timeZone: z.string().nullish(),
timeFormat: z.enum(["hours12", "hours24"]).nullish(),
weekStart: z.number().min(0).max(6).nullish(),
image: z.string().nullish(),
});
type UserData = {
id: string;
name: string;
email?: string | null;
isGuest: boolean;
tier: "guest" | "hobby" | "pro";
timeZone?: string | null;
timeFormat?: "hours12" | "hours24" | null;
weekStart?: number | null;
image?: string | null;
locale?: string | null;
};
export const UserContext = React.createContext<{
user: z.infer<typeof userSchema>;
user: UserData;
refresh: (data?: Record<string, unknown>) => Promise<Session | null>;
ownsObject: (obj: { userId?: string | null }) => boolean;
} | null>(null);
@ -58,7 +58,7 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
const user = session.data?.user;
const subscription = useSubscription();
const updatePreferences = trpc.user.updatePreferences.useMutation();
const { t } = useTranslation();
const { t, i18n } = useTranslation();
if (!user) {
return (
@ -82,6 +82,7 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
tier,
timeZone: user.timeZone ?? null,
image: user.image ?? null,
locale: user.locale ?? i18n.language,
},
refresh: session.update,
ownsObject: ({ userId }) => {

View file

@ -5,7 +5,6 @@ import { PostHogProvider as Provider, usePostHog } from "posthog-js/react";
import React from "react";
import { useMount } from "react-use";
import { useTranslation } from "@/app/i18n/client";
import { useUser } from "@/components/user-provider";
import { env } from "@/env";
@ -49,22 +48,17 @@ function usePostHogPageView() {
export function PostHogProvider(props: PostHogProviderProps) {
const { user } = useUser();
const { i18n } = useTranslation();
usePostHogPageView();
useMount(() => {
if (user.email) {
posthog.identify(user.id, {
email: user.email,
name: user.name,
tier: user.tier,
timeZone: user.timeZone,
locale: i18n.language,
});
} else {
posthog.alias(user.id);
}
posthog.identify(user.id, {
email: user.email,
name: user.name,
tier: user.tier,
timeZone: user.timeZone,
locale: user.locale,
});
});
return <Provider client={posthog}>{props.children}</Provider>;