Mar 7, 2012 - Development    Comments Off

Tracking Email Views with Google Analytics

If you send HTML emails it’s possible to track their views through Google Analytics (GA). Although most email applications do not run Javascript, you can actually record GA data on the server side. This is an example of implementing view tracking for emails in PHP.

Google offers GA code for mobile apps as well as mobile browsers that do not run Javascript. You can record visits through an image whose source is a PHP script. You’ll be adding a tiny image to the end of your HTML emails that triggers the GA code. Google offers the script as an SDK. Download the SDK. The ga.php file doesn’t need to be modified for our example, but we did implement the rest of their code with some variations.

Our own goal was to simply track the increase and decrease in the viewing of emails sent from the website. We wanted to be able to see if changes to our email format affected email delivery. Since most email clients make the user enable images to be loaded in HTML emails, you won’t get true statistics on views, but over time you’ll definitely be able to notice increases and decreases in email viewing.

Below is our altered function that we call in order to create the URL needed to be placed in the image source. We wanted to only record the type of email that was viewed, not any URL/Referrer information like GA normally records. So we altered the function to remove the URL/Referrer code and to accept an $emailcampaign parameter which we populate with “email-something”.

$GA_ACCOUNT = “UA-XXXXX-1″;
$GA_PIXEL = “_ga.php”;

function googleAnalyticsGetImageUrl($emailcampaign)
{
global $GA_ACCOUNT, $GA_PIXEL;
$url = “”;
$url .= $GA_PIXEL . “?”;
$url .= “utmac=” . $GA_ACCOUNT;
$url .= “&utmn=” . rand(0, 0x7fffffff);

$path = $emailcampaign;

if(!empty($path))
{
$url .= “&utmp=” . urlencode($path);
}

$url .= “&guid=ON”;

return $url;
}

While we create the HTML string for the email we call googleAnalyticsGetImageUrl(“email-something”) and set the result as the “src” of an image tag placed at the end of the body of our HTML email. Then in GA we can search for Content like “email-” and see all of the views based on the type of email (campaign).

Comments Closed

Comments are closed.