Files
kupshop/bundles/KupShop/GTMBundle/ServerSideGTMEvent/AnalyticsSessionIDsTrait.php
2025-08-02 16:30:27 +02:00

45 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GTMBundle\ServerSideGTMEvent;
use Symfony\Component\HttpFoundation\Request;
trait AnalyticsSessionIDsTrait
{
protected function getBrowserGASessionId(Request $request, string $number = '')
{
$dbcfg = \Settings::getDefault();
$measurement_id = $dbcfg->analytics['google_tag_manager']['ga4_measurement_id'.$number] ?? '';
$cookies = $request->cookies->all();
// Cookie name example: '_ga_1YS1VWHG3V'.
$cookie_name = '_ga_'.str_replace('G-', '', $measurement_id);
if (isset($cookies[$cookie_name])) {
// Cookie value example: 'GS2.1.s1746623129$o109$g1$t1746623938$j0$l0$h0'.
// Cookie value example: 'GS1.1.1659710029.4.1.1659710504.0'.
// Session Id: ^^^^^^^^^^.
if (preg_match('/^GS\d+\.\d+\.?s?(\d+)/', $cookies[$cookie_name], $matches)) {
return $matches[1];
}
}
return '';
}
protected function getGA3SessionId(): string
{
return preg_replace("/^.+\.(.+?\..+?)$/", '\\1', $_COOKIE['_ga'] ?? '');
}
protected function getBrowserFBPixelSessionId(?Request $request)
{
$cookies = $request?->cookies->all();
if (isset($cookies['_fbp'])) {
return $cookies['_fbp'];
}
return '';
}
}