mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-26 02:21:06 -04:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import {ENV_CONFIG} from 'common/envs/constants'
|
|
import {Request} from 'express'
|
|
import {PostHog} from 'posthog-node'
|
|
import {trackAuditEvent} from 'shared/audit-events'
|
|
import {log} from 'shared/utils'
|
|
|
|
const key = ENV_CONFIG.posthogKey
|
|
|
|
const client = new PostHog(key, {
|
|
host: 'https://us.i.posthog.com',
|
|
flushAt: 1,
|
|
flushInterval: 0,
|
|
})
|
|
|
|
export const track = async (userId: string, eventName: string, properties?: any) => {
|
|
try {
|
|
client.capture({
|
|
distinctId: userId,
|
|
event: eventName,
|
|
properties,
|
|
})
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
|
|
export const trackPublicEvent = async (userId: string, eventName: string, properties?: any) => {
|
|
const allProperties = Object.assign(properties ?? {}, {})
|
|
const {commentId, ...data} = allProperties
|
|
try {
|
|
client.capture({
|
|
distinctId: userId,
|
|
event: eventName,
|
|
properties,
|
|
})
|
|
await trackAuditEvent(userId, eventName, commentId, data)
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
|
|
export const getIp = (req: Request) => {
|
|
const xForwarded = req.headers['x-forwarded-for']
|
|
const xForwardedIp = Array.isArray(xForwarded) ? xForwarded[0] : xForwarded
|
|
const ip = xForwardedIp ?? req.socket.remoteAddress ?? req.ip
|
|
if (ip?.includes(',')) {
|
|
return ip.split(',')[0].trim()
|
|
}
|
|
return ip ?? ''
|
|
}
|