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 is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

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

We will never send any spam emails. Promise 🫢🏻