How do I get PHP-GA to stop incrementing real-time

2019-08-10 07:01发布

I'm looking at using php-ga (https://packagist.org/packages/unitedprototype/php-ga) to send simple analytics to GA - basically capturing when an API endpoint is hit.

I'm using the code below and it's working well, but GA is treating every endpoint hit (tested by refreshing a test page) as a unique visitor. Has anyone out there seen this before? The API knows 'who' is contacting it, thanks to authentication.

Can anyone show me how to tell GA that each refresh is actually a single user session, rather than a brand new visit?

use UnitedPrototype\GoogleAnalytics;


$ga = new GoogleAnalytics\Tracker('UA-12345678-1', 'mysite.com');
$visitor = new GoogleAnalytics\Visitor();
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);

$session = new GoogleAnalytics\Session();

// Assemble Page information
$page = new GoogleAnalytics\Page('/api/v1/test-ga-thingy');
$page->setTitle('Testing the API Thingy');

// Track page view
$ga->trackPageview($page, $session, $visitor);

1条回答
聊天终结者
2楼-- · 2019-08-10 07:38

I had the same problem, to solve this you need to get PHP to check if the analytics cookie is set, if it is then it shouldn't create a new one, if it isn't it should. Open the file GoogleAnalytics/Session.php and go to line number 72. Over there you'll find this code:

public function __construct() {
    $this->setSessionId($this->generateSessionId());
    $this->setTrackCount(0);
    $this->setStartTime(new DateTime());
}

Once you create a new instance of Session, it'll go through these lines. But over here it gives every instance a newly generated sessionid, gives it a new trackcount and a new generated startTime. Well that's not what we want, so you'll need to change it to see if there allready are any cookies from google analytics and if there are, it should use those. Change these lines to this:

public function __construct() {
    if(!isset($_COOKIE['__utmb'])){
        $this->setSessionId($this->generateSessionId());
        $this->setTrackCount(0);
        $this->setStartTime(new DateTime());
    }else{
        $this->setSessionId(_COOKIE['__utmc']);
        $this->fromUtmb($_COOKIE['__utmb']);
    }
}

That solves 1 problem, but theres another one, each time you create a new instance of the class "Visitor" theres the same problem. Open the file GoogleAnalytics/Visitor.php and go to line number 150. Over there you'll find this code:

public function __construct() {
    // ga.js sets all three timestamps to now for new visitors, so we do the same
    $now = new DateTime();
    $this->setFirstVisitTime($now);
    $this->setPreviousVisitTime($now);
    $this->setCurrentVisitTime($now);
    $this->setVisitCount(1);        
}

You'll need to change it into:

public function __construct() {
    // ga.js sets all three timestamps to now for new visitors, so we do the same
    if(isset($_COOKIE['__utma'])){
        $this->fromUtma($_COOKIE['__utma']);
    }else{
        $now = new DateTime();
        $this->setFirstVisitTime($now);
        $this->setPreviousVisitTime($now);
        $this->setCurrentVisitTime($now);
        $this->setVisitCount(1);
    }
}
查看更多
登录 后发表回答