Skip to content

Commit ece4309

Browse files
authored
Merge pull request firecrawl#4091 from firecrawl/fix/api-surface-first-used-company-group
fix(api): add company group to api_surface_first_used
2 parents 93387da + 9a8d7b3 commit ece4309

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

apps/api/src/services/posthog.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ async function resolveDistinctId(
118118
}
119119
}
120120

121+
/** The org a team belongs to, for the PostHog `company` ($group_0) group. */
122+
async function resolveOrgId(teamId: string): Promise<string | null> {
123+
try {
124+
const rows = await dbRr
125+
.select({ orgId: schema.teams.org_id })
126+
.from(schema.teams)
127+
.where(eq(schema.teams.id, teamId))
128+
.limit(1);
129+
return rows[0]?.orgId ?? null;
130+
} catch {
131+
return null;
132+
}
133+
}
134+
121135
/**
122136
* Emit a one-time `api_surface_first_used` event the first time a team makes a
123137
* request from a given surface (playground / sdk / mcp / cli / api / ...).
@@ -162,7 +176,10 @@ export function trackFirstSurfaceUse(args: {
162176

163177
// Key to the person (api-key owner email) so the event attributes to the
164178
// same PostHog person the dashboard identifies, e.g. for experiments.
165-
const distinctId = await resolveDistinctId(teamId, apiKeyId);
179+
const [distinctId, orgId] = await Promise.all([
180+
resolveDistinctId(teamId, apiKeyId),
181+
resolveOrgId(teamId),
182+
]);
166183

167184
capturePostHog("api_surface_first_used", distinctId, {
168185
surface,
@@ -171,8 +188,9 @@ export function trackFirstSurfaceUse(args: {
171188
kind,
172189
api_version: apiVersion,
173190
team_id: teamId,
174-
// Associate with the PostHog `team` group for team-level analysis.
175-
$groups: { team: teamId },
191+
// Associate with the PostHog `team` ($group_1) and, when resolvable, the
192+
// `company` ($group_0) groups for group-level analysis.
193+
$groups: { team: teamId, ...(orgId ? { company: orgId } : {}) },
176194
});
177195
} catch (error) {
178196
_logger.debug("trackFirstSurfaceUse failed", {

0 commit comments

Comments
 (0)