From e3186ae03c29bce97ea1b995f07eb264d365d5d9 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 4 Apr 2023 13:05:11 -0500 Subject: [PATCH] Only send analytics events when the user is logged in (web) --- src/lib/analytics.tsx | 1 + src/lib/analytics.web.tsx | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/lib/analytics.tsx b/src/lib/analytics.tsx index 8964027994..735b8249e1 100644 --- a/src/lib/analytics.tsx +++ b/src/lib/analytics.tsx @@ -20,6 +20,7 @@ export function useAnalytics() { if (store.session.hasSession) { return methods } + // dont send analytics pings for anonymous users return { screen: () => {}, track: () => {}, diff --git a/src/lib/analytics.web.tsx b/src/lib/analytics.web.tsx index 97f456893a..87fc63f363 100644 --- a/src/lib/analytics.web.tsx +++ b/src/lib/analytics.web.tsx @@ -1,6 +1,11 @@ import React from 'react' -import {createClient, AnalyticsProvider} from '@segment/analytics-react' +import { + createClient, + AnalyticsProvider, + useAnalytics as useAnalyticsOrig, +} from '@segment/analytics-react' import {RootStoreModel} from 'state/models/root-store' +import {useStores} from 'state/models/root-store' const segmentClient = createClient( { @@ -16,7 +21,26 @@ const segmentClient = createClient( ) export const track = segmentClient?.track?.bind?.(segmentClient) -export {useAnalytics} from '@segment/analytics-react' +export function useAnalytics() { + const store = useStores() + const methods = useAnalyticsOrig() + return React.useMemo(() => { + if (store.session.hasSession) { + console.log('real') + return methods + } + // dont send analytics pings for anonymous users + return { + screen: () => {}, + track: () => {}, + identify: () => {}, + flush: () => {}, + group: () => {}, + alias: () => {}, + reset: () => {}, + } + }, [store, methods]) +} export function init(_store: RootStoreModel) { // no init needed on web