Skip to content

Commit f8b3134

Browse files
committed
Actually send the session_id to GA4
1 parent aef580d commit f8b3134

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Jobs/SendEventToAnalytics.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ class SendEventToAnalytics implements ShouldQueue
2020

2121
public ?string $userId;
2222

23-
public function __construct($event, string $clientId = null, string $userId = null)
23+
public ?string $sessionId;
24+
25+
public function __construct($event, string $clientId = null, string $userId = null, string $sessionId = null)
2426
{
2527
$this->event = $event;
2628
$this->clientId = $clientId;
2729
$this->userId = $userId;
30+
$this->sessionId = $sessionId;
2831
}
2932

3033
public function handle(EventBroadcaster $broadcaster)
@@ -37,6 +40,10 @@ public function handle(EventBroadcaster $broadcaster)
3740
$broadcaster->withParameters(fn (GA4 $GA4) => $GA4->setUserId($this->userId));
3841
}
3942

43+
if ($this->sessionId) {
44+
$broadcaster->withParameters(fn (GA4 $GA4) => $GA4->setSessionId($this->sessionId));
45+
}
46+
4047
$broadcaster->handle($this->event);
4148
}
4249
}

src/Listeners/DispatchAnalyticsJob.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DevPro\GA4EventTracking\Listeners;
44

55
use DevPro\GA4EventTracking\Http\ClientIdRepository;
6+
use DevPro\GA4EventTracking\Http\SessionIdRepository;
67
use DevPro\GA4EventTracking\Jobs\SendEventToAnalytics;
78
use Illuminate\Queue\InteractsWithQueue;
89
use Illuminate\Support\Facades\Auth;
@@ -12,15 +13,17 @@ class DispatchAnalyticsJob
1213
use InteractsWithQueue;
1314

1415
public ClientIdRepository $clientIdRepository;
16+
public SessionIdRepository $sessionIdRepository;
1517

16-
public function __construct(ClientIdRepository $clientIdRepository)
18+
public function __construct(ClientIdRepository $clientIdRepository, SessionIdRepository $sessionIdRepository)
1719
{
1820
$this->clientIdRepository = $clientIdRepository;
21+
$this->sessionIdRepository = $sessionIdRepository;
1922
}
2023

2124
public function handle($event): void
2225
{
23-
$job = new SendEventToAnalytics($event, $this->clientIdRepository->get(), $this->userId());
26+
$job = new SendEventToAnalytics($event, $this->clientIdRepository->get(), $this->userId(), $this->sessionIdRepository->get());
2427
if ($queueName = config('ga4-event-tracking.tracking.queue_name')) {
2528
$job->onQueue($queueName);
2629
}

0 commit comments

Comments
 (0)