Skip to content

Commit 7390f31

Browse files
authored
Use session-stored UUID7 for anonymous ID, require Laravel 11+ (#27)
Replace hashed session ID with a UUID7 stored via session()->remember() so anonymous users get a stable ID across requests and session regeneration. Fall back to a static UUID7 for non-session contexts. Bump minimum to Laravel 11 / PHP 8.2 for Str::uuid7() support.
1 parent d645675 commit 7390f31

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ The closure will be called fresh each time a metric is formatted. This also work
279279
Drivers automatically resolve the current user ID via `getUserId()`:
280280

281281
1. If a user is authenticated: `auth()->id()`
282-
2. Otherwise: `getAnonymousId()`, which returns a hashed session ID if a session is active, or a random string (stable for the lifetime of the process)
282+
2. Otherwise: `getAnonymousId()`, which returns a UUID stored in the session (stable across requests), or a static UUID for non-session contexts (CLI, queues)
283283

284284
This is used by the PostHog driver as the `distinctId`, and is available to any driver via `$driver->getUserId()`.
285285

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
}
1515
],
1616
"require": {
17-
"php": "^8.1",
18-
"illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0"
17+
"php": "^8.2",
18+
"illuminate/support": "^11.0|^12.0"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "^10.0|^11.5.3",
22-
"orchestra/testbench": "^8.0|^9.0|^10.0",
22+
"orchestra/testbench": "^9.0|^10.0",
2323
"mockery/mockery": "^1.6",
2424
"aws/aws-sdk-php": "^3.2",
2525
"guzzlehttp/guzzle": "^7.5",

src/Drivers/AbstractDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ public function getUserId(): mixed
8787
public function getAnonymousId(): string
8888
{
8989
if (session()->isStarted()) {
90-
return sha1(session()->getId());
90+
return session()->remember('metrics.anonymous_id', fn() => Str::uuid7()->toString());
9191
}
9292

9393
static $anonymousId = null;
9494

95-
return $anonymousId ??= Str::random();
95+
return $anonymousId ??= Str::uuid7()->toString();
9696
}
9797

9898
/**

0 commit comments

Comments
 (0)