Only send analytics events when the user is logged in (web)

This commit is contained in:
Paul Frazee
2023-04-04 13:05:11 -05:00
parent 766ae282cb
commit e3186ae03c
2 changed files with 27 additions and 2 deletions
+1
View File
@@ -20,6 +20,7 @@ export function useAnalytics() {
if (store.session.hasSession) { if (store.session.hasSession) {
return methods return methods
} }
// dont send analytics pings for anonymous users
return { return {
screen: () => {}, screen: () => {},
track: () => {}, track: () => {},
+26 -2
View File
@@ -1,6 +1,11 @@
import React from 'react' 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 {RootStoreModel} from 'state/models/root-store'
import {useStores} from 'state/models/root-store'
const segmentClient = createClient( const segmentClient = createClient(
{ {
@@ -16,7 +21,26 @@ const segmentClient = createClient(
) )
export const track = segmentClient?.track?.bind?.(segmentClient) 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) { export function init(_store: RootStoreModel) {
// no init needed on web // no init needed on web