diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-04-05 11:15:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-05 11:15:22 -0500 |
commit | 8e28d3c6be8e063b6d563b0068cb4fc907ff5df0 (patch) | |
tree | 612615f2e5000e6870b6945f5592fa252d3b7db5 /src/lib/analytics.web.tsx | |
parent | 92b80ff048db251e1e4afa74ed3006268aee5929 (diff) | |
download | voidsky-8e28d3c6be8e063b6d563b0068cb4fc907ff5df0.tar.zst |
* Only send analytics events when the user is logged in * Only send analytics events when the user is logged in (web) * Add analytics identify() call
Diffstat (limited to 'src/lib/analytics.web.tsx')
-rw-r--r-- | src/lib/analytics.web.tsx | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/src/lib/analytics.web.tsx b/src/lib/analytics.web.tsx index 97f456893..467ae278b 100644 --- a/src/lib/analytics.web.tsx +++ b/src/lib/analytics.web.tsx @@ -1,6 +1,12 @@ 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' +import {sha256} from 'js-sha256' const segmentClient = createClient( { @@ -16,10 +22,40 @@ 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) { + 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 +export function init(store: RootStoreModel) { + store.onSessionLoaded(() => { + const sess = store.session.currentSession + if (sess) { + if (sess.email) { + store.log.debug('Ping w/hash') + const email_hashed = sha256(sess.email) + segmentClient.identify(email_hashed, {email_hashed}) + } else { + store.log.debug('Ping w/o hash') + segmentClient.identify() + } + } + }) } export function Provider({children}: React.PropsWithChildren<{}>) { |