From 3c48de3666383c387859f2ce7b3bea518391c556 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 4 Apr 2023 16:38:05 -0500 Subject: [PATCH] Add analytics identify() call --- package.json | 2 +- src/lib/analytics.tsx | 17 ++++++++++++++++- src/lib/analytics.web.tsx | 18 +++++++++++++++--- src/state/models/session.ts | 2 ++ src/view/com/auth/create/CreateAccount.tsx | 7 ++----- yarn.lock | 8 ++++---- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index c997a37645..35efccc685 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "e2e:run": "detox test --configuration ios.sim.debug --take-screenshots all" }, "dependencies": { - "@atproto/api": "0.2.1", + "@atproto/api": "0.2.3", "@bam.tech/react-native-image-resizer": "^3.0.4", "@expo/webpack-config": "^18.0.1", "@fortawesome/fontawesome-svg-core": "^6.1.1", diff --git a/src/lib/analytics.tsx b/src/lib/analytics.tsx index 735b8249e1..d0a8f12436 100644 --- a/src/lib/analytics.tsx +++ b/src/lib/analytics.tsx @@ -7,6 +7,7 @@ import { } from '@segment/analytics-react-native' import {RootStoreModel, AppInfo} from 'state/models/root-store' import {useStores} from 'state/models/root-store' +import {sha256} from 'js-sha256' const segmentClient = createClient({ writeKey: '8I6DsgfiSLuoONyaunGoiQM7A6y2ybdI', @@ -34,8 +35,22 @@ export function useAnalytics() { } 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() + } + } + }) + // NOTE - // this method is a copy of segment's own lifecycle event tracking + // this is a copy of segment's own lifecycle event tracking // we handle it manually to ensure that it never fires while the app is backgrounded // -prf segmentClient.isReady.onChange(() => { diff --git a/src/lib/analytics.web.tsx b/src/lib/analytics.web.tsx index 87fc63f363..467ae278b2 100644 --- a/src/lib/analytics.web.tsx +++ b/src/lib/analytics.web.tsx @@ -6,6 +6,7 @@ import { } 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( { @@ -26,7 +27,6 @@ export function useAnalytics() { const methods = useAnalyticsOrig() return React.useMemo(() => { if (store.session.hasSession) { - console.log('real') return methods } // dont send analytics pings for anonymous users @@ -42,8 +42,20 @@ export function useAnalytics() { }, [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<{}>) { diff --git a/src/state/models/session.ts b/src/state/models/session.ts index c2e10880db..96e058c027 100644 --- a/src/state/models/session.ts +++ b/src/state/models/session.ts @@ -25,6 +25,7 @@ export const accountData = z.object({ accessJwt: z.string().optional(), handle: z.string(), did: z.string(), + email: z.string().optional(), displayName: z.string().optional(), aviUrl: z.string().optional(), }) @@ -201,6 +202,7 @@ export class SessionModel { accessJwt, handle: session?.handle || existingAccount?.handle || '', + email: session?.email || existingAccount?.email || '', displayName: addedInfo ? addedInfo.displayName : existingAccount?.displayName || '', diff --git a/src/view/com/auth/create/CreateAccount.tsx b/src/view/com/auth/create/CreateAccount.tsx index 6ece903d65..467b879487 100644 --- a/src/view/com/auth/create/CreateAccount.tsx +++ b/src/view/com/auth/create/CreateAccount.tsx @@ -8,7 +8,6 @@ import { View, } from 'react-native' import {observer} from 'mobx-react-lite' -import {sha256} from 'js-sha256' import {useAnalytics} from 'lib/analytics' import {Text} from '../../util/text/Text' import {s, colors} from 'lib/styles' @@ -22,7 +21,7 @@ import {Step3} from './Step3' export const CreateAccount = observer( ({onPressBack}: {onPressBack: () => void}) => { - const {track, screen, identify} = useAnalytics() + const {track, screen} = useAnalytics() const pal = usePalette('default') const store = useStores() const model = React.useMemo(() => new CreateAccountModel(store), [store]) @@ -57,14 +56,12 @@ export const CreateAccount = observer( } else { try { await model.submit() - const email_hashed = sha256(model.email) - identify(email_hashed, {email_hashed}) track('Create Account') } catch { // dont need to handle here } } - }, [model, identify, track]) + }, [model, track]) return ( diff --git a/yarn.lock b/yarn.lock index 2312b55a51..979bbdc171 100644 --- a/yarn.lock +++ b/yarn.lock @@ -30,10 +30,10 @@ tlds "^1.234.0" typed-emitter "^2.1.0" -"@atproto/api@0.2.1": - version "0.2.1" - resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.2.1.tgz#034cab5928e1a6b0059e7237f6a82c57daadb264" - integrity sha512-ub92BFrHrm/r1En9IedqRc9r9BZy0i7J8mmFZ5EMxRJwdCJeMYB8CdmLfgNXQcsTPswbYF94pyZkrpeQNJWr1A== +"@atproto/api@0.2.3": + version "0.2.3" + resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.2.3.tgz#0eb9cb542c113b2c839f2c5ca284c30b117f489a" + integrity sha512-i0tWdOPQyZuSlkd2MY3s7QTac2ovH104tzy5rJwTZXZyhpf2Zom1xedaHb+pQmFzug7YaD7tx7OMSPlJIV0dpg== dependencies: "@atproto/common-web" "*" "@atproto/uri" "*"