如果您已经升级到通用分析(新的Google Analytics),则可以直接从PHP生成事件或页面浏览量。
Google创建了
测量协议,它基本上是一组带有一些参数的http请求,包含您想要跟踪的内容。我将在此处粘贴我在自己网站上使用以跟踪下载的代码。
要跟踪任何内容,您只需使用自己的参数调用AnalyticsDoHit()即可。
function AnalyticsDoHit($tid, $slug, $title)
{
$v = 1;
$cid = ParseOrCreateAnalyticsCookie();
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'pageview',
'dt' => $title,
'dp' => $slug
);
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
file_get_contents($getString);
}
function ParseOrCreateAnalyticsCookie()
{
if (isset($_COOKIE['_ga']))
{
list($version, $domainDepth, $cid1, $cid2) = preg_split('[\.]', $_COOKIE["_ga"], 4);
$contents = array(
'version' => $version,
'domainDepth' => $domainDepth,
'cid' => $cid1 . '.' . $cid2
);
$cid = $contents['cid'];
}
else
{
$cid1 = mt_rand(0, 2147483647);
$cid2 = mt_rand(0, 2147483647);
$cid = $cid1 . '.' . $cid2;
setcookie('_ga', 'GA1.2.' . $cid, time() + 60 * 60 * 24 * 365 * 2, '/');
}
return $cid;
}
使用方法如下:
AnalyticsDoHit("UA-XXXXXX-X", "http://www.AutomatedEmailParser.com/EmailAndParser_setup.msi", "EmailAndParser_setup.msi");