Javascript client for Tracking API

This is default recommended client. Learn more at: developer.matomo.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.

Tracking API Clients

Sometimes you cannot use JavaScript to track data with Matomo (Piwik). Whether you develop iOS apps, Android apps, Titanium apps, or apps written in PHP, Java, C#, C++, Python, etc. you can use a native client to track the data in Matomo.

Learn more in Tracking API Clients and SDKs

Tracking API Reference

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

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

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 Matomo using the Image Tracker. Learn more in the FAQ: How do I enable tracking for visitors when JavaScript cannot be used on a page (or for Visitors that have disabled Javascript)?

The Image Tracker code doesn’t use Javascript so Matomo will not be able to track some user information: learn more.

Matomo Tracking API (Advanced users)

It is also possible to call the Matomo 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 (You may need to right click this link and select Save Page As)
  • 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. In the following, replace {$IDSITE} with your Matomo website ID, and replace https://piwik.example.org/ with your Matomo URL.

    <?php 
    // -- Matomo Tracking API init -- 
    require_once "/path/to/PiwikTracker.php";
    PiwikTracker::$URL = 'https://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 Matomo 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} );
      
      // Specify an API token with at least Write permission, so the Visitor IP address can be recorded 
      // Learn more about token_auth: https://matomo.org/faq/general/faq_114/
      $piwikTracker->setTokenAuth('my_token_auth_value_here');
      
      // 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 Matomo Tracking HTTP API or the PHP Client.

More information

See also these related FAQ entries