Skip to content

Commit

Permalink
Update analytics.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
LMacPhail committed Feb 14, 2024
1 parent cb9d5a3 commit d82faef
Showing 1 changed file with 72 additions and 70 deletions.
142 changes: 72 additions & 70 deletions src/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,111 @@
import posthog from 'posthog-js'
import posthog from "posthog-js";
import "vanilla-cookieconsent/dist/cookieconsent.css";
import * as CookieConsent from "vanilla-cookieconsent";
import { useEffect } from 'react';
import { Session, User } from "@supabase/supabase-js";
import { useEffect } from "react";
import { User } from "@supabase/supabase-js";

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

export async function enablePosthog() {
if (!process.env.REACT_APP_POSTHOG_API_KEY || !process.env.REACT_APP_POSTHOG_API_URL) {
return
if (
!process.env.REACT_APP_POSTHOG_API_KEY ||
!process.env.REACT_APP_POSTHOG_API_URL
) {
return;
}
posthog.init(process.env.REACT_APP_POSTHOG_API_KEY!, {
api_host: process.env.REACT_APP_POSTHOG_API_URL,
loaded: function(posthog) {
loaded: function (posthog) {
distinct_id = posthog.get_distinct_id();
}
})
posthog.opt_in_capturing()
},
});
posthog.opt_in_capturing();
}

export async function disablePosthog() {
posthog.opt_out_capturing()
posthog.opt_out_capturing();
}

export function showPrivacyPreferences () {
export function showPrivacyPreferences() {
CookieConsent.show();
}

export function captureAnalyticsEvent (event: string, properties: any) {
posthog.capture(event, properties)
export function captureAnalyticsEvent(event: string, properties: any) {
posthog.capture(event, properties);
}

export function captureAnalyticsPageView (page: string) {
posthog.capture('change tab', { page })
export function captureAnalyticsPageView(page: string) {
posthog.capture("change tab", { page });
}

export function captureEmail (email: string) {
posthog.capture('newsletter signup', { $set: { email } })
export function captureEmail(email: string) {
posthog.capture("newsletter signup", { $set: { email } });
}

export function identifyUser (user?: User | null) {
if (!user?.email) return
export function identifyUser(user?: User | null) {
if (!user?.email) return;
if (distinct_id) {
posthog.alias(
distinct_id,
user.id,
)
posthog.alias(distinct_id, user.id);
}
posthog.setPersonProperties({ email: user.email, phone: user.phone })
posthog.setPersonProperties({ email: user.email, phone: user.phone });
}

export function useAnalytics () {
export function useAnalytics() {
useEffect(() => {
CookieConsent.run({
revision: 1,
categories: {
analytics: {
enabled: true,
readOnly: false,
services: {
posthog: {
label: 'PostHog',
onAccept: () => {
enablePosthog()
revision: 1,
categories: {
analytics: {
enabled: true,
readOnly: false,
services: {
posthog: {
label: "PostHog",
onAccept: () => {
enablePosthog();
},
onReject: () => {
disablePosthog();
},
},
},
},
},
language: {
default: "en",
translations: {
en: {
consentModal: {
title: "Can we use cookies to track usage?",
description:
"You'll help us learn how to improve the tool for others",
acceptAllBtn: "Accept",
acceptNecessaryBtn: "Reject",
showPreferencesBtn: "Manage privacy preferences",
},
preferencesModal: {
title: "Manage privacy preferences",
acceptAllBtn: "Accept",
acceptNecessaryBtn: "Reject",
savePreferencesBtn: "Save preferences",
closeIconLabel: "Close",
sections: [
{
title: "Usage analytics",
description:
"Tracks how the tool is used to help us improve it",
linkedCategory: "analytics",
},
onReject: () => {
disablePosthog()
}
}
}
}
],
},
},
},
language: {
default: 'en',
translations: {
en: {
consentModal: {
title: 'Can we use cookies to track usage?',
description: "You'll help us learn how to improve the tool for others",
acceptAllBtn: 'Accept',
acceptNecessaryBtn: 'Reject',
showPreferencesBtn: 'Manage privacy preferences'
},
preferencesModal: {
title: 'Manage privacy preferences',
acceptAllBtn: 'Accept',
acceptNecessaryBtn: 'Reject',
savePreferencesBtn: 'Save preferences',
closeIconLabel: 'Close',
sections: [
{
title: 'Usage analytics',
description: 'Tracks how the tool is used to help us improve it',
linkedCategory: 'analytics'
}
]
}
}
}
}
},
});
}, []);
}

// TODO:
// - enable PII filter
// - show cookies CookieConsent.show();
// - show cookies CookieConsent.show();

0 comments on commit d82faef

Please sign in to comment.