Search code examples
phpgoogle-analyticsanalytics

Track Unique Visitors with PHP-GA Server Side Analytics Tracking


I'm attempting to use this PHP-GA tracking library for PHP but there seems to be an issue with tracking unique visitors. Others have had this issue, but the suggested solutions don't appear to work.

Here is what I've tried:

$tracker = new GoogleAnalytics\Tracker('UA-XXXXXXX-XX', 'testdomain.com');

if (!isset($_SESSION['SSGA_UniqueID3'])) {
    $visitor = new GoogleAnalytics\Visitor();
    $_SESSION['SSGA_UniqueID3'] = rand(1000000,2000000);
} else {
    $visitor = unserialize($_SESSION['SSGA_visitor']);
}

$visitor->setUniqueId($_SESSION['SSGA_UniqueID3']);
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$visitor->setScreenResolution('1024x768');

$session = new GoogleAnalytics\Session();
$visitor->addSession($session);
$_SESSION['SSGA_visitor'] = serialize($visitor);

Anyone know how to track unique visitors with this library?


Solution

  • After searching for a while on how to solve this, all that appears to be needed is to start the session. Normally this is a given, but it might be forgotten when using on an external system.

    session_start();