Tracking email opens in Google Analytics

2019-01-31 01:50发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.