Skip to content

Add lazy userId resolver for drivers - #25

Merged
jszobody merged 4 commits into
masterfrom
lazy-user-id-resolver
Feb 19, 2026
Merged

Add lazy userId resolver for drivers#25
jszobody merged 4 commits into
masterfrom
lazy-user-id-resolver

Conversation

@jszobody

Copy link
Copy Markdown
Member

Summary

  • Adds resolveUserIdWith(\Closure) and getUserId() to AbstractDriver, available to all drivers
  • Adds resolveUserIdWith(\Closure) to MetricsManager which propagates to all drivers on creation
  • Refactors PostHog driver to use $this->getUserId() instead of a static $distinctId constructor param
  • The default auth/session/random logic in the service provider is now wrapped in a closure, evaluated lazily at metric dispatch time instead of at singleton creation
  • Anonymous fallback ID is memoized per process via static $anonymousId

Test plan

  • Verify userId resolver is called lazily (per format() call)
  • Verify custom resolver can be set on a driver
  • Verify getUserId() returns null when no resolver is set
  • All existing tests still pass

Replaces the eagerly-evaluated distinctId on the PostHog driver with
a closure-based resolveUserIdWith() on AbstractDriver. The default
auth/session/random logic now evaluates at metric dispatch time, not
at singleton creation. The resolver can also be set on MetricsManager
to propagate to all drivers.
The auth/session/random fallback logic now lives in
AbstractDriver::getUserId() so all drivers get sensible defaults.
Custom resolvers can still override via resolveUserIdWith().
PostHog handles its distinct_prefix internally.
Custom resolvers own the full ID. The prefix is only prepended
when falling back to the built-in auth/session/random resolution.
@jszobody
jszobody merged commit 58a0f97 into master Feb 19, 2026
1 check passed
@jszobody
jszobody deleted the lazy-user-id-resolver branch February 19, 2026 21:05
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