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

Using doTrackPageView() with the PiwikTracker PHP API records server IP instead of client IP. #10920

Closed
mahammer opened this issue Nov 29, 2016 · 6 comments
Labels
answered For when a question was asked and we referred to forum or answered it.

Comments

@mahammer
Copy link

I am new to piwik so I am not sure if this is a bug, issue or feature.

  • I require once the PiwikTracker.php
  • Set the PiwikTracker::$URL
  • create a new PiwikTracker object for my idSite
  • call ->doTrackPageView()

However piwik records the server IP instead of the visitors IP. I am aware that I could use ->setIP() but then I need to auth() for what I read.
Is recording the server IP a bug or intended?

@mahammer
Copy link
Author

mahammer commented Nov 30, 2016

I've checked the classes source and my PiwikTracker object $tracker.
$tracker->ip == $_SERVER['REMOTE_ADDR']
So why would $tracker->doTrackPageView('foo') record with $_SERVER['SERVER_ADDR']?

@mattab
Copy link
Member

mattab commented Dec 6, 2016

REMOTE_ADDR is typically set to the client address. if not, it may be you are using proxies or other configuration where IP is set in another header. you can see which IP to use by looking at the output of var_dump($_SERVER);

@mattab mattab closed this as completed Dec 6, 2016
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label Dec 6, 2016
@mahammer
Copy link
Author

mahammer commented Dec 6, 2016

You seem to have misunderstood.
On my server $_SERVER['REMOTE_ADDR'] is the client IP, just like it should be.
Also the PiwikTracker->ip holds the client IP, just like it should do.
However, when calling PiwikTracker->doTrackPageView() the Server IP is recorded by Piwik instead of the client IP.

I also tinkered with the proxy settings in the Piwik config like your issue resolving readme suggests, nothing changed.

I also used setTokenAuth() and implicitly used setIp() to write the client IP into the PiwikTracker object. On calling doTrackPageView it again recorded the server IP

@mattab
Copy link
Member

mattab commented Dec 6, 2016

it should definitely work if you call setTokenAuth and pass a token with admin access to the given website in piwik. If it still does not for you, you can debug the tracking api requests see here: http://developer.piwik.org/api-reference/tracking-api#debugging-the-tracker

@mahammer
Copy link
Author

mahammer commented Dec 6, 2016

I found the issue, I leave this here for future reference.
Your documentation is lacking or I misunderstood/overread something.
Like it said here [1] I did the following:

<?php
  require_once "lib/PiwikTracker/PiwikTracker.php";
  PiwikTracker::$URL="http://foo.bar.com/";
  $tracker = new PiwikTracker(1);
  $tracker->doTrackPageView("foo.dev");
?>

Almost everything worked like intended, $_SERVER['REMOTE_ADDR'] and the PiwikTracker holding the client IP, but Piwik recorded the server IP.

The help on this suggested to find the client IP in your $_SERVER' and send it manually. So I did. The documentation says that setIp()needssetTokenAuth()` thus I tried:

<?php
  require_once "lib/PiwikTracker/PiwikTracker.php";
  PiwikTracker::$URL="http://foo.bar.com/";
  $tracker = new PiwikTracker(1);
  $tracker->setTokenAuth("XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx");
  $tracker->setIp($_SERVER['REMOTE_ADDR']);
  $tracker->doTrackPageView("foo.dev");
?>

This resulted again in Piwik recording the server IP. Which is odd, given the next step and solution.

Then I went back to the first code without setTokenAuth() and enabled debugging and saw that Piwik reports that the request lacks the rights to record CID, which equaled the client IP.

So, lastly I did this:

<?php
  require_once "lib/PiwikTracker/PiwikTracker.php";
  PiwikTracker::$URL="http://foo.bar.com/";
  $tracker = new PiwikTracker(1);
  $tracker->setTokenAuth("XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx");
  $tracker->doTrackPageView("foo.dev");
?>

This now properly records the client IP in Piwik. Unfortunately the documentation in [1] nowhere states that setTokenAuth() is required for this to work. Alternatively there's a bug that requires regular tracking to have admin rights.


[1] https://piwik.org/docs/tracking-api/#tracking-api-reference

@mattab
Copy link
Member

mattab commented Dec 6, 2016

Thanks for the details @mahammer - I've added a call to setTokenAuth in the guide you linked. if you have other suggestions to improve our docs please let us know 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it.
Projects
None yet
Development

No branches or pull requests

2 participants