Skip to content

Commit cb9d5a3

Browse files
authored
Merge pull request #26 from janbaykara/analytics-identification
Identify users to analytics dashboard
2 parents 8cf6221 + b7295c0 commit cb9d5a3

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
REACT_APP_API_ENDPOINT=https://api.futurelabourmps.com/
22
REACT_APP_API_COLS=A3:AZ70
3+
REACT_APP_SUPABASE_URL=
4+
REACT_APP_SUPABASE_ANON_KEY=

src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useDispatch } from "react-redux";
66
import { fetchMPs } from "./data/utils/utils";
77
import { DataStatus, MP } from "./data/types";
88
import { SET_DATA_ACTION } from "./state/actions";
9-
import { useAnalytics } from "./analytics";
9+
import { identifyUser, useAnalytics } from "./analytics";
1010
import { supabase } from "./supabaseClient";
1111
import { Session } from "@supabase/supabase-js";
1212
import { SignUpModal } from "./components/auth/SignUpModal";
@@ -50,6 +50,10 @@ function App() {
5050
}
5151
});
5252

53+
useEffect(() => {
54+
identifyUser(session?.user)
55+
}, [session, session?.user.id, session?.user.email])
56+
5357
const modalDismissed = localStorage.getItem(MODAL_DISMISSED_KEY) !== "true";
5458

5559
return (

src/analytics.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import posthog from 'posthog-js'
22
import "vanilla-cookieconsent/dist/cookieconsent.css";
33
import * as CookieConsent from "vanilla-cookieconsent";
44
import { useEffect } from 'react';
5+
import { Session, User } from "@supabase/supabase-js";
6+
7+
// Anonymous ID that posthog sets by default
8+
let distinct_id: string | null = null;
59

610
export async function enablePosthog() {
711
if (!process.env.REACT_APP_POSTHOG_API_KEY || !process.env.REACT_APP_POSTHOG_API_URL) {
812
return
913
}
1014
posthog.init(process.env.REACT_APP_POSTHOG_API_KEY!, {
11-
api_host: process.env.REACT_APP_POSTHOG_API_URL
15+
api_host: process.env.REACT_APP_POSTHOG_API_URL,
16+
loaded: function(posthog) {
17+
distinct_id = posthog.get_distinct_id();
18+
}
1219
})
1320
posthog.opt_in_capturing()
1421
}
@@ -29,6 +36,21 @@ export function captureAnalyticsPageView (page: string) {
2936
posthog.capture('change tab', { page })
3037
}
3138

39+
export function captureEmail (email: string) {
40+
posthog.capture('newsletter signup', { $set: { email } })
41+
}
42+
43+
export function identifyUser (user?: User | null) {
44+
if (!user?.email) return
45+
if (distinct_id) {
46+
posthog.alias(
47+
distinct_id,
48+
user.id,
49+
)
50+
}
51+
posthog.setPersonProperties({ email: user.email, phone: user.phone })
52+
}
53+
3254
export function useAnalytics () {
3355
useEffect(() => {
3456
CookieConsent.run({

src/components/auth/Auth.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { useState } from "react";
22
import { supabase } from "../../supabaseClient";
33
import { MODAL_DISMISSED_KEY } from "../../state/store";
44
import Link from "../atoms/Link";
5+
import { captureEmail } from '../../analytics'
56

67
export default function Auth() {
78
const [loading, setLoading] = useState(false);
89
const [email, setEmail] = useState("");
910

1011
const handleLogin = async (event: { preventDefault: () => void }) => {
1112
event.preventDefault();
13+
captureEmail(email)
1214

1315
localStorage.setItem(MODAL_DISMISSED_KEY, "false");
1416

@@ -44,7 +46,9 @@ export default function Auth() {
4446
id="sign_up_input"
4547
value={email}
4648
required={true}
47-
onChange={(e) => setEmail(e.target.value)}
49+
onChange={(e) => {
50+
setEmail(e.target.value)
51+
}}
4852
/>
4953
<button
5054
className={"btn btn-accent btn-sm block w-24"}

0 commit comments

Comments
 (0)