我一直想使用Google Analytics(分析)已有一段時間了,我想避免將跟蹤代碼片段手動插入每个網頁中.此外,第三方應用程式(例如Plex,Deluge等)甚至可能不支援這樣做。
无法插入请加入
proxy_set_header Accept-Encoding
""
我將所有這些服務託管在Nginx反向代理後面.我了解可以將Google Analytics(分析)跟蹤代碼段註入每个 Location
使用ngx_http_sub_module
阻止 与 sub_filter
結合 指示.
在過去的几个小時內,我一直在試圖找出方法,但在进行几種不同的配置後却失败了.基本上,現在我已经達到了三个不同的時間,我的配置將通過一次lint測試,並且我可以成功啟動Nginx服務,但是尽管Nginx可以按預期執行,但仍未向Google Analytics(分析)提供任何指標.
有人有什麼想法吗? 是否需要轉發埠或使用Google Analytics(分析)的任何工具? 目前,所有外發請求均未過濾,這是值得的.這是到目前為止我尝試過的配置:
1) Global Site Tag:
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) Analytics.js without JS snippet embedded in config:
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 file referenced above:
<!-- 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 -->
System Information:
操作系統:CentOS 7.5
Nginx版本:1.15.2
包含的模組: http_ssl_module
,威兹威兹 ,威兹威兹 ,威兹威兹
stream
抱歉,這些不是超鏈接. CoDebug表示我的超鏈接未"正確格式化為代碼",因為拒绝让我發表此帖子.將它们格式化為代碼会破壞超鏈接語法,所以我必须這樣做。...
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) http_stub_status_module
上的Google Analytics(分析)文件 :https://developers.google.com/analytics/devguides/collection/gtagjs/
5) http_sub_module
上的Google Analytics(分析)文件 :https://developers.google.com/analytics/devguides/collection/analyticsjs/
6) Sources I have consumed:上的Nginx文件 :https://nginx.org/en/docs/http/ngx_http_sub_module.html
gtag.js