Skip to content

Commit d82faef

Browse files
committed
Update analytics.ts
1 parent cb9d5a3 commit d82faef

File tree

1 file changed

+72
-70
lines changed

1 file changed

+72
-70
lines changed

src/analytics.ts

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,111 @@
1-
import posthog from 'posthog-js'
1+
import posthog from "posthog-js";
22
import "vanilla-cookieconsent/dist/cookieconsent.css";
33
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";
66

77
// Anonymous ID that posthog sets by default
88
let distinct_id: string | null = null;
99

1010
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;
1316
}
1417
posthog.init(process.env.REACT_APP_POSTHOG_API_KEY!, {
1518
api_host: process.env.REACT_APP_POSTHOG_API_URL,
16-
loaded: function(posthog) {
19+
loaded: function (posthog) {
1720
distinct_id = posthog.get_distinct_id();
18-
}
19-
})
20-
posthog.opt_in_capturing()
21+
},
22+
});
23+
posthog.opt_in_capturing();
2124
}
2225

2326
export async function disablePosthog() {
24-
posthog.opt_out_capturing()
27+
posthog.opt_out_capturing();
2528
}
2629

27-
export function showPrivacyPreferences () {
30+
export function showPrivacyPreferences() {
2831
CookieConsent.show();
2932
}
3033

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);
3336
}
3437

35-
export function captureAnalyticsPageView (page: string) {
36-
posthog.capture('change tab', { page })
38+
export function captureAnalyticsPageView(page: string) {
39+
posthog.capture("change tab", { page });
3740
}
3841

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 } });
4144
}
4245

43-
export function identifyUser (user?: User | null) {
44-
if (!user?.email) return
46+
export function identifyUser(user?: User | null) {
47+
if (!user?.email) return;
4548
if (distinct_id) {
46-
posthog.alias(
47-
distinct_id,
48-
user.id,
49-
)
49+
posthog.alias(distinct_id, user.id);
5050
}
51-
posthog.setPersonProperties({ email: user.email, phone: user.phone })
51+
posthog.setPersonProperties({ email: user.email, phone: user.phone });
5252
}
5353

54-
export function useAnalytics () {
54+
export function useAnalytics() {
5555
useEffect(() => {
5656
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",
6799
},
68-
onReject: () => {
69-
disablePosthog()
70-
}
71-
}
72-
}
73-
}
100+
],
101+
},
102+
},
74103
},
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+
},
103105
});
104106
}, []);
105107
}
106108

107109
// TODO:
108110
// - enable PII filter
109-
// - show cookies CookieConsent.show();
111+
// - show cookies CookieConsent.show();

0 commit comments

Comments
 (0)