Introduction to Matomo Tracker
Matomo Tracker is a powerful PHP tracking library to track user activity on your website. It offers a plethora of APIs that you can leverage to gather useful analytics data. This guide will walk you through the most used APIs and provide hands-on code examples.
API Examples
Basic Tracking
To start tracking user activity, initialize the Matomo Tracker with your site ID and URL:
$tracker = new PiwikTracker($idSite = 1, $apiUrl = 'http://your-matomo-domain.tld/matomo.php');
Setting Custom Variables
You can set custom variables to track additional data:
$tracker->setCustomVariable($index = 1, $name = 'UserType', $value = 'Member', $scope = 'visit');
Tracking Page Views
To track page views on your website:
$tracker->doTrackPageView('Homepage');
Tracking Events
To track custom events such as button clicks:
$tracker->doTrackEvent('Button', 'Click', 'SubmitButton', $value = 1);
Tracking Goals
Configure goal tracking to measure conversions:
$tracker->doTrackGoal($idGoal = 1, $revenue = 0.5);
Tracking Outlinks
Track external link clicks:
$tracker->doTrackOutlink($url = 'http://external-website.com');
Tracking Downloads
Track file download activities:
$tracker->doTrackDownload($url = 'http://your-site.com/file.zip');
Complete Application Example
Below is a full example of a simple PHP application demonstrating multiple Matomo Tracker APIs:
<?php require_once 'path/to/PiwikTracker.php'; use PiwikTracker; $tracker = new PiwikTracker($idSite = 1, $apiUrl = 'http://your-matomo-domain.com/matomo.php'); // Track a page view $tracker->doTrackPageView('Homepage'); // Set custom variable $tracker->setCustomVariable($index = 1, $name = 'UserType', $value = 'Member', $scope = 'visit'); // Track an event $tracker->doTrackEvent('Button', 'Click', 'SubmitButton', $value = 1); // Track a goal $tracker->doTrackGoal($idGoal = 1, $revenue = 0.5); // Track an outlink $tracker->doTrackOutlink($url = 'http://external-website.com'); // Track a download $tracker->doTrackDownload($url = 'http://your-site.com/file.zip'); ?>
With these examples, you can start leveraging Matomo Tracker to gain insights into user behavior and improve your website’s performance.
Hash: be1ddff5af987fff94b3bc797223f06a7558dbf19982cf691a812ff6e54b9c2b