23

We have tracking in our emails to track clicks back to our site through Google Analytics. But is there a way to track opens? I would imagine I have to add a google tracking image to the email somewhere. Possibly javascript too?

Yahel
  • 37,023
  • 22
  • 103
  • 153
at.
  • 50,922
  • 104
  • 292
  • 461
  • 2
    You cannot put JavaScript in an email. Well, you *can*, it'll just never execute. – Yahel Dec 16 '10 at 03:43
  • Absolutely ZERO mail clients execute any form of JavaScript? – at. Dec 16 '10 at 03:55
  • very few do. But whats the point, you dont get stats . You only get random numbers which you cant rely on. – KA. Dec 05 '12 at 09:39
  • 1
    If you want to see in Google Analytics, how often an email or newsletter was opened (or viewed), you can use http://email-tracking-with-google-analytics.com/, There you can get a pixel which you insert into your email. The pixel will then cause your Google Analytics to show how often the email has been read. –  Mar 04 '11 at 02:05
  • as demonstrated below, no javascript is needed to use analytics, just loan the gif file. – Brady Moritz Jul 14 '13 at 21:44

6 Answers6

16

As others have pointed out, you can't use Javascript in email. The actual tracking is done by a request for __utm.gif though and the Javascript just constructs the GET parameters.

Google supports non-Javascript uses of Google Analytics per their Mobile web docs: http://code.google.com/mobile/analytics/docs/web/

They document the full list of parameters, but the only necessary parameters are:

Parameter    Description
utmac        Google Analytics account ID
utmn         Random ID to prevent the browser from caching the returned image
utmp         Relative path of the page to be tracked
utmr         Complete referral URL
Turadg
  • 7,471
  • 2
  • 48
  • 49
14

The reference that describes all of the parameters that the Google Analytics tracking GIF allows is here. Use it to build an <img> tag in your email that references the GA GIF.

According to this post, the minimum required fields are:

  • utmwv=4.3
  • utmn=<random#>&
  • utmhn=<hostname>&
  • utmhid=<random#>&
  • utmr=-&
  • utmp=<URL>&
  • utmac=UA-XXXX-1&
  • utmcc=_utma%3D<utma cookie>3B%2B_utmz%3D<utmz cookie>%3B
Raleigh Buckner
  • 8,343
  • 2
  • 31
  • 38
4

It sounds like you are using campaign tracking for GA but also want to know how many opens there were. This is possible to do with Google Analytics, since they track pageviews or events by use of pixel tracking as all (I think?) email tracking does. You cannot use javascript, however, since that will not execute in an email.

Using Google Analytics pixel tracking: The easiest way would be to use browser developer tools such as Firebug for Firefox or Opera's Dragonfly to capture a utm.gif request and copy the URL. Modify the headers to suit your needs. You can count it either as an event or pageview. If you count it as an event it should look something like this:

http://www.google-analytics.com/__utm.gif?utmwv=4.8.6&utmn=1214284135&utmhn=www.yoursite.com&utmt=event&utme=email_open&utmcs=utf-8&utmul=en&utmje=1&utmfl=10.1%20r102&utmdt=email_title&utmhid={10-digit time code}&utmr=0&utmp=email_name&utmac=UA-{your account}

You can use this to understand what describes what in the headers.

vee_ess
  • 699
  • 5
  • 14
  • Yes I was thinking about registering virtual pageviews... but was hoping there was a standard mechanism that would tie nicely into my campaigns. After some digging, looks like google used to support this, but gave up several years ago with so many mail clients not opening images. – at. Dec 16 '10 at 03:57
  • That's a good point; it's not uncommon to see higher page visits than opens. Most mainstream clients, particularly those suited for corporate environments, don't show images by default. – vee_ess Dec 16 '10 at 20:25
  • The reason most email clients doesn't show images by default is to mitigate spammers that can use that to automatically detect valid emails. – Tiago Dec 20 '10 at 16:28
2

I better post this to save everyone the trouble of trying to construct that monstrous UTM gif URL.

You can now use the new Measurement Protocol API to send a POST request and easily record events, page views, hits, or almost any other type of measurement. It's super easy!

POST /collect HTTP/1.1
Host: www.google-analytics.com

payload_data

For example, here's a code snippet to send an event in C# (using SSL endpoint):

public void SendEvent(string eventCategory = null, string eventAction = null, string eventLabel = null, int? eventValue = null)
{
    using(var httpClient = new HttpClient() {BaseAddress = new Uri("https://ssl.google-analytics.com/")}) {
        var payload = new Dictionary<string, string>();

        // Required Data
        payload.Add("v", "1"); // Version
        payload.Add("tid", "UA-XXX"); // UA account
        payload.Add("aip", "1"); // Anonymize IP
        payload.Add("cid", Guid.NewGuid().ToString()); // ClientID
        payload.Add("t", "event"); // Hit Type

        // Optional Data
        payload.Add("ni", "1"); // Non-interactive hit

        // Event Data
        if (eventCategory != null)
        {
            payload.Add("ec", eventCategory);
        }
        if (eventAction != null)
        {
            payload.Add("ea", eventAction);
        }
        if (eventLabel != null)
        {
            payload.Add("el", eventLabel);
        }
        if (eventValue != null)
        {
            payload.Add("ev", eventValue.Value.ToString(CultureInfo.InvariantCulture));
        }

        using (var postData = new FormUrlEncodedContent(payload))
        {
            var response = httpClient.PostAsync("collect?z=" + DateTime.Now.Ticks, postData).Result;

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Could not send event data to GA");
            }
        }
    }
}

Way easier than the hack with the __utm gif.

Helpful Example

You can easily add this to emails by doing this:

In an email:

<img src="{url}/newsletter/track.gif?newsletterName=X" />

In your MVC site, for example, NewsletterController:

public ActionResult Track(string newsletterName) {
    using(var ga = new AnalyticsFacade()) {
       ga.TrackEmailOpen(newsletterName);
    }

    return Content("~/images/pixel.gif", "image/gif");
}

In your Global.asax or RouteConfig:

routes.MapRoute(
    "newsletteropen",
    "newsletter/track.gif",
    new
    {
        controller = "Newsletter",
        action = "Track"
    });

BOOM, done, son. You can now track email opens using a much nicer API that's supported and documented.

kamranicus
  • 4,207
  • 2
  • 39
  • 57
  • 2
    This isn't relevant at all to the original question. You can't have an e-mail make a POST on open. – Tyler Feb 05 '14 at 20:33
  • Yes, you can, by creating an image handler (a GET request). This is how we're tracking opens on our newsletters. It's the same as the hacky __utm.gif except YOU are the one hosting your own "GIF" (which can be a transparent pixel) but in your handler/Action you're using the GA API to send an event. Way easier, way more robust. – kamranicus Mar 04 '14 at 21:58
  • I added an example, to prove it :) And I should preface that by saying, technically, **NO** you cannot POST but the original question was how to track email opens using GA and this is a solution that works for us. – kamranicus Mar 04 '14 at 22:07
  • You're telling me it's less hacky to build an ASP.NET site and maintain an IIS server just to act as a middle man and use the API for which you now have to worry about deprecation than to just modify a few fields in a URL and paste it in (which you'd have to do for your solution anyways)? – vee_ess Sep 07 '16 at 15:35
  • @vee_ess if you already have a website (presumably where you send your newsletters from), then calling an API is easier, yes, especially if you need to capture the authenticated user (as in our case). If you don't have a site, use the GIF. Both methods are subject to changes (see comments on the GIF answer), but the API method is more robust if you need extra features (since it can also do events, granular tracking fields, etc.). My answer is good for those of us that need that extra power and control over the tracking. It has worked great ever since I posted this in our production app ;) – kamranicus Sep 22 '16 at 20:25
  • Did you see that the question is for email opens? It's an extremely common and specific question. If a user already has a session and the email is opened in the browser, Google will already capture that. If you're tracking identifiable data, you're in violation of GA terms. The extra power and control you seek is only available on the site where you're not confined to static code and GA already provides everything. I'm glad this has worked for you, but this is dangerous and unnecessary advice to give out to others. – vee_ess Sep 24 '16 at 16:12
  • I didn't say I was passing any identifiable data to GA--this is for an intranet site and emails are opened in Outlook, so there is no session information. Because there's additional restrictions within an intranet (i.e. external requests can be blocked), my solution is a nice workaround that allows tracking email opens within an intranet environment (because the web server can make proxied GA requests). – kamranicus Sep 30 '16 at 20:15
0

Is your requirement is to track how many times an e-mail is open by given user. We have similar problem. We are using SMTP relay server and wanted to track how many times our marketing e-mails are open in addition to google-analytics which register an even only when someone clicks inside link to our site in e-mail.

This is our solution. It is based on making a REST call by overriding image element of html (our e-mails are html base)

where TRACKING is dynamically generated url which points to our REST service with tracking information about person to which e-mail was send. It is something like that

//def trackingURL = URLEncoder.encode("eventName=emailTracking&entityType=employee&entityRef=" + email.empGuid, "UTF-8");

trackingURL = baseUrl + "/tracking/create?" + trackingURL;

It will be something like "https://fiction.com:8080/marketplace/tracking/Create?eventName=email&entityType=Person&entityRef=56"

When when actual e-mail html is generated it, TRACKING will be replaced by

Important point is to return a response of type image and return a one pixel transparent image with REST response.

-1

So i'll assume that the email contains a link to your Site. Certainly GA can record how often that link is clicked because clicking the link will open the page in turn causing the function *_trackPageview()* to be called, which is recorded by GA as a pageview.

So as long as that page has the standard GA page tag, no special configuration is required--either to the GA code in your web page markup or to the GA Browser. The only additional work you have to do is so that you can distinguish those page views from page views by visitors from another source.

To do that, you just need to tag this link. Unless you have your own system in place and it's working for you, i recommend using Google URL Builder to do this for you. Google URL Builder is just a web-form in which you enter descriptive terms for your marketing campaign: Campaign Source, Campaign Medium, Campaign Content, Campaign Name. Once you've entered values for each of these terms, as well as entered your Site's URL, Google will instantly generate a 'tagged link' for you (by concatenating the values to your Site's URL).

This URL generated by Google URL Builder is the link that would be placed in the text of your marketing email.

doug
  • 69,080
  • 24
  • 165
  • 199
  • Great tool, wasn't aware of it... but I was asking about opens, not clicks. – at. Dec 16 '10 at 08:37
  • I know what you were asking about. In this context, these two are the same. Here's why: to GA, an 'open' as you call it, is just a page loading. This in turn causes the call to *trackPageview* which increments the page view count by +1. In this context (your Q) the 'event' responsible for this is clicking the link. – doug Dec 16 '10 at 09:25
  • 4
    Clearly he's talking about EMAIL opens, not page opens for the site that the email links to. – ebynum Oct 06 '11 at 20:19