[SOLVED] PHP Piwik Tracker issue

Hi there,

I am trying to understand how the Piwik PHP tracking is working.
In order to do that I installed Piwik locally under :
http://localhost/piwik/

I followed the documentation at Tracking API - Analytics Platform - Matomo but I cannot make it work.

Any ideas of where it could come from?

Thanks for your help, i added below the source code that I am using, copied/pasted from the official documentation :

<!doctype html>

My title <?php // -- Piwik Tracking API init -- require_once "/piwik/PiwikTracker.php"; PiwikTracker::$URL = 'http://localhost/'; ?> <?php // Example 1: Tracks a pageview for Website id = {$IDSITE} echo ' Page titles')) . '" alt="" />'; ?>

You can’t really use localhost unless you are running both your server and browser on the same machine which will then break PHP.

The easiest way I’ve found is to modify your local hosts file to create a testbed. for example you could create a hostname of www.mypiwiktesting.com and point it to 127.0.0.1.

Hi,

thanks for your answer, so if I get it right, I need to go to my domain name provider, buy a domain name such as www.mypiwiktest.com and modify the DNS in order for it to point to 127.0.0.1, am I right?

Best regards,

As I said, just for testing you can create your own domain and put it into your computers hosts file. Your computer will look at that first before it goes out to a DNS server to look it up. Take a look at http://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/ for details. This is for Windows but can be used for any other computer as well (The location of the hosts file will be different).

Hi Valdhor,

first of all, thanks I succeeded in creating www.mypiwiktesting.com (I did not know we could do that locally :slight_smile:
I tried again to track one of my page with the following code (but nothing is happening, any ideas?) :

<?php 
// -- Piwik Tracking API init -- 
 require_once "PiwikTracker.php";
PiwikTracker::$URL = 'http://www.mypiwiktesting.com/piwik/';
?>
<?php 
 echo '<img src="'. str_replace("&","&amp;", Piwik_getUrlTrackPageView( $idSite = 1, $customTitle = 'This title will appear in the report Actions > Page titles')) . '" alt="" />'
?>

I don’t use the PHP Tracking client so all I can really do is point you at the documentation.

I use the javascript tracking client. This gives you a lot more data points that you can track. Is there any reason you can’t use the JS client?

Thanks Valdhor once more for your help. Well I am currently writing a book about Piwik and I would like to test all his functionalities by providing tutorials to the users. I am testing all functionalitites before showing them to the readers. That’s why I wanted to test it by myself first.
I am going to insert the code on a real server in order to see if it works or not.

Thanks for your help

Great I finally succeeded in doing it !!!
How did I solve it?
Well mainly by debugging the PHP code.
I guess that out of the mistakes I made :

  • the path to the require_once was the wrong one
  • the $URL was as well the wrong one, I needed to insert the domain name+the repository
  • the url of the image file was wrong as well, I debugged it with Firebug

I can now start to play with Piwik tracking code in PHP :slight_smile: