Reputation: 171
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 :)
Upvotes: 2
Views: 2332
Reputation: 171
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.
Upvotes: 5