Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): add company group to api_surface_first_used

rramprakash9a8d7b3ef077
diff --git a/apps/api/src/services/posthog.ts b/apps/api/src/services/posthog.ts
index 07181b0e8..dd9dc93f5 100644
--- a/apps/api/src/services/posthog.ts
+++ b/apps/api/src/services/posthog.ts
@@ -118,6 +118,20 @@ async function resolveDistinctId(
}
}
+/** The org a team belongs to, for the PostHog `company` ($group_0) group. */
+async function resolveOrgId(teamId: string): Promise<string | null> {
+ try {
+ const rows = await dbRr
+ .select({ orgId: schema.teams.org_id })
+ .from(schema.teams)
+ .where(eq(schema.teams.id, teamId))
+ .limit(1);
+ return rows[0]?.orgId ?? null;
+ } catch {
+ return null;
+ }
+}
+
/**
* Emit a one-time `api_surface_first_used` event the first time a team makes a
* request from a given surface (playground / sdk / mcp / cli / api / ...).
@@ -162,7 +176,10 @@ export function trackFirstSurfaceUse(args: {
// Key to the person (api-key owner email) so the event attributes to the
// same PostHog person the dashboard identifies, e.g. for experiments.
- const distinctId = await resolveDistinctId(teamId, apiKeyId);
+ const [distinctId, orgId] = await Promise.all([
+ resolveDistinctId(teamId, apiKeyId),
+ resolveOrgId(teamId),
+ ]);
capturePostHog("api_surface_first_used", distinctId, {
surface,
@@ -171,8 +188,9 @@ export function trackFirstSurfaceUse(args: {
kind,
api_version: apiVersion,
team_id: teamId,
- // Associate with the PostHog `team` group for team-level analysis.
- $groups: { team: teamId },
+ // Associate with the PostHog `team` ($group_1) and, when resolvable, the
+ // `company` ($group_0) groups for group-level analysis.
+ $groups: { team: teamId, ...(orgId ? { company: orgId } : {}) },
});
} catch (error) {
_logger.debug("trackFirstSurfaceUse failed", {