mysociety / fixmyblock-wordpress

1 stars 1 forks source link

Let WP Statistics access real IP address for (anonymous) visitor/session tracking? #34

Closed zarino closed 4 years ago

zarino commented 4 years ago

WP Statistics can use visitor IP addresses to tie together hits from a single visitor across a session (even if it doesn’t actually store the IP address after that).

It can get the visitor’s IP address from any HTTP header accessible to PHP, eg: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, HTTP_X_REAL_IP.

We’re running the staging site inside a docker container and behind a load-balancer. Our current setup doesn’t expose the user’s IP address neatly in any of the headers – HTTP_X_REAL_IP is the address of the load balancer, and HTTP_X_FORWARDED_FOR is a comma-separated list of IPs.

If we want to get Visitor data working in WP Statistics, we should either:

  1. Fix HTTP_X_REAL_IP so that it returns the user’s IP.

  2. Define our own wp_statistics_sanitize_user_ip filter, in the WordPress theme, which extracts the user’s IP address out of the HTTP_X_FORWARDED_FOR header. Example here.

No huge rush on this – we can still track individual page views ("Hits"). Combining them into sessions was only a nice-to-have, given the limitations around cookie-less tracking.

zarino commented 4 years ago

We’re now live on the 34SP hosting, without the load balancer, so WP Statistics is able to track visitor numbers:

Screenshot 2020-08-20 at 12 03 02