:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { dayjs } from 'lib/dayjs'
export interface DeepLinkTime {
// 'timestamp' is an absolute point in the recording (unix ms).
// 'offset' is a distance from the start of the recording (ms).
kind: 'timestamp' | 'offset'
valueMs: number
}
// `timestamp` is absolute (unix ms or ISO date). `t` is a seconds offset, but the CDP destination
// docs emit an ISO date in `t`, so `t` accepts a date too. kea-router coerces numeric params to numbers.
// Returns null when a param is present but unparseable.
export function parseDeepLinkTime(
timestampParam: string | number | undefined,
tParam: string | number | undefined
): DeepLinkTime | null {
if (isPresent(timestampParam)) {
const absolute = parseAbsoluteMs(timestampParam)
if (absolute !== null) {
return { kind: 'timestamp', valueMs: absolute }
}
}
if (isPresent(tParam)) {
const seconds = toFiniteNumber(tParam)
if (seconds !== null) {
return { kind: 'offset', valueMs: seconds * 1000 }
}
const absolute = parseDate(tParam)
if (absolute !== null) {
return { kind: 'timestamp', valueMs: absolute }
}
}
return null
}
// kea-router parses a numeric query param into a number, so `?t=0` arrives as 0, which is present
// but falsy. A truthiness check drops it and the recording starts wherever the player chooses.
function isPresent(value: string | number | undefined): value is string | number {
return value != null && value !== ''
}
function parseAbsoluteMs(value: string | number): number | null {
// A bare number is a unix-ms timestamp; anything else must be a parseable date.
return toFiniteNumber(value) ?? parseDate(value)
}
function toFiniteNumber(value: string | number): number | null {
if (typeof value === 'string' && value.trim() === '') {
return null
}
const asNumber = Number(value)
return Number.isFinite(asNumber) ? asNumber : null
}
// Zone-less and partial dates parse in the viewer's local zone, so the same link would land on
// different frames for different viewers. Require a full ISO datetime with an explicit zone.
// Error tracking links carry Python-formatted timestamps with a space instead of `T`.
const ISO_DATETIME_WITH_ZONE = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})$/
function parseDate(value: string | number): number | null {
if (typeof value !== 'string' || !ISO_DATETIME_WITH_ZONE.test(value)) {
return null
}
// Some browsers reject the space separator in `Date` parsing, so normalize it to `T`.
const parsed = dayjs(value.replace(' ', 'T'))
return parsed.isValid() ? parsed.valueOf() : null
}