Skip to content

Commit

Permalink
feat(metrics): Send isPatient0 property to Google Analytics (#7261)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianrunhe authored Oct 11, 2022
1 parent 39e9d38 commit a046fe7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
48 changes: 18 additions & 30 deletions packages/server/graphql/mutations/helpers/bootstrapNewUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ import User from '../../../database/types/User'
import generateUID from '../../../generateUID'
import insertUser from '../../../postgres/queries/insertUser'
import IUser from '../../../postgres/types/IUser'
import {analytics} from '../../../utils/analytics/analytics'
import segmentIo from '../../../utils/segmentIo'
import addSeedTasks from './addSeedTasks'
import createNewOrg from './createNewOrg'
import createTeamAndLeader from './createTeamAndLeader'
import isPatientZero from './isPatientZero'

// no waiting necessary, it's just analytics
const handleSegment = async (user: User, isInvited: boolean) => {
const {id: userId, createdAt, email, featureFlags, tier, segmentId, preferredName} = user
const bootstrapNewUser = async (newUser: User, isOrganic: boolean) => {
const {id: userId, createdAt, preferredName, email, featureFlags, tier, segmentId} = newUser
const domain = email.split('@')[1]
const isPatient0 = await isPatientZero(userId, domain)
const r = await getRethink()
const joinEvent = new TimelineEventJoinedParabol({userId})

await Promise.all([
r({
user: r.table('User').insert(newUser),
event: r.table('TimelineEvent').insert(joinEvent)
}).run(),
insertUser(newUser)
])

// Identify the user so user properties are set before any events are sent
segmentIo.identify({
userId,
traits: {
Expand All @@ -27,35 +40,10 @@ const handleSegment = async (user: User, isInvited: boolean) => {
isActive: true,
featureFlags,
highestTier: tier,
isPatient0: await isPatientZero(userId, domain)
isPatient0
},
anonymousId: segmentId
})
segmentIo.track({
userId,
event: 'Account Created',
properties: {
isInvited,
// properties below required for Google Analytics destination
category: 'All',
label: 'isInvited',
value: isInvited ? 1 : 0
}
})
}

const bootstrapNewUser = async (newUser: User, isOrganic: boolean) => {
const {id: userId, preferredName, email} = newUser
const r = await getRethink()
const joinEvent = new TimelineEventJoinedParabol({userId})

await Promise.all([
r({
user: r.table('User').insert(newUser),
event: r.table('TimelineEvent').insert(joinEvent)
}).run(),
insertUser(newUser)
])

const tms = [] as string[]
if (isOrganic) {
Expand Down Expand Up @@ -90,7 +78,7 @@ const bootstrapNewUser = async (newUser: User, isOrganic: boolean) => {
.insert([new SuggestedActionTryTheDemo({userId}), new SuggestedActionCreateNewTeam({userId})])
.run()
}
handleSegment(newUser, !isOrganic).catch()
analytics.accountCreated(userId, !isOrganic, isPatient0)

return new AuthToken({sub: userId, tms})
}
Expand Down
14 changes: 12 additions & 2 deletions packages/server/graphql/mutations/helpers/isPatientZero.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import getPatientZeroByDomain from '../../../postgres/queries/getPatientZeroByDomain'

const isPatientZero = async (userId: string, domain?: string | null) =>
domain ? userId === (await getPatientZeroByDomain(domain)).id : false
const isPatientZero = async (userId: string, domain?: string | null) => {
if (domain) {
const patient0 = await getPatientZeroByDomain(domain)
if (!patient0) {
return true
} else {
return userId === patient0.id
}
} else {
return false
}
}

export default isPatientZero
10 changes: 10 additions & 0 deletions packages/server/utils/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type AnalyticsEvent =
| 'Task Published'
| 'Task Estimate Set'
// user
| 'Account Created'
| 'Summary Email Setting Changed'

/**
Expand Down Expand Up @@ -320,6 +321,15 @@ class Analytics {
this.track(userId, 'Summary Email Setting Changed', {subscribeToSummaryEmail})
}

accountCreated = (userId: string, isInvited: boolean, isPatient0: boolean) => {
this.track(userId, 'Account Created', {
isInvited,
// properties below needed for Google Analytics goal setting
category: 'All',
label: isPatient0 ? 'isPatient0' : 'isNotPatient0'
})
}

private track = (userId: string, event: AnalyticsEvent, properties?: Record<string, any>) =>
this.segmentAnalytics.track(userId, event, properties)
}
Expand Down

0 comments on commit a046fe7

Please sign in to comment.