Google Forms & The Measurement Protocol

Universal Analytics Form & The Measurement Protocol

A few months ago the Google Analytics team launched the Measurement Protocol, a powerful way for developers to send requests to Google Analytics from anywhere. This brings measurement to the next level, from websites and Apps to any customer touch point.

In this article I present the Universal Analytics Form, a solution I developed with my colleague Eduardo Cereto Carvalho, Web Analytics Specialist at Google. The form is an easy-to-use solution to implement the Measurement Protocol to upgrade your current tracking capabilities. Basically, we will use a Google Form and an App Script in order to send requests to Google Analytics using the Measurement Protocol. Sounds cool, doesn’t it? I think it does!

Use Cases

Here are a few cases where you might want to use this solution.

Integrating Offline Transactions into Google Analytics

Suppose you distribute coupons for store purchase in your website; and suppose you make some investment in online advertising. If that’s the case, you should be really eager to understand how much of your ad spending brought in terms of store revenue.

Using this solution, your store cashiers will be able to use a Google Form to update the coupon number and other information on Google Analytics. This way you will be able to link the offline spending to acquisition channels (and other info) for any customer. It is extremely important to keep in mind that Google Analytics Terms of Service strictly forbids to add personal identifiable information (PII) to your data collection, so make sure not to add information like name, surname, username, social security number, etc (see section 7 of link above).

While it is not very difficult to add a coupon ID to the form and get information about the purchase (as you will see in the video below), it might be a bit more challenging to have an ID that can link the offline to online information. The approach I would recommend is to use the Client ID command to retrieve a user Client ID and add it to the coupon. This way, the cashier will be able to fill it in the form too and the cycle will be closed.

Integrating Call Center into Google Analytics

Integrating Call Center data to your analytics data would be somewhat similar to the approach described above. However, it would require an additional step: during a call to complete a transaction, the call center will need an ID number in order to link the transaction to online activity. This can be done by asking the visitor to click on a button on the site that gets the Client ID on Google Analytics (see link above) and shows a popup with that number.

Integrating anything into Google Analytics

Logging expenses details into Google Analytics may also be an useful (and interesting) activity. Using this method, you can use a form to log all transactions you make during the day into Google Analytics. Then you can analyze your personal and/or business finances using Google Analytics powerful reporting and analysis UI. Remember: no PII.

Implementations Details

Below is a video explaining the method proposed in this article. In order to make the explanation clearer we used an example of a website that advertises online and direct visitors to a website where a discount coupon is offered (for purchases in a brick-and-mortar store).

Here are the steps required in order to implement the Universal Analytics solution (as explained in the video):

  1. Think profoundly about what information you need to collect in order to measure your goals. Read through the Measurement Protocol parameter reference to learn more about all the options available to you.
  2. Create a Google form and add one question to collect each data point that you will need. This will define which data will be collected and where it will be displayed on your Google Analytics reports.
  3. Copy the Script below and paste it into your Script Editor on Google Forms (find it on the "Tools" menu). Click Save.
  4. Edit the Script to match your data collection details (read the comments we left on the Script below for you). Click Save.
  5. Add a trigger for the Script to run every time a form is submitted. On the Script page, click on Resources and then on All your triggers. Click on No triggers set up. Click here to add one now and then click on save for the default trigger. You will need to provide permissions for the App, click OK.
  6. Set up Custom Dimensions on Google Analytics interface to work with the dimensions you added (if you decide to use Custom Dimensions). It might be helpful for you to read this article explaining what are custom dimensions.
  7. Make sure the relevant people fill the form every time data becomes available.
  8. Login to Google Analytics and have fun.

The Script

Below is the Script that you will need to copy to your Form. We tried to add as many comments as possible to guide you, but feel free to ask questions in the comments below if you have any.

var GA_TRACKING_ID = 'UA-xxxxxxxx-y';

// maps each form field to a field in GA
var data_mapping = {
  0: 'cid',   // User ID
  1: 'tr',    // Transaction Revenue
  2: 'ta'     // Transaction Affiliation
}

function trackForm(e) {
  var data = [],
      item,
      res = e.response.getItemResponses();
 
 
  for (var i=0; i< res.length; i++){
    item = res[i].getItem();
    if(data_mapping[item.getIndex()]) {
      data.push([
        data_mapping[item.getIndex()],
        res[i].getResponse()
      ]);
    }
  }

 
  data.push(

    ['tid', GA_TRACKING_ID],   // Uses the ID you provided in the beginning of the Script.

    ['v'  , '1'],   // The protocol version.

//  ['cid', Math.floor(Math.random()*10E7)],   // Remove the backslashes starting this line if you don't have a UID, this will set a random value.

    ['t'  , 'transaction'],   // The Hit type, must be one of the following: 'pageview', 'appview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'.

    ['ti', Math.floor(Math.random()*10E7)],   // Assigns a randon value to Transaction ID.

    ['z'  , Math.floor(Math.random()*10E7)]   // Cache Buster.

  );
 
  var payload = data.map(function(el){return el.join('=')}).join('&');
 
  var options =
   {
     'contentType': 'application/json',
     'method' : 'post',
     'payload' : payload
   };
 
  UrlFetchApp.fetch('http://www.google-analytics.com/collect', options);
}

Happy Analyzing! And let us know if you have any comments, questions or suggestions. Also, we would love to know how you are using the Form, leave a comment below.

Related Content

  1. Google Universal Analytics For Offline Behavior
  2. Google Universal Analytics: A User-Centric Approach
  3. Collect & Process Data Wisely [cartoon]

Subscribe To Our Newsletter For Monthly Updates



Your e-mail will be kept private

Online Behavior © 2012