Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geolocation tracking by adding proper public ip address #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

afijal
Copy link
Contributor

@afijal afijal commented Jun 22, 2023

As far as I know, Flutter has no way of getting the public IP address of the device, which is needed for geolocation to work (map in plausible dashboard).
I used the most common API for that.

@afijal afijal mentioned this pull request Jun 22, 2023
@bostrot
Copy link
Owner

bostrot commented Jun 22, 2023

The IP is usually coming from the server so it can just stay at the loopback address (127.0.0.1).

In my production system this works without problems.

@afijal
Copy link
Contributor Author

afijal commented Jun 23, 2023

Sorry but I'm really bad at networking, IPs and stuff like that, can you elaborate?
I think you mean it works well with self-hosted Plausible?

@bostrot
Copy link
Owner

bostrot commented Jun 23, 2023

So as I understand how Plausible works you don't need to tell Plausible your IP as the server itself takes it.

@afijal
Copy link
Contributor Author

afijal commented Jun 23, 2023

This is a part of my conversation with Uku Taht
Screenshot 2023-06-23 at 11 52 30
I asked him to join this discussion, maybe he will

@bostrot
Copy link
Owner

bostrot commented Jun 23, 2023

What he says makes sense as 127.0.0.1 is added to the x-forwarded-for header, and the real IP would be after that somewhere in the list. But at the same time, it works on my system, so I thought plausible is doing something to remove the loopback from the list. I guess that is not the case (https://github.com/search?q=repo%3Aplausible%2Fanalytics%20X-Forwarded-For&type=code). Still wondering why it works on my system, though, then. Maybe 127.0.0.1 is removed by my reverse proxy setup 🤔

image

Returning to your PR, I don't feel comfortable using only one service to get the IP. Maybe adding a fallback or something (e.g. ifconfig.co).

@afijal
Copy link
Contributor Author

afijal commented Jun 23, 2023

Yeah, on my system, where I just use your plugin, it does not work without my changes. The country list and map are empty, unfortunately.
Yeah, good idea, will add it then when I have a chance.

@Pablo-Aldana
Copy link

Hi!
I'm testing the plugin on the simulator and definitely, it does not send any IP and does not appear in the map. Checking plausible docs, if not setup it will take client IP so I guess it would worth to try not setting up this header or make it optional.

X-Forwarded-For optional

Used to explicitly set the IP address of the client. If not set, the remote IP of the sender will automatically be used. Depending on your use-case:

If sending the event from your visitors' device, this header does not need to be set
If sending the event from a backend server or proxy, make sure to override this header with the correct IP address of the client.
The raw value of the IP address is not stored in our database. The IP address is used to calculate the user_id which identifies a [unique visitor](https://plausible.io/data-policy#how-we-count-unique-users-without-cookies) in Plausible. It is also used to fill the Location report with country, region and city data of the visitor.

If the header contains a comma-separated list (as it should if the request is sent through a chain of proxies), then the first valid IP address from the list is used. Both IPv4 and IPv6 addresses are supported. More information about the header format can be found on [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For).

https://plausible.io/docs/events-api

@bostrot
Copy link
Owner

bostrot commented Feb 3, 2024

Hi!
I'm testing the plugin on the simulator and definitely, it does not send any IP and does not appear in the map. Checking plausible docs, if not setup it will take client IP so I guess it would worth to try not setting up this header or make it optional.

X-Forwarded-For optional

Used to explicitly set the IP address of the client. If not set, the remote IP of the sender will automatically be used. Depending on your use-case:

If sending the event from your visitors' device, this header does not need to be set
If sending the event from a backend server or proxy, make sure to override this header with the correct IP address of the client.
The raw value of the IP address is not stored in our database. The IP address is used to calculate the user_id which identifies a [unique visitor](https://plausible.io/data-policy#how-we-count-unique-users-without-cookies) in Plausible. It is also used to fill the Location report with country, region and city data of the visitor.

If the header contains a comma-separated list (as it should if the request is sent through a chain of proxies), then the first valid IP address from the list is used. Both IPv4 and IPv6 addresses are supported. More information about the header format can be found on [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For).

https://plausible.io/docs/events-api

Do you have a reverse proxy in front of your Plausible instance?

@Pablo-Aldana
Copy link

Hi!
I'm testing the plugin on the simulator and definitely, it does not send any IP and does not appear in the map. Checking plausible docs, if not setup it will take client IP so I guess it would worth to try not setting up this header or make it optional.

X-Forwarded-For optional

Used to explicitly set the IP address of the client. If not set, the remote IP of the sender will automatically be used. Depending on your use-case:

If sending the event from your visitors' device, this header does not need to be set
If sending the event from a backend server or proxy, make sure to override this header with the correct IP address of the client.
The raw value of the IP address is not stored in our database. The IP address is used to calculate the user_id which identifies a [unique visitor](https://plausible.io/data-policy#how-we-count-unique-users-without-cookies) in Plausible. It is also used to fill the Location report with country, region and city data of the visitor.

If the header contains a comma-separated list (as it should if the request is sent through a chain of proxies), then the first valid IP address from the list is used. Both IPv4 and IPv6 addresses are supported. More information about the header format can be found on [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For).

https://plausible.io/docs/events-api

Do you have a reverse proxy in front of your Plausible instance?

Yes of course, I'm using nginx and plausible installed over docker. I tried your code by downloading the git and commenting the header and it works like charm, if you have configured the Maxmind it also shows the cities.

Actually I read in a different section if you want to properly identify unique users, this header should not be set as it is intended only to be used by backend services

@Pablo-Aldana
Copy link

In case it is useful my code is as follow

 try {
      HttpClient client = HttpClient();
      HttpClientRequest request =
          await client.postUrl(Uri.parse(serverUrl + '/api/event'));
      request.headers.set('User-Agent', userAgent);
      request.headers.set('Content-Type', 'application/json; charset=utf-8');
      //request.headers.set('X-Forwarded-For', '127.0.0.1');
      Object body = {
        "domain": domain,
        "name": name,
        "url": page,
        "referrer": referrer,
        "screen_width": screenWidth,
        "props": props,
      };

I could create a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants