Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(telemetry): make team invite a queryable signup source (#7789)

claude[bot]2c0837f35523
diff --git a/backend/src/server/routes/v1/sso-router.ts b/backend/src/server/routes/v1/sso-router.ts
index c3159854ae..e9270ba995 100644
--- a/backend/src/server/routes/v1/sso-router.ts
+++ b/backend/src/server/routes/v1/sso-router.ts
@@ -34,7 +34,7 @@ import { addAuthOriginDomainCookie } from "@app/server/lib/cookie";
import { AuthMethod, ProviderAuthResult } from "@app/services/auth/auth-type";
import { OrgAuthMethod } from "@app/services/org/org-types";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
-import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
+import { PostHogEventTypes, SignupAttributionType, SignupSource } from "@app/services/telemetry/telemetry-types";
const passport = new Authenticator({ key: "sso", userProperty: "passportUser" });
@@ -520,7 +520,10 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
properties: {
username: user.username,
email: user.email ?? "",
- ...(passportResult.wasInvited ? { attributionSource: "Team Invite" } : {}),
+ ...(passportResult.wasInvited
+ ? { attributionSource: "Team Invite", attributionType: SignupAttributionType.SystemDerived }
+ : {}),
+ signupSource: passportResult.wasInvited ? SignupSource.TeamInvite : SignupSource.SelfServe,
signupMethod
}
});
diff --git a/backend/src/server/routes/v3/signup-router.ts b/backend/src/server/routes/v3/signup-router.ts
index e62da8b8c5..54ecb4f2f2 100644
--- a/backend/src/server/routes/v3/signup-router.ts
+++ b/backend/src/server/routes/v3/signup-router.ts
@@ -13,7 +13,7 @@ import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { CompleteAccountType } from "@app/services/auth/auth-signup-type";
import { AuthMode } from "@app/services/auth/auth-type";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
-import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
+import { PostHogEventTypes, SignupAttributionType, SignupSource } from "@app/services/telemetry/telemetry-types";
import { SanitizedUserSchema } from "../sanitizedSchemas";
@@ -135,7 +135,7 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
if (!userAgent) throw new Error("user agent header is required");
const appCfg = getConfig();
- const { user, accessToken, refreshToken, authMethod, organizationId, isInvitedUser } =
+ const { user, accessToken, refreshToken, authMethod, organizationId, invitingOrgId, isInvitedUser } =
await server.services.signup.completeAccount({
...req.body,
ip: req.realIp,
@@ -155,14 +155,25 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
}
const bodyAttributionSource = "attributionSource" in req.body ? req.body.attributionSource : undefined;
+ let attributionType: SignupAttributionType | undefined;
+ if (isInvitedUser) {
+ attributionType = SignupAttributionType.SystemDerived;
+ } else if (bodyAttributionSource) {
+ attributionType = SignupAttributionType.UserProvided;
+ }
+ // Invited signups create no org of their own, so fall back to the inviting org. The
+ // OrganizationCreated event below stays on `organizationId`, which is only set when one was created.
+ const signupOrganizationId = organizationId ?? invitingOrgId;
void server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.UserSignedUp,
distinctId: user.username ?? "",
- ...(organizationId ? { organizationId } : {}),
+ ...(signupOrganizationId ? { organizationId: signupOrganizationId } : {}),
properties: {
username: user.username,
email: user.email ?? "",
attributionSource: isInvitedUser ? "Team Invite" : bodyAttributionSource,
+ ...(attributionType ? { attributionType } : {}),
+ signupSource: isInvitedUser ? SignupSource.TeamInvite : SignupSource.SelfServe,
signupMethod: isInvitedUser ? "invite" : authMethod
}
});
@@ -307,7 +318,8 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
distinctId,
...(organizationId ? { organizationId } : {}),
properties: {
- attributionSource: req.body.attributionSource
+ attributionSource: req.body.attributionSource,
+ attributionType: SignupAttributionType.UserProvided
}
});
}
diff --git a/backend/src/services/auth/auth-fns.ts b/backend/src/services/auth/auth-fns.ts
index a18321f5ac..6edeea51c7 100644
--- a/backend/src/services/auth/auth-fns.ts
+++ b/backend/src/services/auth/auth-fns.ts
@@ -168,3 +168,15 @@ export const verifyCaptcha = async (consecutiveFailedPasswordAttempts?: number |
}
}
};
+
+/*
+ * The org that invited a user who has not accepted yet: the org the signup token was issued for,
+ * and only when it is still one of the user's own pending invitations, since the token outlives the
+ * invitation it was minted for. Nothing else identifies the invite a signup came from, and a user
+ * can be invited by several orgs before accepting, so anything inferred from the memberships alone
+ * would group the signup under an org that did not recruit them. Attribute nothing instead.
+ */
+export const resolveInvitingOrgId = (pendingInviteMemberships: { scopeOrgId: string }[], invitedOrgId?: string) =>
+ invitedOrgId && pendingInviteMemberships.some((membership) => membership.scopeOrgId === invitedOrgId)
+ ? invitedOrgId
+ : undefined;
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index faf320e898..5d3c648617 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -809,12 +809,19 @@ export const authLoginServiceFactory = ({
// user's signup (used by the caller to fire signup telemetry exactly once per account).
const wasUserAcceptedBeforeLogin = Boolean(user?.isAccepted);
- // Mirror complete-account's invite detection: a not-yet-accepted user who already has an org
- // membership was invited (the inviter created it), versus an organic signup that has none yet.
- // Only meaningful when this login completes the signup, so skip the read for accepted users.
+ // Mirror complete-account's invite detection: a not-yet-accepted user who already holds a
+ // pending invitation was invited (the inviter created it), versus an organic signup that has
+ // none. Only meaningful when this login completes the signup, so skip the read for accepted
+ // users.
const wasInvited =
!wasUserAcceptedBeforeLogin && user
- ? (await orgDAL.findMembership({ actorUserId: user.id, scope: AccessScope.Organization })).length > 0
+ ? (
+ await orgDAL.findMembership({
+ actorUserId: user.id,
+ scope: AccessScope.Organization,
+ status: OrgMembershipStatus.Invited
+ })
+ ).length > 0
: false;
const serverCfg = await getServerCfg();
diff --git a/backend/src/services/auth/auth-signup-service.ts b/backend/src/services/auth/auth-signup-service.ts
index ec38527071..b31d466614 100644
--- a/backend/src/services/auth/auth-signup-service.ts
+++ b/backend/src/services/auth/auth-signup-service.ts
@@ -15,7 +15,7 @@ import { SmtpTemplates, throwIfSmtpError, TSmtpService } from "../smtp/smtp-serv
import { TUserDALFactory } from "../user/user-dal";
import { TUserAliasDALFactory } from "../user-alias/user-alias-dal";
import { TAuthDALFactory } from "./auth-dal";
-import { extractBearerToken } from "./auth-fns";
+import { extractBearerToken, resolveInvitingOrgId } from "./auth-fns";
import { TAuthLoginFactory } from "./auth-login-service";
import { TSignupOnboardingResponseDALFactory } from "./auth-signup-onboarding-dal";
import { CompleteAccountType, TCompleteAccountDTO, TRecordSignupOnboardingDTO } from "./auth-signup-type";
@@ -183,6 +183,7 @@ export const authSignupServiceFactory = ({
// whether the request is valid. This prevents timing-based user/alias enumeration.
let authMethod: AuthMethod;
let organizationId: string | undefined;
+ let invitingOrgId: string | undefined;
let isInvitedUser = false;
if (dto.type === CompleteAccountType.Email) {
// Determine rejection before hashing, but don't throw yet
@@ -210,8 +211,15 @@ export const authSignupServiceFactory = ({
},
{ tx }
);
- isInvitedUser = existingMemberships.length > 0;
- if (!isInvitedUser && dto.organizationName) {
+ // Org creation is skipped whenever the user already belongs to an org at all. Attribution is
+ // narrower: only a pending invitation means someone recruited them, since a signup can pick
+ // up an `Accepted` membership of its own accord (the self-hosted default auth org).
+ const pendingInviteMemberships = existingMemberships.filter(
+ (membership) => membership.status === OrgMembershipStatus.Invited
+ );
+ isInvitedUser = pendingInviteMemberships.length > 0;
+ invitingOrgId = resolveInvitingOrgId(pendingInviteMemberships, decodedToken.organizationId);
+ if (!existingMemberships.length && dto.organizationName) {
const org = await orgService.createOrganization(
{
userId: user.id,
@@ -257,6 +265,20 @@ export const authSignupServiceFactory = ({
throw new BadRequestError({ message: "Invalid token" });
}
+ // An invitee whose provider did not verify their email completes signup here instead of in
+ // the email branch, so derive the same invite state. Only `Invited` counts: a signup can pick
+ // up an `Accepted` membership of its own accord (the self-hosted default auth org), and the
+ // rest of the codebase already treats `Invited` as what a pending invitation is.
+ if (!user.isAccepted) {
+ const pendingInviteMemberships = await orgDAL.findMembership({
+ actorUserId: user.id,
+ scope: AccessScope.Organization,
+ status: OrgMembershipStatus.Invited
+ });
+ isInvitedUser = pendingInviteMemberships.length > 0;
+ invitingOrgId = resolveInvitingOrgId(pendingInviteMemberships, decodedToken.organizationId);
+ }
+
// Update user-level verification flags based on auth method
const userUpdates: Record<string, boolean> = { isEmailVerified: true };
if (decodedToken.authMethod === AuthMethod.GOOGLE) {
@@ -310,6 +332,7 @@ export const authSignupServiceFactory = ({
refreshToken: tokens.refresh,
authMethod,
organizationId,
+ invitingOrgId,
isInvitedUser
};
};
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index bf7a48d7d3..e656d282b8 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -1139,7 +1139,10 @@ export const orgServiceFactory = ({
const token = crypto.jwt().sign(
{
authTokenType: AuthTokenType.SIGNUP_TOKEN,
- userId: user.id
+ userId: user.id,
+ // the invite this signup was started from, so completing the account can attribute it to
+ // this org rather than guess among every org that invited the user
+ organizationId: orgMembership.scopeOrgId
},
appCfg.AUTH_SECRET,
{
diff --git a/backend/src/services/telemetry/telemetry-types.ts b/backend/src/services/telemetry/telemetry-types.ts
index d6a7f014c0..16da13d1af 100644
--- a/backend/src/services/telemetry/telemetry-types.ts
+++ b/backend/src/services/telemetry/telemetry-types.ts
@@ -343,12 +343,33 @@ export type TAdminInitEvent = {
};
};
+/**
+ * Where a signup came from, as a value the analytics layer can group on. `attributionSource` stays
+ * as-is for the historical queries built on it, including its `"Team Invite"` sentinel.
+ */
+export enum SignupSource {
+ TeamInvite = "team_invite",
+ SelfServe = "self_serve"
+}
+
+/**
+ * How we know the attribution: `UserProvided` is an answer the person typed, `SystemDerived` is one
+ * the signup flow already knew. Without it the two are only separable by matching the sentinel, so
+ * every attribution measure has to exclude invites instead of counting them.
+ */
+export enum SignupAttributionType {
+ UserProvided = "user_provided",
+ SystemDerived = "system_derived"
+}
+
export type TUserSignedUpEvent = {
event: PostHogEventTypes.UserSignedUp;
properties: {
username: string;
email: string;
attributionSource?: string;
+ attributionType?: SignupAttributionType;
+ signupSource?: SignupSource;
signupMethod?: string;
};
};
@@ -389,6 +410,7 @@ export type TSignupAttributionProvidedEvent = {
event: PostHogEventTypes.SignupAttributionProvided;
properties: {
attributionSource: string;
+ attributionType?: SignupAttributionType;
};
};