Merge pull request #3904 from omnivore-app/feature/client-in-analytics
send client info to the analytics
This commit is contained in:
@ -21,6 +21,7 @@ import { articleRouter } from './routers/article_router'
|
||||
import { authRouter } from './routers/auth/auth_router'
|
||||
import { mobileAuthRouter } from './routers/auth/mobile/mobile_auth_router'
|
||||
import { digestRouter } from './routers/digest_router'
|
||||
import { explainRouter } from './routers/explain_router'
|
||||
import { integrationRouter } from './routers/integration_router'
|
||||
import { localDebugRouter } from './routers/local_debug_router'
|
||||
import { notificationRouter } from './routers/notification_router'
|
||||
@ -42,9 +43,9 @@ import { userRouter } from './routers/user_router'
|
||||
import { sentryConfig } from './sentry'
|
||||
import { analytics } from './utils/analytics'
|
||||
import { corsConfig } from './utils/corsConfig'
|
||||
import { getClientFromUserAgent } from './utils/helpers'
|
||||
import { buildLogger, buildLoggerTransport, logger } from './utils/logger'
|
||||
import { apiLimiter, authLimiter } from './utils/rate_limit'
|
||||
import { explainRouter } from './routers/explain_router'
|
||||
|
||||
const PORT = process.env.PORT || 4000
|
||||
|
||||
@ -70,10 +71,18 @@ export const createApp = (): Express => {
|
||||
// set client info in the request context
|
||||
app.use(httpContext.middleware)
|
||||
app.use('/api/', (req, res, next) => {
|
||||
// get client info from header
|
||||
const client = req.header('X-OmnivoreClient')
|
||||
if (client) {
|
||||
httpContext.set('client', client)
|
||||
}
|
||||
|
||||
// get client info from user agent
|
||||
const userAgent = req.header('User-Agent')
|
||||
if (userAgent) {
|
||||
const client = getClientFromUserAgent(userAgent)
|
||||
httpContext.set('client', client)
|
||||
}
|
||||
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')
|
||||
|
||||
@ -411,3 +411,15 @@ export const setRecentlySavedItemInRedis = async (
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const getClientFromUserAgent = (userAgent: string): string => {
|
||||
// for plugins, currently only obsidian and logseq are supported
|
||||
const plugins = userAgent.match(/(obsidian|logseq)/i)
|
||||
if (plugins) return plugins[0].toLowerCase()
|
||||
|
||||
// web browser
|
||||
const browsers = userAgent.match(/(chrome|safari|firefox|edge|opera)/i)
|
||||
if (browsers) return 'web'
|
||||
|
||||
return 'other'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user