Tracking Email with PHP and Image

Tracking email with PHP and image

Basically, in the HTML body of your email, there will be an <img> tag that would look like this :

<img src="http://www.yoursite.com/tracker.php?id=123456" alt="Sample Image" />

When someone reads his mail, with images enabled, the email-client will send a request to tracker.php, to load the image, passing it id=123456 as a parameter.


This tracker.php script will be on your server, and, when called, it will :

  • Check the id parameter,
  • Use it to find to which email address it corresponds -- when generating the email for each one of your subscribers, you'll have generated an id different for each e-mail.
  • Do some stuff -- like log "email 123456 has been opened", and some additional informations
  • return the content of a small image ; like a 1x1 transparent gif.


The tracker.php script knows from which IP address it's been called -- like any other PHP script :

$ipAddress = $_SERVER['REMOTE_ADDR'];

And, starting from this IP address, you can use a geolocation service to find out from where in the world the email has been opened.

As a couple of examples, you could take a look at MaxMind, or IPInfoDB

As you know that id=123456 corresponds to one specific email address, this allows to find out where each one of your subscribers are.

Tracking email opens with a real image

If you're going to use a PHP script like that, it needs to return image data, not just a HTML image tag. Easiest way to do that will be something like:

<?php
header("Content-Type: image/jpeg");
readfile("image.jpeg");

do_all_your_tracking_stuff();

Note that this is returning the image data first, so that a mail client can start displaying it immediately, rather than waiting for your SQL queries to complete.

showing image in email using the php image tracking trick

src attribute should point to an image. Your src points to the (dynamic) text file which contains URL, not the actual image. newsletterInfo.php should output an image, not its URL.

Possible solutions:

  1. The easy way: make a redirection within newsletterInfo.php, like this:

    header('Location: url_to_the_image.jpg');

    (replace the file name with what you're currently echoing and remove that echo)

  2. The hard way: open the image as a file, send proper headers and then echo the image. (not really a good idea)


Track php emails with php script?

You can't directly track the other status from the mail() function. Technically Is Sent only tells you that the mail was passed over to the server's outbound mail queue successfully - you can't actually tell if it left your server.

1,. You will need to check your mail server logs to see exactly when the email left the server.

2,3. Num of delivered and delivered date - again you would need to check your mail server logs to see when the mail was handed over (successfully) to a third party mail server. However it would depend on your definition of delivered (into the end-users mailbox? Into their email client?) as to how reliable these stats would be.

4,5,6. Total number read, unique number read, read date. You can't accurately track this. However if you sent HTML email you could embed an image into the email whereby the source of the image was your webserver. If each image URL had a unique identifier for the person you sent the email to then you could track this from your server logs (or via php if the url was a php script that returned an image). However this relies on the end user allowing the loading of images from external webservers (Outlook and gmail for example have this turned off by default).

7,. If you sent the from address to be a script on your server it could parse the bounce message and determine how many bounced. Alternatively you can just have the from address be a mailbox that you go into and check manually.

8, 9. Each link in the email would need to be a link to a url on your webserver. That URL could be a script that would track who clicked (by the use of a query variable) and what they want to see (another query variable) and then redirect them (header function in php) to where you want them to end up.

Email tracking techniques in php

The tracking image must not be sent as inline with the email, as what you are counting, to keep track of people who opened your newsletter, is the number of times that image was downloaded / requested from your server.

This means your tracking has a URL that looks like this :

http://www.yoursite.com/tracking.php?id_newsletter=X&user_id=Y

Note, though, that this will only work for users who choose to enable the display of images while looking at your newsletter -- and more and more e-mail clients disable images, by default.


Other tracking solutions ?

Instead of tracking number of views, you could track number of clicks on links.

For example, instead of having a direct link, in your newsletter, that would look like this :

http://www.yoursite.com/destination-page.php

The link would point to a counter / tracking page :

http://www.yoursite.com/track-clicks.php?newsletter_id=X&user_id=Y&destination_page_id=Z

And that track-clicks.php page would :

  • insert some data to the database (or somewhere else), to keep track of the click
  • select from the database (or elsewhere) the URL of the page that corresponds to destination_page_id=Z
  • redirect the user to that page.


Tracking clicks, instead of views, has several advantages :

  • Even if the number is smaller, you are not tracking users who registered a long time ago and are not interested anymore : you are only keeping track of users who are interested enough to read your newsletter and clicks on links
  • This should works much better : even if external images are disabled, users will have to click on your tracking links to access the page that interests them


Related Topics



Leave a reply



Submit