mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
send client info to the analytics
This commit is contained in:
parent
99f5369513
commit
23eae7871a
2 changed files with 40 additions and 1 deletions
|
|
@ -74,6 +74,7 @@ export const createApp = (): Express => {
|
|||
if (client) {
|
||||
httpContext.set('client', client)
|
||||
}
|
||||
// TODO: get client info from user agent
|
||||
next()
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,42 @@
|
|||
import httpContext from 'express-http-context2'
|
||||
import { PostHog } from 'posthog-node'
|
||||
import { env } from '../env'
|
||||
|
||||
export const analytics = new PostHog(env.posthog.apiKey || 'test')
|
||||
interface AnalyticEvent {
|
||||
distinctId: string
|
||||
event: string
|
||||
properties?: Record<string | number, any>
|
||||
}
|
||||
|
||||
interface AnalyticClient {
|
||||
capture: (event: AnalyticEvent) => void
|
||||
shutdownAsync?: () => Promise<void>
|
||||
}
|
||||
|
||||
class PostHogClient implements AnalyticClient {
|
||||
private client: PostHog
|
||||
|
||||
constructor(apiKey: string) {
|
||||
this.client = new PostHog(apiKey)
|
||||
}
|
||||
|
||||
capture({ distinctId, event, properties }: AnalyticEvent) {
|
||||
// get client from request context
|
||||
const client = httpContext.get<string>('client') || 'other'
|
||||
|
||||
this.client.capture({
|
||||
distinctId,
|
||||
event,
|
||||
properties: {
|
||||
...properties,
|
||||
client,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async shutdownAsync() {
|
||||
return this.client.shutdownAsync()
|
||||
}
|
||||
}
|
||||
|
||||
export const analytics = new PostHogClient(env.posthog.apiKey || 'test')
|
||||
|
|
|
|||
Loading…
Reference in a new issue