简体   繁体   English

如何使PHP-GA停止增加Google Analytics(分析)中的实时访问者?

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

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. 我正在使用php-ga( https://packagist.org/packages/unitedprototype/php-ga )向GA发送简单的分析数据-基本是在API端点被命中时捕获。

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. 我正在使用下面的代码,并且运行良好,但是GA会将唯一匹配的访问者(通过刷新测试页进行测试)视为每个匹配点。 Has anyone out there seen this before? 外面有人看过吗? The API knows 'who' is contacting it, thanks to authentication. 由于进行身份验证,API知道“谁”正在联系它。

Can anyone show me how to tell GA that each refresh is actually a single user session, rather than a brand new visit? 谁能告诉我如何告诉GA每次刷新实际上是单个用户会话,而不是全新的访问?

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

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. 我遇到了同样的问题,要解决此问题,您需要让PHP检查是否设置了分析cookie,如果已设置,则不应创建新的cookie,否则应创建一个新cookie。 Open the file GoogleAnalytics/Session.php and go to line number 72 . 打开文件GoogleAnalytics/Session.php并转到第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. 一旦创建了新的Session实例,它将经历以下几行。 But over here it gives every instance a newly generated sessionid, gives it a new trackcount and a new generated startTime. 但是在这里,它为每个实例提供一个新生成的sessionid,为其提供新的trackcount和新生成的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. 好吧,这不是我们想要的,因此您需要对其进行更改,以查看是否有来自Google Analytics(分析)的所有cookie,如果有,它应该使用它们。 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. 这解决了一个问题,但是又解决了一个问题,每次您创建类“ Visitor”的新实例时,都会遇到相同的问题。 Open the file GoogleAnalytics/Visitor.php and go to line number 150 . 打开文件GoogleAnalytics/Visitor.php并转到第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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM