Skip to content

Count people who use apps in product analytics - #395

Merged
tnkuehne merged 1 commit into
mainfrom
app-user-activity
Nov 25, 2026
Merged

tnkuehne merged 1 commit into
mainfrom
app-user-activity

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

People who use an app sign in through the organization's company sign-in and never open the control plane. Product analytics only identified control-plane visitors, so app users were not counted at all. App MCP analytics is attributed to the organization, not to a person.

Change

The app runtime reports app_user_active at the points where a person proves their identity, not on every request:

Trigger Where
sign_in Company sign-in callback on the auth host, once the session cookie is issued
renewal An expired app session renewed on an app request, with that app's app_id
embedded_sign_in A host token starts an embedded session
mcp_sign_in / mcp_renewal An MCP access token is issued or refreshed

Browser sessions renew with the identity provider's token lifetime and MCP access tokens last at most five minutes, so an active person produces one event per renewal. PostHog then counts unique people per day or week directly.

  • distinct_id is app-user: plus a SHA-256 of the provider's issuer and subject, the same for browser and MCP use. Name and email are person properties, and the organization is the group.
  • createAuthProxy accepts an optional observe callback and createAuthHandler adds the organization and app. The auth proxy stays independent of analytics. Observer failures never change responses.
  • Events are sent only when the runtime has POSTHOG_PROJECT_TOKEN, as for MCP analytics, and delivery runs in the background.

Verification

  • pnpm check
  • Auth proxy session tests: sign-in and renewal are reported once each, ordinary requests report nothing and a failing observer keeps sign-in working
  • Embedded sign-in test: the host-token sign-in is reported
  • MCP login runtime test: token issuance and refresh report the same person
  • Runtime observer in workerd: the delivered PostHog event and the unconfigured case
  • Worker-level auth proxy on the Cloudflare runtime: sign-in names the organization

RetriggerView in GreptileConfidence Score: 5/5

This PR appears safe to merge.

Summary

Adds app_user_active events when people sign in or renew their identity through browser, embedded, and MCP flows.

  • App sign-ins and browser session renewals now count people using apps.
  • MCP token issue and refresh now count the signed-in person.

No actionable issues found. Reviewed source and test coverage; tests were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  B[Browser sign-in or renewal] --> P[Auth proxy observer]
  E[Embedded sign-in] --> P
  P --> H[Add organization and optional app]
  H --> A[appUserAnalytics]
  M[MCP token issuance or refresh] --> A
  A --> C{PostHog token configured?}
  C -->|No| S[Skip analytics]
  C -->|Yes| I[Hash issuer and account ID]
  I --> D[Send event in background]
Loading

Reviews (1) · Last reviewed commit: "Count people who use apps in product ana..." · Reviewed by Greptile

App users sign in through the organization's company sign-in and never
open the control plane, so product analytics had no record of them.

The app runtime now reports an app_user_active event when a person
signs in to an app, when an expired app session is renewed, when a
host token starts an embedded session and when an MCP token is issued
or refreshed. Sessions renew with the identity provider's token
lifetime, so active people produce one event per renewal rather than
one per request. The person is identified by a hash of the provider's
issuer and subject, with name and email as person properties, and
grouped by organization. Renewals name the app that triggered them.

Events are sent only when the runtime has a PostHog project token, as
for MCP analytics. Delivery runs in the background and failures never
affect sign-in or app requests.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs e1db561 Nov 25 2026, 02:13 PM

@tnkuehne
tnkuehne added this pull request to stack #397 November 25, 2026 14:27
@tnkuehne
tnkuehne merged commit 372714b into main Nov 25, 2026
16 of 18 checks passed
@tnkuehne
tnkuehne deleted the app-user-activity branch November 25, 2026 14:55
tnkuehne added a commit that referenced this pull request Nov 25, 2026
## Problem

With #395, a member who also uses apps appears as two people in product
analytics: `user:<id>` from the control plane and `app-user:<hash>` from
app sign-in.

## Change

- `appUserDistinctId` in `@platform/contracts` derives the app user ID
from issuer and subject. The runtime and the control plane use it, so
both produce the same ID.
- On every control-plane sign-in, the server sends `$create_alias` from
`user:<id>` for each of the person's app identities:
- the identity apps issue when they sign in through Widefleet
(`<PLATFORM_URL>/api/auth`, user ID)
- the company identity of each organization whose company sign-in the
person has an account with, as `createAppUserIdentity` already derives
it, including Entra's object ID
- Company identities come from the person's company accounts, not
memberships. Company sign-in adds the membership after the session, so
the first company sign-in would otherwise link nothing.
- Linking is awaited in the session hook so it is captured before the
request's analytics drain. Failures never affect sign-in.

App use before a person's next control-plane sign-in merges into their
account when the alias arrives.

## Verification

- `pnpm check`
- App identity integration test: the first company sign-in links the
Widefleet identity and the company identity, and the linked ID equals
the one derived from the identity app sign-in issues
- Server analytics test: the alias is delivered as `$create_alias` with
`distinct_id: user:<id>`
- Not verified against PostHog itself yet: whether a person already
created by `app_user_active` events merges into the account. That needs
a separate test project.

Stacked on #395.




<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=119518203&amp;sig=3c7b76ee994909a694ac18759cf6b7d78e8047ef758f26757b4eb04d02d2c896"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F396&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5</h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge; both
previous findings are fixed.

<details open><summary><h3>Summary</h3></summary>

Links members' app identities to their control-plane account in product
analytics.
- App activity now joins the member’s account in product analytics.
</details>

<details open><summary><h3>Diagram</h3></summary>

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Control-plane sign-in] --> B[Await observer alias]
  B --> C{Analytics enabled?}
  C -->|No| D[Skip identity lookups]
  C -->|Yes| E[Read app identity IDs]
  E --> F[Queue aliases for user account]
  F --> G[Drain analytics after request]
```
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (2) · Last reviewed commit: ["Forward app identity links on
Cloudflare..."](31b229a)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant