1
- import posthog from ' posthog-js'
1
+ import posthog from " posthog-js" ;
2
2
import "vanilla-cookieconsent/dist/cookieconsent.css" ;
3
3
import * as CookieConsent from "vanilla-cookieconsent" ;
4
- import { useEffect } from ' react' ;
5
- import { Session , User } from "@supabase/supabase-js" ;
4
+ import { useEffect } from " react" ;
5
+ import { User } from "@supabase/supabase-js" ;
6
6
7
7
// Anonymous ID that posthog sets by default
8
8
let distinct_id : string | null = null ;
9
9
10
10
export async function enablePosthog ( ) {
11
- if ( ! process . env . REACT_APP_POSTHOG_API_KEY || ! process . env . REACT_APP_POSTHOG_API_URL ) {
12
- return
11
+ if (
12
+ ! process . env . REACT_APP_POSTHOG_API_KEY ||
13
+ ! process . env . REACT_APP_POSTHOG_API_URL
14
+ ) {
15
+ return ;
13
16
}
14
17
posthog . init ( process . env . REACT_APP_POSTHOG_API_KEY ! , {
15
18
api_host : process . env . REACT_APP_POSTHOG_API_URL ,
16
- loaded : function ( posthog ) {
19
+ loaded : function ( posthog ) {
17
20
distinct_id = posthog . get_distinct_id ( ) ;
18
- }
19
- } )
20
- posthog . opt_in_capturing ( )
21
+ } ,
22
+ } ) ;
23
+ posthog . opt_in_capturing ( ) ;
21
24
}
22
25
23
26
export async function disablePosthog ( ) {
24
- posthog . opt_out_capturing ( )
27
+ posthog . opt_out_capturing ( ) ;
25
28
}
26
29
27
- export function showPrivacyPreferences ( ) {
30
+ export function showPrivacyPreferences ( ) {
28
31
CookieConsent . show ( ) ;
29
32
}
30
33
31
- export function captureAnalyticsEvent ( event : string , properties : any ) {
32
- posthog . capture ( event , properties )
34
+ export function captureAnalyticsEvent ( event : string , properties : any ) {
35
+ posthog . capture ( event , properties ) ;
33
36
}
34
37
35
- export function captureAnalyticsPageView ( page : string ) {
36
- posthog . capture ( ' change tab' , { page } )
38
+ export function captureAnalyticsPageView ( page : string ) {
39
+ posthog . capture ( " change tab" , { page } ) ;
37
40
}
38
41
39
- export function captureEmail ( email : string ) {
40
- posthog . capture ( ' newsletter signup' , { $set : { email } } )
42
+ export function captureEmail ( email : string ) {
43
+ posthog . capture ( " newsletter signup" , { $set : { email } } ) ;
41
44
}
42
45
43
- export function identifyUser ( user ?: User | null ) {
44
- if ( ! user ?. email ) return
46
+ export function identifyUser ( user ?: User | null ) {
47
+ if ( ! user ?. email ) return ;
45
48
if ( distinct_id ) {
46
- posthog . alias (
47
- distinct_id ,
48
- user . id ,
49
- )
49
+ posthog . alias ( distinct_id , user . id ) ;
50
50
}
51
- posthog . setPersonProperties ( { email : user . email , phone : user . phone } )
51
+ posthog . setPersonProperties ( { email : user . email , phone : user . phone } ) ;
52
52
}
53
53
54
- export function useAnalytics ( ) {
54
+ export function useAnalytics ( ) {
55
55
useEffect ( ( ) => {
56
56
CookieConsent . run ( {
57
- revision : 1 ,
58
- categories : {
59
- analytics : {
60
- enabled : true ,
61
- readOnly : false ,
62
- services : {
63
- posthog : {
64
- label : 'PostHog' ,
65
- onAccept : ( ) => {
66
- enablePosthog ( )
57
+ revision : 1 ,
58
+ categories : {
59
+ analytics : {
60
+ enabled : true ,
61
+ readOnly : false ,
62
+ services : {
63
+ posthog : {
64
+ label : "PostHog" ,
65
+ onAccept : ( ) => {
66
+ enablePosthog ( ) ;
67
+ } ,
68
+ onReject : ( ) => {
69
+ disablePosthog ( ) ;
70
+ } ,
71
+ } ,
72
+ } ,
73
+ } ,
74
+ } ,
75
+ language : {
76
+ default : "en" ,
77
+ translations : {
78
+ en : {
79
+ consentModal : {
80
+ title : "Can we use cookies to track usage?" ,
81
+ description :
82
+ "You'll help us learn how to improve the tool for others" ,
83
+ acceptAllBtn : "Accept" ,
84
+ acceptNecessaryBtn : "Reject" ,
85
+ showPreferencesBtn : "Manage privacy preferences" ,
86
+ } ,
87
+ preferencesModal : {
88
+ title : "Manage privacy preferences" ,
89
+ acceptAllBtn : "Accept" ,
90
+ acceptNecessaryBtn : "Reject" ,
91
+ savePreferencesBtn : "Save preferences" ,
92
+ closeIconLabel : "Close" ,
93
+ sections : [
94
+ {
95
+ title : "Usage analytics" ,
96
+ description :
97
+ "Tracks how the tool is used to help us improve it" ,
98
+ linkedCategory : "analytics" ,
67
99
} ,
68
- onReject : ( ) => {
69
- disablePosthog ( )
70
- }
71
- }
72
- }
73
- }
100
+ ] ,
101
+ } ,
102
+ } ,
74
103
} ,
75
- language : {
76
- default : 'en' ,
77
- translations : {
78
- en : {
79
- consentModal : {
80
- title : 'Can we use cookies to track usage?' ,
81
- description : "You'll help us learn how to improve the tool for others" ,
82
- acceptAllBtn : 'Accept' ,
83
- acceptNecessaryBtn : 'Reject' ,
84
- showPreferencesBtn : 'Manage privacy preferences'
85
- } ,
86
- preferencesModal : {
87
- title : 'Manage privacy preferences' ,
88
- acceptAllBtn : 'Accept' ,
89
- acceptNecessaryBtn : 'Reject' ,
90
- savePreferencesBtn : 'Save preferences' ,
91
- closeIconLabel : 'Close' ,
92
- sections : [
93
- {
94
- title : 'Usage analytics' ,
95
- description : 'Tracks how the tool is used to help us improve it' ,
96
- linkedCategory : 'analytics'
97
- }
98
- ]
99
- }
100
- }
101
- }
102
- }
104
+ } ,
103
105
} ) ;
104
106
} , [ ] ) ;
105
107
}
106
108
107
109
// TODO:
108
110
// - enable PII filter
109
- // - show cookies CookieConsent.show();
111
+ // - show cookies CookieConsent.show();
0 commit comments