Use Google Analytics without JavaScript

Published in: Google Analytics - Google Apps Script

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

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 = ""
                 + "&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 = ""
                    + "utmac=" + account
                    + "&utmn=" + z
                    + "&utmp=" + utmp
                    + "&utmdt=" + encodeURIComponent(email_subject)
                    + "&utme=" + utme
                    + "&utmr=0&utmwv=5.4.5&";
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Analytics - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube