Developing a tracking pixel

I am trying to build a pixel that would track the current URL the user is on when they visit. I can use either JS (preferred) or a 1x1 image pixel. With JS I am assuming that I'd need to run an AJAX request to a PHP script to capture the info that I need and with an image pixel I am having issues getting the currently URL.

I also thought about URL encoding the current URL with JS and dynamically placing the image pixel with the encoded current URL as a query string to a PHP script, but that I can get to be very long.

If I am to go the AJAX route, which AJAX library can I use? JQuery is too bloated for this purpose.

Any other ideas?



You can write a script that creates and returns a .gif, .jpeg or .png image using PHP for tracking purposes using the GD library (which is often distributed with PHP in modern versions). If you don't have access to GD, you can always recompile PHP with GD enabled.


pixel.php (commented for the purposes of explanation):


  // Create an image, 1x1 pixel in size

  // Set the background colour

  // Allocate the background colour

  // Set the image type

  // Create a JPEG file from the image

  // Free memory associated with the image


In a simple example, you can then call this tracking pixel using the following example URL in an email or other page:

<img src="">

Using variables:

Within your pixel.php you can then parse and interpret any $_GET variables that are passed to it within the image tag, simplistically:

if (isset($_GET['a'])) {
  // (Do|log) act on a
if (isset($_GET['b'])) {
  // (Do|log) act on b
if (isset($_GET['c'])) {
  // (Do|log) act on c

Apply and repeat as you need, but you can be quite sophisticated about what you do and especially as you have access to quite a lot of information about the user through being able to set vars on the $_GET string.

A more applicable example might be:

<img src="">

Tracking more than just $_GET variables:

You can also pick up much more information using PHP, such as:

// Server variables
$referer = $_SERVER['HTTP_REFERER'];
$useragent = $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser(null, true);

and then perhaps insert into a tracking table in your database:

$sql = "INSERT INTO campaign_tracking 

This is a(the) basic method used widely for tracking email marketing campaigns and specifically in PHP, but the same method is applicable using other scripting/programming languages and libraries - and for other purposes too.

Further and useful information on GD:


Here is another PHP implementation of a tracking pixel, from the Open Web Analytics project, which attempts to basically be a PHP clone of Google Analytics.

It returns a 1x1 transparent GIF image (without using a PHP image library!), with a no-cache header (important for accurate tracking), and flushes the output so you can continue processing the analytics without blocking the HTTP response (performance). It seems like a pretty advanced implementation, worth trying out.


// turn off gzip compression
if ( function_exists( 'apache_setenv' ) ) {
  apache_setenv( 'no-gzip', 1 );

ini_set('zlib.output_compression', 0);

// turn on output buffering if necessary
if (ob_get_level() == 0) {

// removing any content encoding like gzip etc.
header('Content-encoding: none', true);

//check to ses if request is a POST
  // the GIF should not be POSTed to, so do nothing...
  echo ' ';
} else {
  // return 1x1 pixel transparent gif
  header("Content-type: image/gif");
  // needed to avoid cache time on browser side
  header("Content-Length: 42");
  header("Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
  header("Expires: Wed, 11 Jan 2000 12:59:00 GMT");
  header("Last-Modified: Wed, 11 Jan 2006 12:59:00 GMT");
  header("Pragma: no-cache");

  echo sprintf('%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%',71,73,70,56,57,97,1,0,1,0,128,255,0,192,192,192,0,0,0,33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59);    

// flush all output buffers. No reason to make the user wait for OWA.


Output 1px x 1px this way:

header('Content-type: image/png');
echo gzinflate(base64_decode('6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfsEMYJC3jH0ikOLxdHEMqZiTnJCQAOSxMDB+E7cIBcl7uvq5rHNKaAIA'));

Here's an extremely simplified tracking pixel written in PHP.

How a Tracking Pixel Works

A tracking pixel is like the most primitive beacon possible, and it operates by exploiting a fact of web pages: images are a separate request from the page.

If you are already able to run your JS code on someone else's page, you should just POST the data back to your server. No need to display a tiny pixel that will only get the same kind of data.


It is a similar problem with this effect, since a call to a function to execute a mark of when the email was seen or opened was introduced in the alt of the pixel, but it does not throw the action correctly.

<img src="" width="1" height="1" alt="Web Bug from;id_cart=1002&amp;action=visualize&amp;wichRemind=1">

public static function visualize()

    $wichRemind = Tools::getValue('wichRemind');
    $id_cart = Tools::getValue('id_cart');
    $token = Tools::getValue('token_cart');

    if ($token == md5(_COOKIE_KEY_.'recover_cart_'.$id_cart)) {
        $query = "UPDATE "._DB_PREFIX_."cartabandonment_remind SET visualize = 1 WHERE wich_remind = ".(int)$wichRemind." AND id_cart = ".(int)$id_cart;

    header('Content-Type: image/png');



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.