php 使用 API 服务器端向 Google Analytics 发送事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32200016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send event to Google Analytics using API server sided
提问by aokozlov
I have a website where I send events to Google Analytics using javascript function:
我有一个网站,我使用 javascript 函数将事件发送到 Google Analytics:
ga('send', 'event', 'showphone', 'feedback', 'result');
ga('send', 'event', 'showphone', 'feedback', 'result');
However I also need to send some similar events from server-side
using PHP. I tried this quick start tutorial: Hello Analytics API: PHP quickstart for service accountsand reporting works like a charm, but I have no idea how to send event.
但是,我还需要server-side
使用 PHP发送一些类似的事件。我尝试了这个快速入门教程:Hello Analytics API:服务帐户和报告的PHP 快速入门就像一个魅力,但我不知道如何发送事件。
Could you please show me step-by-step what I should code to send exactly same event like mentioned above.
能否请您逐步向我展示我应该编写什么代码来发送与上述完全相同的事件。
回答by DaImTo
Hello Analytics API: PHP quickstart for service accounts is not going to help you at all. That code uses the core reporting API the core reporting API is for requesting data fromGoogle Analytics not sending data toGoogle Analytics.
Hello Analytics API:服务帐户的 PHP 快速入门根本不会帮助您。该代码使用核心报告 API,核心报告 API 用于从Google Analytics请求数据,而不是将数据发送到Google Analytics。
To send data to Google Analytics we use the Measurement Protocol. The measurement protocol is used to send information to Google analytics the JS snippet you posted also uses the measurement protocol.
要将数据发送到 Google Analytics,我们使用测量协议。测量协议用于向 Google Analytics 发送信息,您发布的 JS 代码段也使用测量协议。
You can use the measurement protocol from any language that supports HTTP post or Http Get. That being said there is no PHP specific library for sending information to Google analytics you are going to have to format your post yourself. A tip would be to use Validating hitsto check it before you send it to Google while you are developing this.
您可以使用支持 HTTP post 或 Http Get 的任何语言的测量协议。话虽如此,但没有特定于 PHP 的库可用于将信息发送到 Google 分析,您将不得不自己格式化您的帖子。一个提示是在您开发它时使用验证命中来检查它,然后再将其发送给 Google。
It will probably look something like this
它可能看起来像这样
http://www.google-analytics.com/collect?v=1&tid=UA-XXX-Y&cid=35009a79-1a05-49d7-b876-2b884d0f825b&an=My%20Awesom%20APP&aid=com.daimto.awesom.app&av=1.0.0&aiid=come.daimto.awesom.installer &t=event&ec=list&ea=accounts&userclicked&ev=10
回答by Adarsh Madrecha
There is a PHP library php-ga-measurement-protocolby theiconic
on github which can be used to send data using Measurement Protocal.
有一个PHP库PHP-GA-测量协议通过theiconic
在github其可以被用于发送数据使用测量Protocal。
use TheIconic\Tracking\GoogleAnalytics\Analytics;
// Instantiate the Analytics object
// optionally pass TRUE in the constructor if you want to connect using HTTPS
$analytics = new Analytics(true);
// Build the GA hit using the Analytics class methods
// they should Autocomplete if you use a PHP IDE
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-26293728-11')
->setClientId('12345678')
->setDocumentPath('/mypage')
->setIpOverride("202.126.106.175");
// When you finish bulding the payload send a hit (such as an pageview or event)
$analytics->sendPageview();