📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
import _isString from 'lodash/isString'
import { PID_REGEX } from '../../common/constants'
import { getIPFromHeaders, TRUSTED_PROXY_IPS } from '../../common/utils'
const firstHeaderValue = (value: unknown): string => {
if (_isString(value) && value) {
return value.trim()
}
if (Array.isArray(value) && _isString(value[0]) && value[0]) {
return value[0].trim()
}
return ''
}
export const getTrustworthyIp = (req: {
headers: Record<string, unknown>
ip?: string
}): string => {
const connectionIp = firstHeaderValue(req.headers['x-real-ip'])
if (connectionIp && TRUSTED_PROXY_IPS.has(connectionIp)) {
return firstHeaderValue(req.headers['x-client-ip-address'])
}
if (connectionIp) {
return connectionIp
}
return getIPFromHeaders(req.headers) || req.ip || ''
}
export const getSinglePid = (
query: Record<string, unknown>,
params?: Record<string, unknown>,
): string | null => {
const paramPid = params?.pid
if (_isString(paramPid) && PID_REGEX.test(paramPid)) {
return paramPid
}
const { pid, pids } = query
if (pid !== undefined && pids !== undefined) {
return null
}
if (_isString(pid) && pid) {
return pid
}
if (_isString(pids) && pids) {
try {
const parsed = JSON.parse(pids)
if (
Array.isArray(parsed) &&
parsed.length === 1 &&
_isString(parsed[0])
) {
return parsed[0]
}
} catch {
return null
}
}
return null
}
export const getAnalyticsRoute = (path: string): string =>
path.replace(/^\/(v1\/)?log\/?/, '').replace(/\/$/, '')