Show the Piwik data in a php page!

Hi,
I’m new at Piwik, and I’d like to know if I can display the piwik data, such as unique page views for a specific page, in the php pages of my website for my users.
For example, in my website I have a page callled “information”, and in this page I’d like to have a field where is shown “Number of visits: ?”. Can it be done using the API? And if yes, how can I do it?
I already tried these methods (Querying the Reporting API: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3), but but it didn’t worked. Please, some one can help me?
I don’t know if i’m doing something really wrong.

Please, someone can help with this? It’s really important!!

In what way didn’t it work?

I tried the “Call the Piwik API in PHP” way. I made a test page to get the piwik data, using the technique shown in the page, but it didn’t work. Was just presented a blank page, with no data or any error massege.

Here is the code I used:

<?php

use Piwik\API\Request;
use Piwik\FrontController;

define(‘PIWIK_INCLUDE_PATH’, realpath(’’));
define(‘PIWIK_USER_PATH’, realpath(’’));
define(‘PIWIK_ENABLE_DISPATCH’, false);
define(‘PIWIK_ENABLE_ERROR_HANDLER’, false);
define(‘PIWIK_ENABLE_SESSION_START’, false);

require_once PIWIK_INCLUDE_PATH . “/index.php”;
require_once PIWIK_INCLUDE_PATH . “/core/API/Request.php”;

$environment = new \Piwik\Application\Environment(null);
$environment->init();

FrontController::getInstance()->init();

// This inits the API Request with the specified parameters
$request = new Request(‘
module=API
&method=Resolution.getResolution
&idSite=7
&date=yesterday
&period=week
&format=XML
&filter_limit=3
&token_auth=anonymous
’);
// Calls the API and fetch XML data back
$result = $request->process();
echo $result;

Where is your Piwik installation in relation to your code? In my case my Piwik installation is in an analytics directory at the same level as my code file. For this I have:

define('PIWIK_INCLUDE_PATH', realpath('analytics'));

Also, I had to change to

&idSite=1

As my site ID is 1.

Finally I had to change my token_auth from anonymous to the token for my user from the Manage Users admin page as I don’t allow unauthorized users.

Once I did all of this I got the response as shown on that page. If you still don’t see a response, turn on error reporting for your PHP and check the PHP error log for any errors.

Thanks for the help Valdhor.
I did all the changes you said.
My Piwik instalation is in the same level as my code file too, in a Piwik directory.
I also changed the site ID, once mine is 1 too.
And then I changed the token_auth from anonymous to my own.
And after all this, I got the follow erro messages:

Warning: require_once(/core/bootstrap.php): failed to open stream: No such file or directory in /home/u340084269/public_html/piwik/index.php on line 21

Fatal error: require_once(): Failed opening required ‘/core/bootstrap.php’ (include_path=’.:/opt/php-5.5/pear’) in /home/u340084269/public_html/piwik/index.php on line 21

Can you post the updated code?

Also, what version of PHP and Piwik do you have?

What is the documentroot of your Apache installation?

The updated code is:

<?php use Piwik\API\Request; use Piwik\FrontController; define('PIWIK_INCLUDE_PATH', realpath('piwik')); define('PIWIK_USER_PATH', realpath('piwik')); define('PIWIK_ENABLE_DISPATCH', false); define('PIWIK_ENABLE_ERROR_HANDLER', false); define('PIWIK_ENABLE_SESSION_START', false); require_once PIWIK_INCLUDE_PATH . "index.php"; require_once PIWIK_INCLUDE_PATH . "core/API/Request.php"; $environment = new \Piwik\Application\Environment(null); $environment->init(); FrontController::getInstance()->init(); // This inits the API Request with the specified parameters $request = new Request(' module=API &method=Resolution.getResolution &idSite=1 &date=yesterday &period=week &format=XML &filter_limit=3 &token_auth=ab*************************************b '); // Calls the API and fetch XML data back $result = $request->process(); echo $result; The version of my php is 5.5.3 and the version of the Piwik I'm using is 2.16.1 I couldn't find the documentroot of my Apache installation.

So I know there were some major updates and changes between PHP 5.5 and 5.6. I am using 5.6.8. I would be inclined to update your PHP first.

To find the documentroot you would need to look in your httpd.conf file.

Is this on a Mac, Windows or Linux box?

I updated my php, but nothing happend, the same erro massages were showed. So I believe this is not the problem.
I can’t see the httpd.conf file because I use a shared server, and i’m not allowed to acces this kind of file.

One thing you could try is creating a phpinfo() script (Use Google to find out how) and looking for DOCUMENT_ROOT.

Hello there , Can anybody help me with the code of Piwik ,
i am using godaddy internal application to install piwik with my new website , which is in testing phase , please help me out how to install and copy the code into website for monitoring .
link to the site is here
www.funeager.com

@Valdhor, thanks again.
I tried with phpinfo(), but in the doc_root field was showed:

doc_root = no value

So I used this script:

<?php //Gets the document root $root = getenv("DOCUMENT_ROOT") ; Echo $root; ?>

and the answer was: /home/u340084269/public_html

is this my documentroot?

I believe that is your documentroot.

You may want to change your PIWIK_INCLUDE_PATH

define('PIWIK_INCLUDE_PATH', '/home/u340084269/public_html/piwik/');
define('PIWIK_USER_PATH', '/home/u340084269/public_html/piwik/');

This is the problem:

Failed opening required '/core/bootstrap.php'

As you can see it is starting from documentroot but your installation is at

/piwik/core/bootstrap.php

@Valdhor, it worked! Thanks a lot.
My question now is if there is some code that I can put in each page, that will display the namber of visits of the specific page (using the url). Cause in this way, is showing the visits of the entire site.

Excellent we are getting somewhere.

My site is a single page application (So Apache only sees one page) so I have to put some code in my router for Piwik to track visitors. The two things I want to know about are who the visitors are and what “Pages” they are visiting. Our organization requires a login so I can get the user ID and the router knows the page requested so I just add the following code to the router (currentTitle is the page Requested and userID is their login ID):

_paq.push(['setDocumentTitle', currentTitle]);
_paq.push(['setUserId', userID]);
_paq.push(['trackPageView']);

This uses the async javascript to do the tracking.