在Nginx反向代理后面的网站中注入Google Analytics(分析)跟踪代码段

时间:2018-08-02 19:19:12

标签: nginx google-analytics centos reverse-proxy javascript-injection

我一直想使用Google Analytics(分析)已有一段时间了,我想避免将跟踪代码片段手动插入每个网页中。此外,第三方应用程序(例如Plex,Deluge等)甚至可能不支持这样做。

我将所有这些服务托管在Nginx反向代理后面。我知道可以结合使用Locationngx_http_sub_module指令,将Google Analytics(分析)跟踪代码片段注入每个sub_filter块中。

在过去的几个小时内,我一直在试图找出方法,但在进行几种不同的配置后却失败了。基本上,现在我已经达到了三个不同的时间,我的配置将通过一次lint测试,并且我可以成功启动Nginx服务,但是尽管Nginx可以按预期运行,但仍未向Google Analytics(分析)提供任何指标。

有人有什么想法吗?是否需要转发端口或使用Google Analytics(分析)的任何工具?目前,所有外发请求均未过滤,这是值得的。这是到目前为止我尝试过的配置:

1)全局站点标记:

http {
    server  {
        listen  443 ssl;
        server_name www.website.com;
        ssl  on;
        location  / {
            proxy_pass http://12.34.56.78:2000/;

            sub_filter   </head>
                    "<script>
                        <!-- Global site tag (gtag.js) - Google Analytics -->
                        <script async src='https://www.googletagmanager.com/gtag/js?id=UA-##########-1'></script>
                        <script>
                          window.dataLayer = window.dataLayer || [];
                          function gtag(){dataLayer.push(arguments);}
                          gtag('js', new Date());

                      gtag('config', 'UA-##########-1');
                    </script>
                </script>";
            sub_filter_once on;
    }
}

2)Analytics.js:

http {
    server  {
        listen  443 ssl;
        server_name www.website.com;
        ssl  on;
        location  / {
            proxy_pass http://12.34.56.78:2000/;

            sub_filter </head> '<script>(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,"script","https://www.google-analytics.com/analytics.js","ga");ga("create","UA-##########","auto");ga("send","pageview");</script></head>';        

            sub_filter_once on;
    }
}

3)配置中未嵌入JS代码段的Analytics.js:

http {
    server  {
        listen  443 ssl;
        server_name www.website.com;
        ssl  on;
        location  / {
            proxy_pass http://12.34.56.78:2000/;

            sub_filter  </head>
            '<script language="javascript" src="/etc/nginx/analytics.js"></script></head>';
            sub_filter_once on;
    }
}
上面引用的

analytics.js文件:

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-##########', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

系统信息:

操作系统:CentOS 7.5
Nginx版本:1.15.2
包含的模块:http_ssl_modulestreamhttp_stub_status_modulehttp_sub_module

我消耗的资源:

抱歉,这些不是超链接。 StackOverflow表示我的超链接未“正确格式化为代码”,因为拒绝让我发表此帖子。将它们格式化为代码会破坏超链接语法,所以我必须这样做。...

1)GitHub Gist概念验证:https://gist.github.com/jirutka/5279057

2)博客文章概念证明:https://adarrohn.com/blog/nginx-google-analytics

3)关于Ruby论坛的问题:https://www.ruby-forum.com/topic/1985946

4)gtag.js上的Google Analytics(分析)文档:https://developers.google.com/analytics/devguides/collection/gtagjs/

5)analytics.js上的Google Analytics(分析)文档:https://developers.google.com/analytics/devguides/collection/analyticsjs/

6)http_sub_module上的Nginx文档:https://nginx.org/en/docs/http/ngx_http_sub_module.html

1 个答案:

答案 0 :(得分:1)

这就是我的工作方式

sub_filter '</body>' '<script src="/tealeaf/file.js" type="text/javascript"></script>\r\n</body>';

即在一排。

如今,人们并没有在每个网站页面上添加GA代码。我建议开始使用GTM,并在每页上插入GTM代码段(使用相同的方法)。这样,您将无需更改跟踪代码就可以自定义数据收集。

需要检查的地方,您不是在此处提供有问题的网站网址,而是  -请加载页面,并确保在代码之前包含GA代码段

  • 请在浏览器中打开开发人员工具,切换到“网络”标签,然后单击cntrl + f5(硬刷新)页面。比看看是否从Google服务器加载了文件analytics.js

  • 如果是,请查看是否向/ collect谷歌分析端点发出了请求。 如果是这样,您应该会在GA中看到数据。

如果以上都不是,我将查看nginx的proxy_pass位置是否支持sub_flter

于2018年8月8日编辑 There is a problem with JS file

enter image description here