How do I get PHP-GA to stop incrementing real-time visitors in google analytics?

余生颓废 提交于 2020-01-15 10:57:27

问题


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:


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);
    }
}


来源:https://stackoverflow.com/questions/20866407/how-do-i-get-php-ga-to-stop-incrementing-real-time-visitors-in-google-analytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!