Use Google Analytics without JavaScript

The tutorial — tracking Gmail opens with Google Analytics - — uses Google Script to insert the Google Analytics tracking code into the outgoing email messages without requiring JavaScript.

The script creates a 1x1 tracking GIF that creates an event in your Google Analytics when a recipient opens your email. Since Gmail now automatically downloads images, the email open activity is very likely to be tracked.

Apps Script can track email opens using both Classic Google Analytics (utm) and the new Universal Analytics and requires no JavaScript.

/*
  Email Tracking with Google Analytics
  ====================================
  Credit: http://labnol.org/?p=8082
*/

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
    .toString(16)
    .substring(1);
}

function guid() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

function emailTrackingGIF(version, email_address, email_subject, event_name) {
  /* Google Analytics Account ID like UA-1234-56 */
  var account = 'UA-12345-XX';

  /* Random ID to prevent browser caching */
  var z = Math.round(new Date().getTime() / 1000).toString();

  var imageURL = '';

  if (version == 'universal') {
    imageURL =
      'https://ssl.google-analytics.com/collect?v=1&t=event' +
      '&ec=' +
      encodeURIComponent(event_name) +
      '&tid=' +
      account +
      '&z=' +
      z +
      '&cid=' +
      guid() +
      '&ea=' +
      encodeURIComponent(email_subject.replace(/'/g, '')) +
      '&el=' +
      email_address;
  } else if (version == 'classic') {
    /* Relative path of the Web Page to be tracked */
    var utmp = '/inbox/' + email_address.replace('@', '.');

    var utme = encodeURIComponent('5(' + event_name + '*' + email_address + '*' + email_subject + ')');

    var request =
      'http://www.google-analytics.com/__utm.gif?' +
      'utmac=' +
      account +
      '&utmn=' +
      z +
      '&utmp=' +
      utmp +
      '&utmdt=' +
      encodeURIComponent(email_subject) +
      '&utme=' +
      utme +
      '&utmr=0&utmwv=5.4.5&utmhn=labnol.org&utmt=event&utmcc=__utma%3D999.999.999.999.999.1%3B';
  }
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.