How to create email pixel tracking in PHP

Published - Updated

Email Pixel Tracking

When creating email campaigns, its important to know how many recipients actually opened your email. Most mail champaign tools such as Mailchimp and SendInBlue use a technique called Pixel Tracking. In this post, you will learn how to create email pixel tracking in PHP.

Note: This method requires the recipient to have downloaded images on the email.

Create the pixel for email tracking

You will need to create a php file or function that which runs the following script. If you are using a function to run the script, you will need to ensure it is called when your specified endpoint is loaded. Example https://example.com/pixel-tracking or https://example.com/pixel-tracking.php.

$pixel = imagecreate(1,1);

Now you will need to set the color of the pixel which is going to be tracked. Ensure the color doesn’t stand out in you emails design. If the background of your email is white, then set the color to white using RGB.

$color = imagecolorallocate($pixel,255,255,255);

Now its time to set the pixel to the color you chose and set the image type and headers.

imagesetpixel($pixel,1,1,$color);
header("content-type:image/jpg");
imagejpeg($pixel);

And finally, destroy the image to free up memory.

imagedestroy($pixel);

Now following your URL, an image will be create 1 pixel by 1 pixel in your specified color.

How to count email pixel tracking

To count up how many times this image was requested in emails, you can either use your server access logs or store a record in a database at the same time the pixel is created.

You can also use URL parameters to differentiate requests, such as a campaign id (https://example.com/pixel-tracking?cid=123) so you know which campaign the request was coming from.