Fixes analytics init on web (#1923)
parent
610eeecd26
commit
952f5033d0
|
@ -4,10 +4,11 @@ import {
|
||||||
AnalyticsProvider,
|
AnalyticsProvider,
|
||||||
useAnalytics as useAnalyticsOrig,
|
useAnalytics as useAnalyticsOrig,
|
||||||
} from '@segment/analytics-react'
|
} from '@segment/analytics-react'
|
||||||
import {RootStoreModel} from 'state/models/root-store'
|
|
||||||
import {useStores} from 'state/models/root-store'
|
|
||||||
import {sha256} from 'js-sha256'
|
import {sha256} from 'js-sha256'
|
||||||
|
|
||||||
|
import {useSession} from '#/state/session'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
|
import {listenSessionLoaded} from '#/state/events'
|
||||||
|
|
||||||
const segmentClient = createClient(
|
const segmentClient = createClient(
|
||||||
{
|
{
|
||||||
|
@ -24,10 +25,10 @@ const segmentClient = createClient(
|
||||||
export const track = segmentClient?.track?.bind?.(segmentClient)
|
export const track = segmentClient?.track?.bind?.(segmentClient)
|
||||||
|
|
||||||
export function useAnalytics() {
|
export function useAnalytics() {
|
||||||
const store = useStores()
|
const {hasSession} = useSession()
|
||||||
const methods = useAnalyticsOrig()
|
const methods = useAnalyticsOrig()
|
||||||
return React.useMemo(() => {
|
return React.useMemo(() => {
|
||||||
if (store.session.hasSession) {
|
if (hasSession) {
|
||||||
return methods
|
return methods
|
||||||
}
|
}
|
||||||
// dont send analytics pings for anonymous users
|
// dont send analytics pings for anonymous users
|
||||||
|
@ -40,15 +41,14 @@ export function useAnalytics() {
|
||||||
alias: () => {},
|
alias: () => {},
|
||||||
reset: () => {},
|
reset: () => {},
|
||||||
}
|
}
|
||||||
}, [store, methods])
|
}, [hasSession, methods])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function init(store: RootStoreModel) {
|
export function init() {
|
||||||
store.onSessionLoaded(() => {
|
listenSessionLoaded(account => {
|
||||||
const sess = store.session.currentSession
|
if (account.did) {
|
||||||
if (sess) {
|
if (account.did) {
|
||||||
if (sess.did) {
|
const did_hashed = sha256(account.did)
|
||||||
const did_hashed = sha256(sess.did)
|
|
||||||
segmentClient.identify(did_hashed, {did_hashed})
|
segmentClient.identify(did_hashed, {did_hashed})
|
||||||
logger.debug('Ping w/hash')
|
logger.debug('Ping w/hash')
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue