The standard way of recording data in Piwik is to use the Javascript Tracking tag. This works well for most users where pasting a Javascript code in the page footer is not an issue.
There are other frequent cases however where Javascript can not be used: Ebay pages, MySpace pages, but also Software Apps, iPhone or Android apps, mobile websites, etc.
In these cases, you can use alternative ways to record visitors, visits, pages and Goal conversions in Piwik:
We will now look at each of these methods to record data in Piwik.
In the following, replace {$IDSITE} with your Piwik website ID, and replace http://demo.piwik.org/ with your Piwik URL.
Some websites like MySpace or eBay will not allow users to add Javascript to their profile but accept HTML. In this case, you can still track visits with
Piwik using the Image Tracker.
Note: the code doesn't use Javascript so Piwik will not be able to track some user information
such as search keywords, referrers, screen resolutions, browser plugins and page titles.
<!-- Piwik Image Tracker -->
<img src="http://demo.piwik.org/piwik.php?idsite={$IDSITE}&rec=1" style="border:0" alt="" />
<!-- End Piwik -->
$_SERVER['HTTP_REFERER']
The Piwik Tracking API allows to trigger visits (page views and Goal conversions) from any environment (Desktop App, iPhone or Android app, Mobile website, etc.).
We currently provide a PHP client to call the API from your PHP projects. If you would like to contribute a version of the client in another programming language (Python, Java, Ruby, Perl, etc.) please create a ticket in our developer area (please attach the client code to the ticket).
Follow these instructions to get started with the Tracking API:
<?php
// -- Piwik Tracking API init --
require_once "/path/to/PiwikTracker.php";
PiwikTracker::$URL = 'http://demo.piwik.org/';
?>
The client is used to generate the tracking URL that is wrapped inside a HTML <img src=''> code.
Paste this code before the </body> code in your pages.
<?php
// Example 1: Tracks a pageview for Website id = {$IDSITE}
echo '<img src="'. str_replace("&","&", Piwik_getUrlTrackPageView( $idSite = {$IDSITE}, $customTitle = 'This title
will appear in the report Actions > Page titles')) . '" alt="" />';
// Example 2: Triggers a Goal conversion for Website id = {$IDSITE} and Goal id = 2
// $customRevenue is optional and is set to the amount generated by the current transaction (in online shops for example)
echo '<img src="'. str_replace("&","&", Piwik_getUrlTrackGoal( $idSite = {$IDSITE}, $idGoal = 2, $customRevenue =
39)) . '" alt="" />';
?>
The Advanced Image Tracker method is similar to using the standard Javascript Tracking code. However, some user settings are not
detected (resolution, local time, plugins and cookie support).
You can also query the Piwik Tracker API remotely via HTTP.
This is useful for environment where you can't execute HTML nor Javascript.
Paste this code anywhere in your code where you wish to track a user interaction.
<?php
$piwikTracker = new PiwikTracker( $idSite = {$IDSITE} );
// You can manually set the visitor details (resolution, time, plugins, etc.)
// See all other ->set* functions available in the PiwikTracker.php file
$piwikTracker->setResolution(1600, 1400);
// Sends Tracker request via http
$piwikTracker->doTrackPageView('Document title of current page view');
// You can also track Goal conversions
$piwikTracker->doTrackGoal($idGoal = 1, $revenue = 42);
?>
The following code snippet is an example of how to track a Page View using the Tracking API PHP client.
Ecommerce tracking example
Here is an example showing how to track Ecommerce interactions on your website, using the PHP Tracking API. Usually, Ecommerce tracking is done using standard Javascript code, but it is very common to record Ecommerce interactions after the fact (for example, when payment is done with Paypal and user doesn’t come back on the website after purchase). For more information about Ecommerce tracking in Piwik, check out the documentation: Tracking Ecommerce in Piwik.
// Example 3: Tracking Ecommerce Order
$t->addEcommerceItem($sku = ‘SKU0011′, $name = ‘Endurance – Shackleton’ , $category = ‘Books’, $price = 17, $quantity = 1);
$t->addEcommerceItem($sku = ‘SKU0321′, $name = ‘Amélie’ , $categories = array(‘DVD Foreign’,'Best sellers’,'Our pick’), $price = 25, $quantity = 1);
$t->doTrackEcommerceOrder($orderId = ‘B000111387′, $grandTotal = 55.5, $subTotal = 42, $tax = 8, $shipping = 5.5, $discount = 10);
Set the visitor IP, or the visit date/time
To set the visitor IP, or the date and time of the visit, or to force to record the visit (or page, or goal conversion) to a specific Visitor ID, you must call setTokenAuth( $token_auth ). The token_auth must be either the Super User token_auth, or the token_auth of any user with ‘admin’ permission for the website you are recording data against.
Track Ecommerce, Custom Variables, etc.
The PHP Tracking Client provides all features of the Javascript Tracker, in particular all Ecommerce Tracking, Custom Variable, etc. is also available in the PHP Tracking client. Functions are named the same as the Javascript functions.
See below the full reference for the PHP Client used to track visits, page views, actions or Goal conversions in Piwik.
Source: API documentation for the PiwikTracker class.
A Java client for the Piwik Tracking API was submitted by a Piwik user. The code and doc can be found in the Java Tracking Client for Piwik.
A complete Python client implementation of the Tracker API is also available at Piwik Tracking API Client in Python.
If you wish to contribute an implementation of the Piwik Tracker class in another language, please do so and send us an email and we will gladly list it here.
If you are not using PHP, Java, or Javascript (for which clients are available), you might be looking for the REST API reference documentation so you can build your simple Piwik client to track your app or software analytics.
See: http://piwik.org/docs/tracking-api/reference/#toc-debugging-the-tracking-api-requests
See: http://piwik.org/docs/tracking-api/reference/#toc-tracking-api-reference
See: http://piwik.org/docs/tracking-api/reference/#toc-example-http-request-to-piwik-tracking-api
In Piwik, we use the Tracking API PHP client in our Quality Assurance process, to generate fake (controlled) tracking requests used to test that reports are processed correctly. For more information, you can check out some of the code in Main.test.php, and learn more about our QA process.
See also these related FAQ entries
Feedback on this page
Have you found an error in this page, or do you think some information is missing or not clear? We appreciate you taking the time to send us your suggestions and feedback on this page.