Inspecting and Debugging the Google Tag Manager dataLayer

the datalayer in action
ah data jokes…

The Data Layer Inspector and Debugger

This project began as a solution to a problem that I often faced setting up Google Tag Manager and specifically the data layer. The solution that I ended making was dataLayer logger, inspector, and debugger that offered the same features as the Google Analytics Debugger but for the data that was being held in or pushed to the dataLayer. It pretty-logs all the new contents of the dataLayer as it is pushed no matter if the dataLayer.push() happens on the page, in a script or in a custom HTML tag within GTM. Its all there as soon as it happens and it saves a fair bit of time.

datalayer debugger
Each object that is pushed to the dataLayer is logged like this.

One interesting difference between what I ended up with is that unlike the Chrome extension, this works across browsers (as far as I cared to test) and can be run as a standalone script, a custome HTML tag in GTM or even as bookmarklet. (Bare in mind I started this project about a month ago and, if you don’t already know, you will see why declaring that now makes sense shortly.) Also, Simo, if you ever read this, please integrate this into your Chrome extension.

The Google Tag Manager Debugging Problem

If you have ever worked with Google Tag Manager, you recognize that one of the most important, if not the most important feature of GTM is the dataLayer. This was a very conscious choice by the makers of GTM and other tag management companies to solve a problem that has arisen as websites have invariably become web apps with multiple players playing roles on a page, consuming and passing in data. That the data layer offered is a central stage for all the actors in the data game to meet and access the data that they require. It was an elegant solution to eliminated a lot of redundancy and has truly made data access and use a lot more agile.

The problem was that the Google Tag Manager interface, in its infancy was a bit less transparent than was ideal. GTM initially made claims that it would not require IT but that, for all but the most trivial cases was flat out untrue. In the beginning and still at the time that I write this, the process involves a fair bit of Javascript coding and hence, quality analysis and debugging that comes along with it. So if you have pushed to or pulled from the GTM dataLayer, you have entered this into your console more than once … many times more than one.

datalayer array
The tedious way to inspect the dataLayer.

You have inspected the dataLayer array and to see if that event was fired at the appropriate time or if all customer or event attributes made it through accurately. I did, quite often and that why I said, “These nine keystrokes, they can be automated.”

The Rabbit Hole of Problem Solving

Initially I thought it would be cool just to copy the style of the Google Analytics Chrome extension and just show every new push. But two things changed that initially course, this is not the same problem as that and I was getting really interested in the jQuery source. (Which is amazing if you every want to learn some really killer idioms.)

I learned about the Chrome Console API which as luck has it means I learned about the other cool Webkit browser, Firefox’s Console API. After that I realized I was half way there so I learned about decent but not quite as advanced Safari and IE console APIs. (Can you believe there are even cross browser considerations for the Javascript console!?)

So the main feature is just a nifty combo of the console.group method and string styling in the better two browsers to display the pushed object. This all goes on because the main change that the script makes is that it redefines the dataLayer.push method to wrap around the original GTM version of the dataLayer.push method. Once I got that far I had to ask myself why don’t I do this, why don’t I do that and so on until I ended up with a console API of sorts specifically for one, in my opinion conceptually significant, Javascript array. If you want to learn more about all the properties and methods of the Data Layer Debugger I will write a bit of an API reference for kick later. Otherwise the comments in the code do a decent job explaining the what everything does.

The Bookmarklet

If you want to try it out click the bookmarklet below and open up your Javascript console and click around a bit. You’ll see it working, hopefully as expected. If you want yours to keep, drag the bookmarklet into your bookmark bar and click it on any page with a dataLayer and watch the interaction take place.

dataLayer_debugger

The Custom HTML Script

Here is the GTM debugging script on GitHub. Its kind of useless now that the good folks at Google Tag Manager have baked this functionality in to the GTM interface. ( I like to think they saw my code on github and said, “that guy Trevor, he’s on to something…” Try it out, maybe you will find it useful.

Automate Mobile Preferred Ad Creation with Adwords Scripts

Scripts Save Lives

I love algorithms and you should too. They provide simple consistency to life like no calendar, routine or habit ever could.  They do exactly what you tell them to do and they do it fast, and most beautiful of all, they make life automatic. (sigh of contentment)

I also love javascript but sadly, there are not a lot of places where javascript and algorithms intersect. Scripts for Adwords is one of those happy unions and this delicious union of medium and method has a little cherry on top called automation. Ain’t it sweet!

Adwords Scripts are great for automating reporting, pulling info from other API’s into Adwords and routine account maintenance. The idea for this one was brought up to me by my Google Adwords Rep. Scott, who it turns out, is rad. He called me up one day mentioning how we could optimize one of my accounts. (To be read: “make more money selling ads for Google.”) He suggested that we add mobile preferred ads to all my adgroups with a click-to-call extension, suggesting that this could potentially increase conversion rates from mobile users who are better at calling then poking their screen.

“Lets try it!” I said then thought, “How can I automate this?”

As it turns out, its not fully automatable, as the Adwords Script API does not allow for extensions to be added to ads in the ad creation process. But we can automate our work most of the way there:

How it Works

This script mirrors the process that I went through with my Adwords rep:

  • Select the ad with highest click through rate in its adgroup
  • Copy the headline and URL’s for the selected ad
  • Make a copy with a historically successful call-to-action
  • Make a copy with a mobile optimized call-to-action
  • Set this ad to be mobile-preferred.

But instead of doing this one by one or in a spreadsheet, the script iterates through all adgroups in a the account, and makes the changes with one click. (Now you get why I love algorithms?)

There is one last manual step though. As I mentioned before, Scripts won’t allow for adding extensions or labels in the ad creation process. This means that you will have to go into the Adwords interface, create your click-to-call ad extension and apply it to all your ad groups. To be safe make sure that the call-to-click extension applies only to mobile ad extensions.

So here is the script the script can also be found on my github.

function main() {
  
  //set the following variables to customize the script to your account 
  
  var IMPRESSIONS_THRESHOLD = 100;       // min. impressions an ad needs to have to be considered
  var DATE_RANGE = "LAST_30_DAYS";       // time frame for impression threshold 
  var ADGROUP_NAME_CONTAINS = 'you specify';   // word or phrase that eligible adgroup name must contain
  
  var DESC_LINE_1 = "Free Shipping and Free Returns!";   // description line 1 (end with punctuation)
  var CTA_CONTROL = "Shop Latest Cool Styles Today";      // a historically succesful CTA for line 2
  var CTA_MOBILE = "Call Now To Place Your Order";       // new mobilized CTA to test for line 2
      
  // select adgroups that meet criteria defined above
  
  var adGroupSelector = AdWordsApp
  .adGroups()
  .withCondition("Status = ENABLED")
  .withCondition("CampaignName CONTAINS_IGNORE_CASE " + "'" + ADGROUP_NAME_CONTAINS + "'")
  
  var adGroupIterator = adGroupSelector.get(); 
  
  // iterate through all selected adgroups
  
  while (adGroupIterator.hasNext()) {
     
    var adGroup = adGroupIterator.next();
    
    var headline;
    var displayURL;
    var destURL;
    var optArgs = {
        isMobilePreferred: true
    };
    
    // select enabled ads that meet predifined criteria
    // ad with best ctr for each group will be copied
    
    var adSelector = adGroup.ads()
    .forDateRange(DATE_RANGE)
    .withCondition("Status = ENABLED")
    .withCondition("Impressions > " + IMPRESSIONS_THRESHOLD)
    .orderBy("Ctr DESC");    
    
    // this iterator does not really iterate, instead
    // it orders by CTR and selects ad with highest CTR
    
    var adIterator = adSelector.get();  
    while (adIterator.hasNext()) {
      var ad = adIterator.next();
      var stats= ad.getStatsFor(DATE_RANGE); 
      
      // headline and URLS are selected from ad
      
      headline = ad.getHeadline();     
      displayUrl = ad.getDisplayUrl();
      destinationUrl = ad.getDestinationUrl();
      Logger.log(headline +": " + stats.getCtr())
      break;     
    }
    
    // and are copied along with pre-defined description line
    // to create to versions of the mobile-prefered ad
    
    adGroup.createTextAd(
      headline,
      DESC_LINE_1,
      CTA_CONTROL,
      displayUrl,
      destinationUrl,
      optArgs
    )
  
    adGroup.createTextAd(
      headline,
      DESC_LINE_1,
      CTA_MOBILE,
      displayUrl,
      destinationUrl,
      optArgs
    )
  }
}

Automate MailChimp Reporting with Google Spreadsheets


I made it! I am finally a nerd. At the top of the MailChimp API there is a clear disclaimer: “Woah, Nerds Only!” Undeterred, I scrolled on to test my nerdy medal. The following is my account of navigating the well-charted waters of the MailChimp API and a tutorial on how to automate a “BARF” report using Google Apps Scripts and Google Spreadsheets.

First, let me tell you how this started. I love automating reports. Ok done explaining.

The MailChimp API is a Friendly API

MailChimp’s API is nice and clean with great documentation. It has well defined endpoints and returns JSON objects. Google Apps Scripts is great for parsing JSON objects thanks to the JSON class made specifically for that.

The problem as I defined it was as follows:

  1. MailChimp API has the data I want.
  2. I have to make a call to API using Apps Script’s UrlFetchApp
  3.  The call has to be authenticated by passing along API key.
  4. First I have to make a call to get all the campaigns/list endpoint to get a list of campaigns’ name/id info.
  5. Then I have to iterate through all the selected campaigns and pass their id to the reports/[whichever-stat-I-want] endpoints to get back each campaign’s stats.
  6. Format that data into an array and write it to the spreadsheet.

The Google Apps Script

Yup, so that’s exactly what I did. The interesting part for me was getting a better understanding of the UrlFetchApp class. I had used it before for getting data from APIs or webpages but only the simple way:

UrlFetchApp.fetch(“whateversiteiwant.com/data-from”)

But Mailchimp’s API asks for more. It asks that all requests be made as POST requests and it asks that the “apikey” and additional parameters be passed as JSON objects rather than a query string like: www.site.com/api?param=value.

This meant using the additional “param” parameter in the UrlFetchApp.fetch() method. I am happy that I got it to work. It’s clear in the code how it is used but I still need to know exactly why and how it works because it’s pretty cool. Expect a blog post on that.

Beyond that, the code is pretty self-explanatory. I commented parts that would be dodgy for codeacademy level folks. (I know. I was there not too long ago.)

Writing the report to a Google Spreadsheet

Check it out. It is explained in the comments.

// campaigns list api endpoint docs: apidocs.mailchimp.com/api/2.0/campaigns/list.php
// reports api endpoint: apidocs.mailchimp.com/api/2.0/reports/summary.php
// GAS docs: developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)
// mailchimp open tracking calculations: kb.mailchimp.com/article/about-open-tracking
  
// your api key can be found at: admin.mailchimp.com/account/api/
// standard "24 hour format in GMT, eg "2013-12-30 20:30:00" - if this is invalid the whole call fails"
// add formated values for start and end date like: var REPORT_START_DATE = "2013-12-30 20:30:00"

function chimpReport() {
  
  var API_KEY = 'yourapikeygoeshere';
  var REPORT_START_DATE;
  var REPORT_END_DATE;
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();  
  
  var dc = API_KEY.slice(-3);
  var api = 'https://'+ dc +'.api.mailchimp.com/2.0';
  var campaignList = '/campaigns/list.json' 
  var reports = '/reports/summary.json' 
  
  // MC api-specific parameters
  var payload = {
    "apikey": API_KEY,
    "sendtime_start": REPORT_START_DATE,
    "sendtime_end": REPORT_END_DATE
  }; 
  
  // GAS specific parameters: 
  var params = {
    "method": "POST",
    "muteHttpExceptions": true,
    "payload": payload
  };
  
  var apiCall = function(endpoint,cid){
    if(cid){
      payload.cid = cid
    }
    var apiResponse = UrlFetchApp.fetch(api+endpoint, params);
    var json = JSON.parse(apiResponse);
    return json
    }
  
  var campaigns = apiCall(campaignList);
  var total = campaigns.total;
  var campaignData = campaigns.data;
  
  for (var i=0; i< campaignData.length; i++){
      
    var c = campaignData[i];
    var cid = c.id;
    var title = c.title;
    var subject = c.subject;
    var send_time = c.send_time;
    
    // send_time values are only present for campaigns that have been sent. otherwise set to null.
    // this if statement will only call for report data and write to the spreadsheet, data from sent campaigns.
    
    if (send_time){
      
      var r = apiCall(reports,cid);
      var emails_sent = r.emails_sent;
      var opens = r.opens;
      var unique_opens = r.unique_opens;
      var clicks = r.clicks;
      var unique_clicks = r.unique_clicks;
      var open_rate = (unique_opens / emails_sent).toFixed(4);
      var click_rate = (unique_clicks / emails_sent).toFixed(4);
      
      // the report array is how each row will appear on the spreadsheet
      var report = [send_time, subject, emails_sent, opens, unique_opens, clicks, unique_clicks, open_rate, click_rate];
      
      Logger.log(report);
      
      // note that this method will append to the bottom of the spread sheet wherever that is.
      // to overwrite a specific range use setValues()
      
      sheet.appendRow(report)
    }    
  }
}

Google Result Date Bookmarklet Using “&as_qdr=y15″

I have been goofing around a lot with JavaScript lately. It’s fun to explore because every time I find and open a new door of understanding, there are two more doors to open and explore. I am currently in the room of browser functionality and thats where I found my bookmarklet.

Making a bookmarklet was an easy way to put a bit of code into action without building anything too big. As it turns out, it’s surprisingly useful. If you have ever wondered when a web page was published, it will tell you, (fairly accurately) just that.

A bookmarklet is essentially a bookmark that holds a bit of JavaScript code that a web browser, like Chrome or Firefox, will execute. Just like any bookmark it is a bit of information held within a link. A link is which is just a reference), but instead of referring to a web page, it refers to some instructions coded in JavaScript. The browser knows the code lingo and does exactly what it is instructed to do.

The instructions look like this (Javascript and HTML translated to plain English):

 

<a href='javascript:

“Hey browser, this looks like its a link, but no, here comes some JavaScript instructions.”

(function(){if(location.hostname === "www.google.com")

“This is the beginning of the Javascript function: IF the user is on google.com…”

{window.location.href=document.URL+"&as_qdr=y15"}

“THEN: take the page the user is on and change it (make it EQUAL to) the page they are on ADD ON ‘as_qdr=y15′.” (That is the instruction for Google to tell you the dates of all the pages on the results page.)

else{alert("This only works on Google Searches")}

“If the person is not on google.com, ELSE pop-up an alert window and tell them the bad news.”

})()'>Google Date Finder</a>

“Finally, end the Javascript code. Specify the text that anchors the link. End the link code.”

When its all done it looks like this:  

<a href=’javascript:(function(){if(location.hostname === “www.google.com”){window.location.href=document.URL+”&as_qdr=y15″}else{alert(“This only works on Google Searches”)}})()’>Google Date Finder</a>

And amounts to this with the super-sexy CSS styled version below:

Google Date FinderGoogle Date Finder

Just drag one of the links into your bookmark bar, make a Google search, click the “Google Date Finder” bookmarklet and Whammy! each one of the links has a date on it.

Event Tracking on Squarespace Using Google Tag Manager

I am in “like” with
Google Tag Manager and Squarespace
for the same reason: they simplify and
streamline the processes website design and Google Analytics implementation
while maintaining power and functionality of the product.  For the old sliced bread analogy, they are
the greatest things to come along since the knife.

Squarespace is a
great plug-and-play, drag-and-drop Content Management System (CMS). It allows
users to easily add all kinds of content in a simple non-technical way. For the
average user, Squarespace has all but rendered WordPress, the old standard CMS,
obsolete.  But that argument is a
different conversation. Google it later.

Google Tag Manager is
a Tag Management System for Google Analytics
. (You could say TMS but nobody
does.) It allows for on-page tagging and most other advanced analytics
capabilities of Google Analytics. It greatly minimizes technical implementation
and actual code that must be added to a website in order to track user
interaction. Its semi-technical, in fact, its still pretty technical but once
the code is implemented by your friendly webmaster or neighborhood hacker, the
backend is pretty accessible to fairly tech savvy person.  I recommend checking out this short Google
Tag Manager video
 to get a basic understanding of its features and
capabilities. When I watched it, it blew my mind, but then again, I love
analytics.

“Stepping Into”
Google Tag Manager

That clever heading is a JavaScript joke. Don’t worry if you
don’t get, I wouldn’t have either until this year.  If you do, then you are in pretty good shape
to follow this guide.  To get you up to
speed, here are a few great references
to learn what’s going on with the technical side of the following
implementation.

If you were to do this from scratch, you would need a basic
understanding of JavasScript, HTML, CSS and JQuery. Actually, since Squarespace
uses YUI instead of JQuery you will have to also understand YUI. I’ll explain later. You can learn to code at codeacedemy.com. They offer a great step-by-step method that makes learning to
code fun and fulfilling. I really did enjoy it.

For the YUI to JQuery translation, use jsrosettastone.com.
It does exactly what it sounds like: lists translations of all the
functionalities of JQuery into YUI.

For help references use Google Tag Manager Help, Squarespace Developer Center and the Holy Grail: stackoverflow.com if you ever get
hung up.

Step 1: Get Your Google Tag Manager Account and Container

Now that you are a technically proficient hack, let’s dive
in!

This has been covered many times so I will let Google take
it from here. Go to Google
Tag Manager’s website
and follow steps 1-3. Do not follow step 4! Do it
yourself or hire me instead!

Also check out Justin Cutroni’s extremely helpful guide  for setting up your account.

Step
2:  Insert the Container
and Initialize the Data Layer

 

This is where the fun starts; the beginning of analytics
tracking glory!

Inserting the
Container:

Google Tag Manager documentation recommends that you place
your container just below the opening body tag. One way to do this is by adding
the container into a code block at the top of each page, but that is just a lot
of work. Instead, the functional alternative, which works just as well, is
“injecting” the container in the header section.

Initializing the Data
Layer:

The container is the brain that runs Google Tag Manager on
your site but it doesn’t really do anything too special without the data layer.

You will also have to insert a bit of code above the
container to initialize the data layer. Technically, this bit of code creates
an empty data layer object, which will be filled as user interaction push
information to the data layer object. The code looks like this:

<script>
    dataLayer = [];
</script>

<!-- Google Tag Manager -->

 **Make sure that you place the data later snippet above the container!**

Step 3: Insert
Code to Push Events to the Data Layer

 

This
method of event tracking offers the agility that differentiates GTM from
previous methods of Google Analytics event tracking. It is truly where the magic happens.

Inject this snippet of javascript into the footer. This way
loading the script will not slow down loading the visual elements of the page.

This snippet of code says, “every time a user clicks a link
to a social media site, I will push the corresponding key/value pair to the
data layer.  Most commonly this is done
using JQuery, but Squarespace uses YUI so you have to play the game between the
lines.

<script>

 YUI().use('event', 'node', function(Y) {
   
   // (1) These variables reference the 'class' link attribute. 
   // Each social media site link has its own 'class' attribute.
   // Note that by using an element class rather than element id,
   // any button that shares that class will fire the GTM rule 
   // triggerd by that class.    
   
   var twitterLink = Y.all('a.social-twitter');
   var gPlusLink = Y.all('a.social-google');
   var linkedInLink = Y.all('a.social-linkedin');
   
   // (2) The 'socialView' function tracks a button click event (2a) 
   // then pushes a corresponding data layer variable to the data layer. (2b)
   
   var socialView= function(socialLink,socialClick){
     socialLink.on("click", function(e){                //(2a)          
       
       dataLayer.push({'event': socialClick});          //(2b)
       
       //These are logs I used for debugging:
       console.log(socialLink + " has been clicked");    
       console.log(socialClick + " was pushed to the datalayer");
   })
 }
   // (3) Finally, 'socialView' (2) is called for each link class variable (1).
   // This pushes a data layer value (2b) into the "key : value: pair below.
   
  socialView(twitterLink, "twitterClick");
  socialView(gPlusLink, "gPlusClick");
  socialView(linkedInLink, "linkedInClick"); 
   
});
</script>

Step 4: Setup Tag
Manager Rules and Tags

The Google Tag Manager interface is not immediately
intuitive so give yourself some time to play around with it. Once you get lost
in it, you will be amazed by the power it provides.

For this example, we will go through event tracking on the
Google+ button on the footer of each page. This will also track any button
click for any other button with a designated div “class” attribute of  “social-google.” I use the “class” attribute
because my intent is to measure navigation to my social media profiles rather
than on-page UX button testing. This can be modified pretty easily to track
other events. Let me know if you have questions.

Part 1: Setup the Tag

This tag measures both a Virtual Pageview and a Custom Variable. This way its possible to track
navigation to my Google+ page in aggregate and also at the visitor level.

Part 2: Setup the
Rule to fire the Tag

We are tracking a JavaScript event, not to be confused with
a Google Analytics event so we use the {{event}} macro to trigger the rule.
When the  {{event}} matches the string
assigned as the data layer variable,

Note: The {{event}} macro must exactly match thedata layer and sure that the match is
case sensitive!

In the case of the Google+ button, the data layer variable
is “gPlusClick.”

Part 3: Create a Container Version 

Create a version of your container. Name your container
version with a specific and consistent naming convention. This will really help
in keeping track of progress and contained tags.

Step 5: Debugging and Google Analytics Real-Time Feature

Good job so far!

Now, to makes sure this is all working properly, preview the
new container version. There are few ways to check if your tags are working.
First preview your container in “Preview and Debug” mode. If the tags are
firing on the target user interaction publish the new container and use the
Real-Time feature in Google Analytics to make sure that the virtual pageviews
and/or events are being reported properly.

If you followed this whole process or you managed to
customize your own event tracking, give yourself a pat on the back! If you have
any questions, please comment below or reach me on Google+.

 

Hopped Up on the Hillsboro Hops

I am so glad that baseball is coming back to this neck of the woods! I can’t tell you how bitter I was when I found out that major league soccer would be playing in the old PGE Park. The place was old but well worn  like my high school baseball hat. The place still keeps the ghosts of a venue where people would dress in suits to go to the game, pitchers threw high and tight, and the only performance enhancers were that extra cup of coffee and topical horse anti-inflammatory.

I miss the majesty of that old ballpark, but as hard as it is for me to admit, its time had passed. I stopped wearing that old baseball hat too.

The Hops hats aren’t available yet, but you can bet as soon as I can get my hands on one, I will be showing my Hillsboro Hops love.. Hillsboro is the perfect place for a Northwest League, Short Season Single A Baseball team. Hillsboro is the just edges out Beaverton as being Oregon’s 5th largest city with nearly 90,000 residents. The market is perfect. Its a young and expanding suburbia full of kids who will attend their first baseball game wondering what the heck a Hop is and why the mascot looks like a cross between Oscar the Grouch and Big Bird. To this day I remember my first single A game. I saw the Eugene Ems and my friend tried to convince me that a grand slam was a ball that was hit out of the stadium and I asked for the bat boys autograph after the game.

I can already see it, Kids standing up, shakin’ and dancin’ to “Danny and the Jurors’ “At the Hop” as the mascot bounces his big grinning hop bud body. And this is how families fall in love. Mom is happy that the kids are happy. Dad is happy to be sharing America’s pastime with kids and maybe even prompting a game of catch. They’re both happy because they can sit back and enjoy a beer as the kids bounce around the, the inflatable, you guessed it, “Hoppy Castle.”

Did someone mention beer? I’m counting the days until Thirsty Third Thursday. A day that will pull yuppies and hipsters on MAX trains to Hillsboro from the Pearl to Alberta and as far as Gresham. Local music plays before the game and a lively social beer deck doesn’t watch the game. And as for the beer batter, are you kidding me!? I can already hear the music playing after the second strike. Fans ask each other what the featured beer is this week. 1st strikeout, one dollar off; second strike out, two dollars off, and the magical hat trick makes beer half priced!

The Hillsboro Hops was a great idea. I can’t wait to see what life it takes on. Please subscribe to my RSS feed to keep up on latest news, commentary and profiles from the team.

Heres to the Hops!

 

On the Web… on the Web