@@ -2,13 +2,20 @@ import posthog from 'posthog-js'
2
2
import "vanilla-cookieconsent/dist/cookieconsent.css" ;
3
3
import * as CookieConsent from "vanilla-cookieconsent" ;
4
4
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 ;
5
9
6
10
export async function enablePosthog ( ) {
7
11
if ( ! process . env . REACT_APP_POSTHOG_API_KEY || ! process . env . REACT_APP_POSTHOG_API_URL ) {
8
12
return
9
13
}
10
14
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
+ }
12
19
} )
13
20
posthog . opt_in_capturing ( )
14
21
}
@@ -29,6 +36,21 @@ export function captureAnalyticsPageView (page: string) {
29
36
posthog . capture ( 'change tab' , { page } )
30
37
}
31
38
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
+
32
54
export function useAnalytics ( ) {
33
55
useEffect ( ( ) => {
34
56
CookieConsent . run ( {
0 commit comments