Google Analytics for Your Emails

Last time I was talking about Google Analytics in your rails app. Today I will continue with Google Analytics but this time in your emails.

This is a small hack but it can be really useful. As you probably know, with a classic email, you cannot know if the receiver open the email you just sent but when you use some services like Mailchimp or Sendgrid, you can have those statistics. So today we will do like Sendgrid and Mailchimp but using Google Analytics.

I will not talk too much, here is directly the trick to log when people open your email.

<img src="" />

Let explain a bit. : This is the API for google analytics to send some data. You can send almost all the data you want. It takes many parameters:

required parameters:

  • v: version of the API for now this parameter value is 1
  • tid: the profile id of your website (ie: UA-XXXXXXXX-XX)
  • cid: client id
  • t: the kind of data you want to send. In our case an event

optional parameters:

  • ec: the category of the data. In our case email
  • ea: the action relative to your data. In our case open
  • el: the label of your data for example the name of your campaign. This will be helpful to identify your data in Google Analytics
  • cs: campaign source. In our case this is a newsletter
  • cm: campaign medium. In our case this is an email
  • cn: campaign name. You can put the same value ad el
  • cm1: custom metric. You can just set 1, like that easily create some custom variables in Google Analytics with 0 or 1 value.

In our case for the email we want to send a new event open in the category email with the label you want.

How does it works

When the email client (web or application) will load the email it will try to download the image because we wrap this API call in an IMG tag and every src in IMG tag will be called by the GET method which is exactly what Google needs for this API. So when the email client will load the picture, in fact it will execute one GET request to send some data in your analytics and so, now, you can even see in the real-time view of your Google Analytics people who open this email and because you set campaign parameters you can also see in the Acquisition/Campaigns tab all your campaigns. And, even with the previous post about fetching google analytics data you can easily display some graphs with the statistics of your campaign using some filters on your campaign ;)

personal thinking : This trick of an API call using an IMG tag is really nice but to be honest I’m not sure this will be possible in the next years because the behavior is not the one expected and in general you can do some bad things with IMG tags like download some scripts instead of pictures, you can also inject some code so I think IMG have to be rethink and it’s good we have to rethink about it also for responsive pictures.

To finish this hack will perhaps not works in many years but until this moment it’s a really simple hack and it can be very helpful ;)


Copyright © 2014 - Anthony Estebe -