Google Analytics(分析4)与Nginx反向代理

发布于 2025-02-09 06:52:24 字数 3355 浏览 3 评论 0 原文

我有一个主要是工作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&gtm=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&gtm=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&gtm=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&gtm=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 参数被设置为访问者的真实IP地址,而不是我的ALB的10.1.x IP。

我的GA4事件和页面浏览量如预期的那样触发 - 但地理位置显示每个人都来自Ashburn。

我缺少/不了解什么?

我应该注意,为了表演,我的Google Analytics(我的Google Analytics)js库与我的WebApp一起加载,并且不包括在此代理业务中(尽管已编辑以指向所有Google Analytics(Google Analytics)API呼叫MyDomain.com/workaround-ga4/)。

任何帮助都非常感谢:)

I have a mostly working nginx reverse proxy to handle interfacing with ga4.
The problem I have, is that the geolocation data of visitors to my site all comes from ashburn virginia (where my alb lives).

Here is my nginx config with site-specific data edited:

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;
        }
    }
}

I can see in my error_log which catches and debugs the nginx rewrites, the following two entries:

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/"

and

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/"

Which to me suggests that the rewrite is working as intended - the uip parameter is being set to the real ip address of the visitor, and not the 10.1.x ip of my albs.

my ga4 events and page views are firing as expected - but geolocation shows everyone as coming from ashburn.

what am I missing/not understanding?

I should note that for performances' sake my google analytics js library is loaded locally with my webapp, and not included in this proxy business (though has been edited to point all google analytics api calls to mydomain.com/workaround-ga4/).

Any help is greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2025-02-16 06:52:24

好的。事实证明,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 my proxy_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文