Google Analytics(分析4)与Nginx反向代理
我有一个主要是工作nginx反向代理来处理与GA4的接口。 我遇到的问题是,我网站的访问者的地理分配数据全部来自Ashburn Virginia(我的Alb Lives)。
这是我的nginx配置,并编辑了特定于网站的数据:
events {
worker_connections 4096;
}
http {
set_real_ip_from 10.1.20.0/24;
set_real_ip_from 10.1.30.0/24;
real_ip_header X-Forwarded-For;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referrer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log notice;
rewrite_log on;
server {
listen 80;
server_name _;
client_max_body_size 500M;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:8000;
}
location /workaround-ga4/ {
resolver 8.8.8.8;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /workaround-ga4/([^/]+) /g/collect?$args&uip=$remote_addr break;
proxy_pass https://www.google-analytics.com;
}
}
}
我可以在我的error_log中看到并调试nginx重写,以下两个条目:
2022/06/21 22:03:36 [notice] 528#528: *1165 "/workaround-ga4/([^/]+)" matches "/workaround-ga4/g/collect", client: 555.555.555.555, server: _, request: "POST /workaround-ga4/g/collect?v=2&tid=G-MYGOOGLEANALYTICSCODE>m=555555&_p=555555555&_z=55555&cid=555555555.5555555555&ul=en-us&sr=5555x555&sid=5555555555&sct=55&seg=5&dl=https%3A%2F%2Fmy.site.com%2F&dt=my%20site%20which%20%7C%20is%20having%20trouble%20with%20googleanalytics&_s=1 HTTP/1.1", host: "my-site.com", referrer: "https://my-site.com/"
对
2022/06/21 22:03:36 [notice] 528#528: *1165 rewritten data: "/g/collect", args: "v=2&tid=G-MYGOOGLEANALYTICSCODE>m=555555&_p=555555555&_z=5555555&cid=555555555.5555555555&ul=en-us&sr=5555x555&sid=5555555555&sct=55&seg=5&dl=https%3A%2F%2Fmy-site.com%2F&dt=my%20site%20is%20%7C%20having%20problems%20with%20google%20analytics&_s=1&uip=my.real.ip/&v=2&tid=G-MYGOOGLEANALYTICSCODE>m=555555&_p=5555555555&_z=555555&cid=5555555555.5555555555&ul=en-us&sr=5555x555&sid=5555555555&sct=55&seg=5&dl=https%3A%2F%2Fmy-site.com%2F&dt=my%20site%20is%20%7C%20having%20trouble%20with%20google%20analytics&_s=1", client: my.actual.ip, server: _, request: "POST /workaround-ga4/g/collect?v=2&tid=G-MYGOOGLECODE>m=555555&_p=5555555555&_z=555555&cid=5555555555.5555555555&ul=en-us&sr=5555x555&sid=5555555555&sct=55&seg=5&dl=https%3A%2F%2Fmy-site.com%2F&dt=my%20site%20is%20%7C%20having%20trouble%20with%20google%20analytics&_s=1 HTTP/1.1", host: "my-site.com", referrer: "https://my-site.com/"
我而言,这表明重写是按预期工作的 - uip
我的GA4事件和页面浏览量如预期的那样触发 - 但地理位置显示每个人都来自Ashburn。
我缺少/不了解什么?
我应该注意,为了表演,我的Google Analytics(我的Google Analytics)js库与我的WebApp一起加载,并且不包括在此代理业务中(尽管已编辑以指向所有Google Analytics(Google Analytics)API呼叫MyDomain.com/workaround-ga4/)。
任何帮助都非常感谢:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。事实证明,GA4尚未正式支持URL中的
uip
参数。但是,Google前缀alpha/beta URL参数带有
_
。更改& uip = $ remote_Addr
在我的proxy_pass
中更改为& _UIP = $ remote_addr
做到了这一点。**请注意,这是一个临时解决方案,如果/Google决定正式支持
uip
参数,或者以其他方式决定更改API,则将中断。OK. Turns out GA4 does not yet officially support the
uip
parameter in the URL.However, Google prefixes alpha/beta URL parameters with
_
. Changing&uip=$remote_addr
in myproxy_pass
to&_uip=$remote_addr
did the trick.**Note this is a temporary solution, and will break if/once Google decides to officially support the
uip
parameter, or otherwise decides to change the API.