mssql
小站长之家
2020-04-09发布于“原创教程”
关注

在Nginx反向代理的的网站中插入入Google Analytics(分析)跟和代码

我一直想使用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:

  1. http {
  2. server {
  3. listen 443 ssl;
  4. server_name www.website.com;
  5. ssl on;
  6. location / {
  7. proxy_pass http://12.34.56.78:2000/;
  8. sub_filter </head>
  9. "<script>
  10. <!-- Global site tag (gtag.js) - Google Analytics -->
  11. <script async src='https://www.googletagmanager.com/gtag/js?id=UA-##########-1'></script>
  12. <script>
  13. window.dataLayer = window.dataLayer || [];
  14. function gtag(){dataLayer.push(arguments);}
  15. gtag('js', new Date());
  16. gtag('config', 'UA-##########-1');
  17. </script>
  18. </script>";
  19. sub_filter_once on;
  20. }
  21. }

2) Analytics.js:

  1. http {
  2. server {
  3. listen 443 ssl;
  4. server_name www.website.com;
  5. ssl on;
  6. location / {
  7. proxy_pass http://12.34.56.78:2000/;
  8. 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>';
  9. sub_filter_once on;
  10. }
  11. }

3) Analytics.js without JS snippet embedded in config:

  1. http {
  2. server {
  3. listen 443 ssl;
  4. server_name www.website.com;
  5. ssl on;
  6. location / {
  7. proxy_pass http://12.34.56.78:2000/;
  8. sub_filter </head>
  9. '<script language="javascript" src="/etc/nginx/analytics.js"></script></head>';
  10. sub_filter_once on;
  11. }
  12. }

analytics.js file referenced above:

  1. <!-- Google Analytics -->
  2. <script>
  3. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  4. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  5. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  6. })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  7. ga('create', 'UA-##########', 'auto');
  8. ga('send', 'pageview');
  9. </script>
  10. <!-- 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

1768
0
2
发表评论
评论 0 排序方式
查看更多