Pixel Tracking in PHP

By Dillon Smart · · · 0 Comments

PHP

When creating email campaigns, it’s important to know how many recipients actually opened your email. This information can help you determine if your efforts are working or if it’s time to change your strategy. 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 and how to count the number of times your email was opened.

If you are unfamiliar with PHP, learn why PHP powers some of the biggest sites in the world and why developers choose to use PHP.

Create the pixel for email tracking

First, you will need to create a PHP file or function which executes 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);

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

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

Now it’s time to set the pixel to the color you chose as well as the image file type and headers.

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

Finally, destroy the image to free up memory.

imagedestroy($pixel);

Now following your URL, an image will be created 1 pixel by 1 pixel in your specified color. Use the URL as the image source in your email template.

<img src="https://example.com/pixel-tracking.php">
Email Pixel Tracking in PHP

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.

Conclusion

You have now successfully generated an image in PHP for use in your email templates for tracking email opens.

PHP

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Laravel 419 page expired after login [SOLVED]

Updated 1st January 2024

Laravel is the most popular PHP framework, dominating the space since 2014, and the chosen framework by many new PHP developers. Many new developers run into the same errors when first using the framework, the most common being a 419 Page Expired error on a form post.  What is Cross-Site Request Forgery (CSRF)? Cross-site request

WordPress wp-admin redirects to HTTPS

Updated 18th May 2021

Does your WordPress wp-admin redirect to https rather than http? Below is a simple tutorial to stop this from happening. First, check the siteurl in the wp_options table within your database. Make sure the option_value is your site address with http:// not https://. If your siteurl is set as http:// the try the following. Open

PHP Variable Variables

Updated 1st June 2022

In PHP, it is sometimes necessary to have variables that have variable names. Traditionally, a variable is given a name and a value is given to the variable like so: $foo = 'bar'; A Variable Variable, or Double Dollar Variable, gets its name from the value of another variable like so: $foo = 'bar'; $$foo