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

View file

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