Skip to content

Commit def0b81

Browse files
committed
Rename to resolveUserIdUsing, extract getAnonymousId, pass driver to resolver
1 parent 58a0f97 commit def0b81

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,18 @@ This is used by the PostHog driver as the `distinctId`, and is available to any
286286

287287
### Custom user ID resolver
288288

289-
You can override the default resolution by providing your own closure:
289+
You can override the default resolution by providing your own closure. The driver instance is passed to your closure, so you can fall back to the built-in anonymous ID if needed:
290290

291291
```php
292-
Metrics::resolveUserIdWith(fn() => auth()->id());
292+
Metrics::resolveUserIdUsing(function($driver) {
293+
return auth()->check()
294+
? 'custom-prefix:' . auth()->id()
295+
: $driver->getAnonymousId();
296+
});
293297
```
294298

295299
This will apply to all drivers. You can also set it on a specific driver:
296300

297301
```php
298-
Metrics::driver('posthog')->resolveUserIdWith(fn() => $team->id);
302+
Metrics::driver('posthog')->resolveUserIdUsing(fn() => $team->id);
299303
```

src/Drivers/AbstractDriver.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function setExtra(array|\Closure $extra): static
6666

6767
protected ?\Closure $userIdResolver = null;
6868

69-
public function resolveUserIdWith(\Closure $resolver): static
69+
public function resolveUserIdUsing(\Closure $resolver): static
7070
{
7171
$this->userIdResolver = $resolver;
7272

@@ -76,18 +76,23 @@ public function resolveUserIdWith(\Closure $resolver): static
7676
public function getUserId(): mixed
7777
{
7878
if ($this->userIdResolver) {
79-
return call_user_func($this->userIdResolver);
79+
return call_user_func($this->userIdResolver, $this);
8080
}
8181

82-
static $anonymousId = null;
83-
8482
return match(true) {
8583
auth()->check() => auth()->id(),
8684
session()->isStarted() => sha1(session()->getId()),
87-
default => $anonymousId ??= Str::random()
85+
default => $this->getAnonymousId()
8886
};
8987
}
9088

89+
public function getAnonymousId(): string
90+
{
91+
static $anonymousId = null;
92+
93+
return $anonymousId ??= Str::random();
94+
}
95+
9196
/**
9297
* Implement this, when the driver needs to expose metrics to be polled by a third party service such as prometheus
9398
*/

src/Drivers/PostHog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function format(Metric $metric): array
5353
public function getUserId(): mixed
5454
{
5555
if ($this->userIdResolver) {
56-
return call_user_func($this->userIdResolver);
56+
return call_user_func($this->userIdResolver, $this);
5757
}
5858

5959
return $this->distinctPrefix . parent::getUserId();

src/MetricsManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MetricsManager extends Manager
2020

2121
protected ?\Closure $userIdResolver = null;
2222

23-
public function resolveUserIdWith(\Closure $resolver): static
23+
public function resolveUserIdUsing(\Closure $resolver): static
2424
{
2525
$this->userIdResolver = $resolver;
2626

@@ -46,7 +46,7 @@ protected function createDriver($driver)
4646
$driver = parent::createDriver($driver);
4747

4848
if($this->userIdResolver) {
49-
$driver->resolveUserIdWith($this->userIdResolver);
49+
$driver->resolveUserIdUsing($this->userIdResolver);
5050
}
5151

5252
if($this->driverCreatedCallback) {

tests/PostHogDriverTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testUserIdResolvedLazily()
5353

5454
$callCount = 0;
5555
$driver = app(PostHog::class);
56-
$driver->resolveUserIdWith(function () use (&$callCount) {
56+
$driver->resolveUserIdUsing(function () use (&$callCount) {
5757
$callCount++;
5858
return 'user_' . $callCount;
5959
});
@@ -73,7 +73,7 @@ public function testCustomUserIdResolver()
7373
$this->setupPostHog();
7474

7575
$driver = app(PostHog::class);
76-
$driver->resolveUserIdWith(fn() => 'custom-user-42');
76+
$driver->resolveUserIdUsing(fn() => 'custom-user-42');
7777

7878
$metric = new \STS\Metrics\Metric("file_uploaded");
7979
$formatted = $driver->format($metric);
@@ -102,10 +102,23 @@ public function testDistinctPrefixAppliesWithDefaultResolver()
102102
$this->assertStringStartsWith('user:', $formatted['distinctId']);
103103
}
104104

105+
public function testCustomResolverReceivesDriverAndCanFallBack()
106+
{
107+
$driver = new PostHog();
108+
$driver->resolveUserIdUsing(fn($driver) => $driver->getAnonymousId());
109+
110+
$metric = new \STS\Metrics\Metric("test");
111+
$formatted1 = $driver->format($metric);
112+
$formatted2 = $driver->format($metric);
113+
114+
$this->assertNotEmpty($formatted1['distinctId']);
115+
$this->assertEquals($formatted1['distinctId'], $formatted2['distinctId']);
116+
}
117+
105118
public function testDistinctPrefixSkippedWithCustomResolver()
106119
{
107120
$driver = new PostHog('user:');
108-
$driver->resolveUserIdWith(fn() => '42');
121+
$driver->resolveUserIdUsing(fn() => '42');
109122

110123
$metric = new \STS\Metrics\Metric("test");
111124
$formatted = $driver->format($metric);

0 commit comments

Comments
 (0)