You can call the Matomo Tracking API using your favorite programming language.

As an example, follow these steps to get started with the PHP Tracking Web API client

  • Click here to download the file MatomoTracker.php (You may need to right click this link and select Save Page As)
  • Upload the MatomoTracker.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://matomo.example.org/ with your Matomo URL.

    <?php 
    // -- Matomo Tracking API init -- 
    require_once "/path/to/MatomoTracker.php";
    MatomoTracker::$URL = 'https://matomo.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;", Matomo_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;", Matomo_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 (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 
      $matomoTracker = new MatomoTracker( $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/
      $matomoTracker->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 MatomoTracker.php file
      $matomoTracker->setResolution(1600, 1400);
      
      // Sends Tracker request via http
      $matomoTracker->doTrackPageView('Document title of current page view');
      
      // You can also track Goal conversions
      $matomoTracker->doTrackGoal($idGoal = 1, $revenue = 42);
      ?>
      

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

Previous FAQ: Server-side analytics tracking and/or Desktop apps tracking with Matomo