Javascript client for Tracking Web API

This is default recommended client. Learn more at: developer.piwik.org/api-reference/tracking-javascript

Using the Javascript client 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: Software Apps, iPhone or Android apps, mobile websites, Ebay pages, etc.

In these cases, you can use alternative ways to record visitors, visits, pages and Goal conversions in Piwik:

  • Image Tracker
  • PHP client for Tracking API
  • Java client for Tracking API
  • Python client for Tracking API
  • and more

Tracking Web API Clients

Learn more about the various clients you can use to track data in Piwik.

iOS SDK

If you are building iOS apps, you can use the Objective-C PiwikTracker client to send tracking data to your Piwik server.

Learn more and download Piwik iOS SDK on Github at github.com/piwik/piwik-sdk-ios

Android SDK

If you are building Android apps, you can use the Android Java client to send tracking data to your Piwik server.

Learn more and download Piwik Android SDK on Github at github.com/piwik/piwik-sdk-android

PHP client

The default client is the PHP PiwikTracker, learn more at: developer.piwik.org/api-reference/PHP-Piwik-Tracker

Java client

A Java client for the Piwik Tracking API is available on Github: github.com/piwik/piwik-java-tracking

Python client

A complete Python client implementation of the Tracking API is available on Github at github.com/piwik/piwik-python-api

C# client

A C# client implementation of the Tracking API is available on Github at github.com/piwik/piwik-dotnet-tracker

Appcelerator Titanium Module

If you are building Mobile apps using Appcelerator Titanium, use the Piwik Analytics Module for Titanium to measure how users are navigating and consuming your apps. Learn more and download the Piwik Module for Titanium at github.com/manumaticx/ti.piwik

Tracking Web API Reference

If you are not using Javascript, PHP, Java, Python, C# or Objective -C (for which clients are available), you can still directly call the Tracking Web API over http.

The Tracking Web API Reference documents the various parameters and features of the Tracking API.

Use case: Tracking data using the php client

We will now look at some of these methods to record data in Piwik. In the following, replace {$IDSITE} with your Piwik website ID, and replace http://piwik.example.org/ with your Piwik URL.

Image Tracker code

The Image Tracker code can be used when Javascript is not allowed.

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://piwik.example.org/piwik.php?idsite={$IDSITE}amp;rec=1" style="border:0" alt="" />
<!-- End Piwik -->

The following parameters can also be passed to the image URL:

  • rec – (required) The parameter &rec=1 is required to force the request to be recorded
  • idsite – (required) Defines the Website ID being tracked
  • action_name – Defines the custom Page Title for this page view
  • urlref – The Referrer URL: must be set to the referrer URL used before landing on the page containing the Image tracker. For example, in PHP this value is accessible via
    $_SERVER['HTTP_REFERER']
  • idgoal – The request will trigger the given Goal
  • revenue – Used with idgoal, defines the custom revenue for this conversion
  • and more! – There are many more parameters you can set beyond the main ones above. See the Tracking API documentation page.

Piwik Tracking API (Advanced users)

It is also possible to call the Piwik Tracking API using your favorite programming language.

Follow these instructions to get started with the PHP Tracking Web API client:

  • Click here to download the file PiwikTracker.php
  • Upload the PiwikTracker.php file in the same path as your project files
  • Copy the following code, then paste it onto every page you want to track.

    <?php 
    // -- Piwik Tracking API init -- 
    require_once "/path/to/PiwikTracker.php";
    PiwikTracker::$URL = 'http://piwik.example.org/';
    ?>
    
  • Choose a Tracking method, then paste the code onto every page you want to track.

    • Method 1: Advanced Image Tracker
      The client is used to generate the tracking URL that is wrapped inside a HTML code.
      Paste this code before the code in your pages.

      <?php 
      // Example 1: Tracks a pageview for Website id = {$IDSITE}
      echo '<img src="'. str_replace("&","&amp;", 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("&","&amp;", 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).

    • Method 2: HTTP Request
      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);
      ?>
      

      Read more about the Piwik Tracking HTTP API or the PHP Client.

More information

See also these related FAQ entries