Skip to content

Commit

Permalink
add posthog user identification
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ivashchuk committed Mar 27, 2024
1 parent f6c6d42 commit 7b9f4a8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"lucide-react": "^0.363.0",
"next": "^14.1.3",
"next-auth": "^4.24.6",
"posthog-js": "^1.116.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"server-only": "^0.0.1",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use client'
"use client";

import "~/styles/globals.css";

import { Inter } from "next/font/google";

import { TRPCReactProvider } from "~/trpc/react";
import { SessionProvider } from 'next-auth/react';
import { Layout } from "~/components/patterns/layout";
import Providers from "~/components/providers";

const inter = Inter({
subsets: ["latin"],
Expand All @@ -27,11 +26,9 @@ export default function RootLayout({
return (
<html lang="en">
<body className={`font-sans ${inter.variable}`}>
<TRPCReactProvider>
<SessionProvider>
<Providers>
<Layout>{children}</Layout>
</SessionProvider>
</TRPCReactProvider>
</Providers>
</body>
</html>
);
Expand Down
4 changes: 3 additions & 1 deletion src/components/patterns/login-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function LoginScreen() {
</div>
<div className="grid gap-4">
<Button
onClick={() => signIn("discord", { callbackUrl: "/" })}
onClick={() =>
signIn("discord", { callbackUrl: "/?loginState=signedIn" })
}
variant="outline"
className="w-full"
>
Expand Down
47 changes: 47 additions & 0 deletions src/components/providers/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use client";

import React from "react";
import posthog from "posthog-js";
import { PostHogProvider, usePostHog } from "posthog-js/react";
import { SessionProvider, useSession } from "next-auth/react";

import { Layout } from "~/components/patterns/layout";
import { TRPCReactProvider } from "~/trpc/react";
import { env } from "~/env.mjs";
import { useSearchParams } from "next/navigation";

if (typeof window !== "undefined" && process.env.NODE_ENV === "production") {
posthog.init(env.NEXT_PUBLIC_POSTHOG_API_KEY!, {
api_host: env.NEXT_PUBLIC_POSTHOG_HOST!,
});
}

const PostHogIdentification = ({ children }: { children: React.ReactNode }) => {
const { data: session } = useSession();
const posthog = usePostHog();

const params = useSearchParams();
const newLoginState = params.get("loginState");

if (newLoginState == "signedIn" && session) {
posthog.identify(session.user.id);
}

return <>{children}</>;
};

const Providers = ({ children }: { children: React.ReactNode }) => {
return (
<TRPCReactProvider>
<SessionProvider>
<PostHogProvider client={posthog}>
<PostHogIdentification>
<Layout>{children}</Layout>
</PostHogIdentification>
</PostHogProvider>
</SessionProvider>
</TRPCReactProvider>
);
};

export default Providers;
11 changes: 10 additions & 1 deletion src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ export const env = createEnv({
* `NEXT_PUBLIC_`.
*/
client: {
// NEXT_PUBLIC_CLIENTVAR: z.string(),
NEXT_PUBLIC_POSTHOG_API_KEY:
process.env.NODE_ENV === "production"
? z.string()
: z.string().optional(),
NEXT_PUBLIC_POSTHOG_HOST:
process.env.NODE_ENV === "production"
? z.string()
: z.string().optional(),
},

/**
Expand All @@ -64,6 +71,8 @@ export const env = createEnv({
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
NEXT_PUBLIC_POSTHOG_API_KEY: process.env.POSTHOG_API_KEY,
NEXT_PUBLIC_POSTHOG_HOST: process.env.POSTHOG_HOST,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down

0 comments on commit 7b9f4a8

Please sign in to comment.