Piwik is an open source web analytics system in PHP and a great alternative to Google Analytics.
It offers simplified tracking via an image tracking code:
<!-- Piwik Image Tracker -->
<img src="http://example.org/piwik/piwik.php?idsite=1&rec=1" style="border:0" alt="" />
<!-- End Piwik -->
In some scenarios, you might not want to expose that you are using Piwik analytics, or would like to avoid loading unnecessary resources.
Let’s use the Piwik Tracking API to turn any image into a tracker. This is useful for websites, newsletter stats and email signatures alike.
Prequisites
You need to have a Piwik install running with administrative access. Apache is required to get a “nice” image url.
Step 1 – Setup Piwik
Create a new website in Piwik, which will contain your stats for the image. Make note of the new site ID (Visible in Settings > Websites)
Create a new user and give that user admin rights to the website. Make note of the token_auth for that user. (Under Settings > Users)
Step 2 – Get the PiwikTracker.php Tracking API
You can obtain this file by going to the following URL (Replace example.org with your piwik install path)
http://example.org/piwik/index.php?module=SitesManager&action=downloadPiwikTracker&idSite={$IDSITE}&piwikUrl=http://example.org/piwik/
You can also find a copy of the file here.
Step 3 – Build the tracking code
Pick an image you’d like to use, name it stats.png and put it in a folder on your web server. I am going to use this “thumbs up” clipart. Also put PiwikTracker.php in this folder.

Now, create the file stats_t.php. (Base code below) Change line 3 and 4 to your own site id and the token_auth of the user you created in step 2. Also set your Piwik URL at line 10. If you want to distinguish between multiple tracking images, you can change line 22 to have a different message. That means you can track any number of images using one site in Piwik.
<?php
//Set the id of your piwik site here
$idSite = 1;
$token_auth = 'your user token here';
//Load Piwik Tracker
require_once 'PiwikTracker.php';
//Set the URL path to your Piwik Installation
$t = new PiwikTracker($idSite,'http://example.org/piwik');
//Auth to allow for more API functions
$t->setTokenAuth($token_auth);
//Set correct IP (Should be users, not the web server issuing the request)
$t->setIp($_SERVER['REMOTE_ADDR']);
//Set referrer (if applicable)
if(isset($_SERVER['HTTP_REFERER']))
$t->setUrl($_SERVER['HTTP_REFERER']);
$t->doTrackPageView('Image viewed');
$im = imagecreatefrompng("stats.png");
//For transparency (Alpha blending)
imagealphablending($im, true);
imagesavealpha($im, true);
//Set header
header('Content-Type: image/png');
//Output image
imagepng($im);
//Unload image
imagedestroy($im);
?>
You can set many more visitor details, refer to the reference.
Create a .htaccess file in the same folder. We will use this to rewrite the URL so that we can still use a .png extension for the file. When stats_t.png is called, Apache will instead load stats_t.php , which will run the tracking code.
RewriteEngine On
RewriteRule stats_t.png stats_t.php
Your folder structure should now look like this:

Verify that your image is displayed properly in a web browser by navigating to stats_t.png

Verify that Piwik recorded the visit correctly. If it did not, make sure you have entered $token_auth and $idSite correctly.
Step 4 – Display your new tracking image anywhere
Now you can include your image anywhere, like this:
<img src="http://khromov.se/email2/stats_t.png" alt="thumbs up" />
If you would like more information please see the official documentation of the Tracking API.
Let me know if this helped you out or if you have any suggestions or improvements!
Update: Now includes referrer tracking. If the image is used on a page, you will see what page it is used on. (See line 19 and 20 of stats_t.php)