海天无影Blog

Google Analytics异步优化之Nginx后端处理(更新蜘蛛收集排除)

/ 5273阅 / 6评 / 0

10月15日更新 收集了多种搜索引擎蜘蛛进行排除,提升统计准确性,否则 Direct 的统计会严重虚高。将以下代码,增加到排除蜘蛛的代码段了,本文下方中已经同步更新。

GroupHigh|CCBot|MJ12bot|SemrushBot|bingbot|YandexBot|DotBot|AhrefsBot|msnbot|IABot|SMTBot|SEOkicks|YisouSpider

如果大家发现其他蜘蛛,欢迎评论提交,谢谢!


为了减少使用Google Analytics打开网页同步加载相关js文件造成的加载时间,很多大牛想到了多种异步加载优化的方法,之前用了php在后端加载一个curl来发送相关访问数据到Google analytics服务器的方法(具体查看Google Analytics异步优化之php后端中转),在博客使用了一段时间,各方面统计的数据都挺到位。

这次来个更后端的异步处理,直接在Nginx服务器来处理访问数据并发送给Google Analytics服务器进行统计处理。

生成访客ID

使用Google Analytics服务的网站都会生成一个_ga 的 cookie项,而同步加载的ga.js 文件则将此 cookie 中的数据处理后,将以唯一值uid作为访客依据发送给Google Analytics服务器,Google Analytics 根据 uid 进行访客行为的追踪和新旧访客的区别。

这里使用Nginx的userid模块,生成用户的信息。userid 模块会在用户访问时检查 cook­ie 中是否有 cid 项,如果没有 cid 项,则会在返回的 header 中加入 set-cook­ies 头标记这个用户,并将$uid_set 变量设定为 cid=XXXXXX 这一形式,将$uid_got 变量设定为空。如果有 cid 项,则将$uid_got 变量设定为 cid=XXXXXX 这一形式,将$uid_set 变量设定为空。

userid on;
userid_name cid;
userid_do­main 你的域名;
userid_path /;
userid_ex­pires max;

另外还需要获取客户端的语言环境

if ($http_accept_language ~* '^(.+?),') {
    set $first_language $1;
}

或者通过 map 操作从 Ac­cept-Lan­guage 中提取

map $http_ac­cept_lan­guage $lang { 
        ~^([a-zA-Z-]*) $1;
    }

建立使用 tracker location 转发至 Google Analytics

要将客户端数据在Nginx后端异步转发给Google Analytics,需要使用到Nginx的“location 内部 redirect 匹配”和“proxy”功能。

location @tracker {        
        internal;
        resolver 8.8.8.8 ipv6=off;
        proxy_method GET;
        proxy_pass https://www.google-analytics.com/collect?v=1&tid=UA-xxxxxxxx-1&$uid_set$uid_got&t=pageview&dh=$host&dp=$uri&uip=$remote_addr&dr=$http_referer&z=$msec;
        proxy_set_header User-Agent $http_user_agent;
        proxy_pass_request_headers off;
        proxy_pass_request_body off;
}

相关参数:

最后只要在网站对应访问的 location 块中加上 post_action @tracker; 就大功告成了。

这里使用的是lnmp1.6+wordpress,网站对应的访问 location 为 location ~ [^/].php(/|$){},存在enable-php.conf文件里,然后在网站的conf文件里调用。

location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;

            post_action @tracker;
         }

排除DNT、wordpress管理页等、搜索引擎蜘蛛的访问统计

为了保证Google Analytics统计的数据高效、准确,我们要将管理员使用wordpress后台的访问,以及搜索引擎抓取网站页面的访问给屏蔽掉,再加上排除部分不想被追踪的访客。

1、排除DNT

用户启用 Do Not Track 功能向网站表明自己不希望被追踪,遵守该规则的网站就不会追踪用户的个人信息。判断排除条件是:

if ($http_dnt != 1)

2、排除wordpress后台等访问

我们统计网站数据,自己的操作比如说后台管理,以及一些订阅等访问并不需要被统计,可以通过以下正则式判断排除:

if ($request_uri !~* "^/wp-|/feed/$|\.pagespeed\.|robots\.txt|favicon\.ico|replytocom") 

3、排除搜索引擎蜘蛛

这个就不需要过多解释了,我们追踪的是访客,不是搜索引擎。排除代码为:

if ($http_user_agent !~* "GroupHigh|CCBot|YisouSpider|MJ12bot|SemrushBot|bingbot|YandexBot|DotBot|AhrefsBot|msnbot|IABot|SMTBot|SEOkicks|qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|YiSou Spider")

合起来通过flag变量,一层层判断赋值,然后最后判断是否tracker location 转发至 Google Analytics。

set $flag "0";
   if ($request_uri !~* "^/wp-|/feed/$|\.pagespeed\.|robots\.txt|favicon\.ico|replytocom") {
     set $flag "${flag}1";
 }   
 
 if ($http_user_agent !~* "GroupHigh|CCBot|YisouSpider|MJ12bot|SemrushBot|bingbot|YandexBot|DotBot|AhrefsBot|msnbot|IABot|SMTBot|SEOkicks|qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|YiSou Spider") {
     set $flag "${flag}2";
 }
  
 if ($http_dnt != 1) {
     set $flag "${flag}3";
     }
  
 if ($flag = "0123") {
     post_action @tracker;
 }

将以上代码添加至网站对应的访问 location即可。

wordpress使用完整代码

userid on;
userid_name cid;
userid_domain 你的域名;
userid_path /;
userid_expires max;
if ($http_accept_language ~* '^(.+?),') {
    set $first_language $1;
}

location @tracker {
    resolver 8.8.8.8 ipv6=off;
    internal;
    proxy_method GET;
    proxy_pass https://www.google-analytics.com/collect?v=1&tid=UA-xxxxxxxx-1&$uid_set$uid_got&t=pageview&dh=$host&dp=$request_uri&uip=$remote_addr&dr=$http_referer&ul=$first_language&z=$msec;
    proxy_set_header User-Agent $http_user_agent;
    proxy_pass_request_headers off;
    proxy_pass_request_body off;
}


location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;


    set $flag "0";
   if ($request_uri !~* "^/wp-|/feed/$|\.pagespeed\.|robots\.txt|favicon\.ico|replytocom") {
     set $flag "${flag}1";
 }   
 
 if ($http_user_agent !~* "GroupHigh|CCBot|YisouSpider|MJ12bot|SemrushBot|bingbot|YandexBot|DotBot|AhrefsBot|msnbot|IABot|SMTBot|SEOkicks|qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|YiSou Spider") {
     set $flag "${flag}2";
 }
  
 if ($http_dnt != 1) {
     set $flag "${flag}3";
     }
  
 if ($flag = "0123") {
     post_action @tracker;
 }
    }

将以上代码替换网站conf文件中的

include enable-php.conf;
#lnmp1.6默认模板

或者

location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

参考文章:

Ng­inx 内配置 Google An­a­lyt­ics 指南
使用Nginx将请求转发至Google Analytics实现后端统计
Nginx 异步处理Google Analytics

《 “Google Analytics异步优化之Nginx后端处理(更新蜘蛛收集排除)” 》 有 6 条评论

  1. […] 先说明一下背景情况,之前海天为了提高博客的速度,将google analytics的统计转移到nginx服务器层面。使用一段时间发现google analytics会将一些明显应该是404的访问也统计进去,比如恶意的猜解数据库备份文件、wordpress漏洞文件等,当时以为是wordpress的404页面设置有问题,但是通过检测,发现head返回的代码的确是404错误而不是200。 […]

  2. 世纪之光说道:

    你现在做的工作和php有关?我看你花了不少时间在相关技术上。

  3. 天毅说道:

    终于成功了吗? 恭喜恭喜!

  4. […] (Nginx服务器后端处理请参考Google Analytics异步优化之Nginx后端处理) […]

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注