Skip to content

: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.

3.0 KB
Raw Blame
import type { Dayjs } from 'lib/dayjs'

import {
    type SampleIds,
    type SampleRow,
    type SampleRowsSource,
    buildSampleRows,
    samplePersonDisplayName,
    samplePropertyValue,
} from '~/queries/nodes/DataTable/sampleRows'

interface SampleEvent {
    event: string
    email: string
    properties: Record<string, string | null>
    minutesAgo: number
}

const WEB = { $lib: 'web', $browser: 'Chrome', $os: 'Mac OS X' }

const SAMPLE_EVENTS: SampleEvent[] = [
    {
        event: '$pageview',
        email: '[email protected]',
        properties: { ...WEB, $current_url: 'https://example.com/pricing', $pathname: '/pricing', $screen_name: null },
        minutesAgo: 1,
    },
    {
        event: 'user signed up',
        email: '[email protected]',
        properties: { ...WEB, $current_url: 'https://example.com/signup', $pathname: '/signup', $screen_name: null },
        minutesAgo: 3,
    },
    {
        event: '$screen',
        email: '[email protected]',
        properties: {
            $lib: 'posthog-ios',
            $os: 'iOS',
            $browser: null,
            $current_url: null,
            $pathname: null,
            $screen_name: 'HomeScreen',
        },
        minutesAgo: 6,
    },
    {
        event: 'purchase completed',
        email: '[email protected]',
        properties: {
            $lib: 'posthog-node',
            $os: null,
            $browser: null,
            $current_url: null,
            $pathname: null,
            $screen_name: null,
        },
        minutesAgo: 12,
    },
]

const URL_OR_SCREEN_COLUMN = 'coalesce(properties.$current_url, properties.$screen_name)'

function sampleValue(key: string, sample: SampleEvent, ids: SampleIds, now: Dayjs): unknown {
    const timestamp = now.subtract(sample.minutesAgo, 'minute').toISOString()

    if (key === '*') {
        return {
            uuid: ids.eventId,
            event: sample.event,
            distinct_id: ids.distinctId,
            properties: sample.properties,
            timestamp,
        }
    }
    if (key === 'event') {
        return sample.event
    }
    if (key === 'timestamp') {
        return timestamp
    }
    if (key === 'distinct_id') {
        return ids.distinctId
    }
    if (key === 'person_display_name') {
        return samplePersonDisplayName(sample.email, ids)
    }
    if (key === 'person') {
        return { uuid: ids.personId, distinct_id: ids.distinctId, properties: { email: sample.email } }
    }
    if (key === URL_OR_SCREEN_COLUMN) {
        return sample.properties.$current_url ?? sample.properties.$screen_name
    }
    return samplePropertyValue(key, sample.properties)
}

export function buildSampleEventRows(columns: string[], now: Dayjs): SampleRow[] {
    return buildSampleRows(SAMPLE_EVENTS, columns, (key, sample, ids) => sampleValue(key, sample, ids, now))
}

export const SAMPLE_EVENTS_SOURCE: SampleRowsSource = {
    buildRows: buildSampleEventRows,
    // pinned: the data-attr value is an autocapture selector, so renaming it breaks dashboards
    dataAttr: 'events-table-sample-data',
    excludedColumns: [],
}