Home > php教程 > php手册 > Google Analytics谷歌统计来访评论者

Google Analytics谷歌统计来访评论者

WBOY
Release: 2016-06-06 20:09:52
Original
1586 people have browsed it

代码 以下PHP代码加在Google Analysis统计代码之前 ?phpif ($_COOKIE["comment_author_" . COOKIEHASH]!="") {$fk = $_COOKIE['comment_author_'.COOKIEHASH];} elseif ($_COOKIE["wordpress_logged_in_" . COOKIEHASH]!="") {$fk = "admin";//自己} else {$

ga-reviewers (1)

代码

以下PHP代码加在Google Analysis统计代码之前

<?php if ($_COOKIE["comment_author_" . COOKIEHASH]!="") {
	$fk = $_COOKIE['comment_author_'.COOKIEHASH];
} elseif ($_COOKIE["wordpress_logged_in_" . COOKIEHASH]!="") {
	$fk = "admin";//自己
} else {
	$fk ="anonymous user";//没有评论cookies的人
}
?>
Copy after login

然后在ga统计代码里加上一行, (可以加在_gaq.push(['_trackPageview']);下面) :

_gaq.push(['_setCustomVar', 1, 'Reviewer', '<?php echo $fk;?>', 1]);
Copy after login

然后等第二天就能看到统计了, 在受众群体 – 自定义 – 自定义变量中查看, 可以看到如下图所示:

ga-reviewers (3)

点进去就可以看到效果, 如开头的图所示

Google Analytics自定义变量参数详解

Google Analysis自定义变量(CustomVariables)有4个自定义参数:

_gaq.push(['_setCustomVar', 'index', 'name', 'value', 'opt_scope']);
Copy after login
  1. index — 必选, 自定义变量的键值, 可选范围 1 – 5,
  2. name — 必选, 填自定义变量的名称, 如”Reviewer” “评论者”
  3. value — 必选, 变量的值也就是统计到的数据
  4. opt_scope — 可选, 代表变量统计的类型/级别, 有3个参数: 1 (visitor-level 访客级别), 2 (session-level 访问级别), or 3 (page-level 页面级别).

参考文档: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables?hl=zh-CN

扩展

以此类推还可以统计访客IP, 因为谷歌统计默认不会统计具体IP
PHP部分:

if(!empty($_SERVER['REMOTE_ADDR'])){ $ip = $_SERVER['REMOTE_ADDR']; } else { $ip = "null"; }
Copy after login

GA代码部分

_gaq.push(['_setCustomVar', 2, 'IP', '<?php echo $ip;?>', 1]);
Copy after login

如图:

ga-reviewers (2)

收工, 匿了~
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template