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.

1.6 KB
Raw Blame
import { DateTime } from 'luxon'

import { GroupTypeManager } from '~/common/groups/group-type-manager'
import { Properties } from '~/plugin-scaffold'
import { GroupTypeToColumnIndex, ProjectId, TeamId } from '~/types'

export function enrichPropertiesWithGroupTypes(
    properties: Properties,
    groupTypesToColumnIndex: GroupTypeToColumnIndex
): Properties {
    const groups = properties.$groups
    if (typeof groups !== 'object' || groups === null || Array.isArray(groups)) {
        return properties
    }
    for (const [groupType, groupIdentifier] of Object.entries(groups)) {
        if (groupType in groupTypesToColumnIndex) {
            // :TODO: Update event column instead
            const groupIndex = groupTypesToColumnIndex[groupType]
            properties[`$group_${groupIndex}`] = groupIdentifier
        }
    }
    return properties
}

export async function addGroupProperties(
    teamId: TeamId,
    projectId: ProjectId,
    properties: Properties,
    groupTypeManager: GroupTypeManager,
    eventTimestamp: DateTime
): Promise<Properties> {
    const groups = properties.$groups
    if (typeof groups !== 'object' || groups === null || Array.isArray(groups)) {
        return properties
    }
    const resolvedTypes: GroupTypeToColumnIndex = {}
    for (const [groupType] of Object.entries(groups)) {
        const columnIndex = await groupTypeManager.fetchGroupTypeIndex(teamId, projectId, groupType, eventTimestamp)
        if (columnIndex !== null) {
            resolvedTypes[groupType] = columnIndex
        }
    }
    return enrichPropertiesWithGroupTypes(properties, resolvedTypes)
}