How I track banner impressions using Google Tag Manager and Universal Analytics

How I track banner impressions using Google Tag Manager and Universal Analytics

Say you want to track banner impressions or views of a piece of sponsored / featured content on your awesome blog. It doesn't matter what the piece of content is as long as its distinctly identifiable by an id or class. We want to measure impression as an event in universal analytics as soon as the page loads.

Doing this with Google Tag Manager is pretty straight forward here is what we need. Assuming this is the sponsored content we have

<div id="sponsored-content" data-sp-content="paypal" data-sp-location="sidebar">
    <h1>Never Miss a Transaction!</h1>
    <p> 100% accuracy of paypal transactions in google analytics
    <a href="">Beta Signups Open</a>

I want to see whenever this code block is displayed on a page. The easiest way to do so is look for the block whenever a page is loaded. We can do this by including this code with the google analytics pageview tracking code already being triggered.

ps. The paypal tracking thing is completely real and awesome if your sites uses paypal check it out here.

Detecting Banner To do this create a new js macro. Name it detect sponsored content Include the following code

  return function(){
    // Check if the page contains the div
    var node = document.getElementById("sponsored-content"); // use whatever id your block has
    isInBody = document.body.contains(node);
    // If true
    if (isInBody){

Now add this to your Universal Analytics Pageview tag. Field to Set hitCallback set value to {{detect sponsored content}}

Now whenever the pageview tag loads and detects the sponsored content it will fire an event. What we want to do is use the gtm event to trigger an event in GA.

We will extract the details about the content name / location etc and pass it to google analytics. Here is where our weird sounding data-sp-content and data-sp-location come in handy. We need two more custom js macros to do this

Macro 1 Name the macro detect sponsored name

    var contentId = document.getElementById("sponsored-content"); // id of our sponsored block
    var contentName = contendId.getAttribute("data-sp-content"); //custom attribute name
    return contentName;

Macro 2 Name the macro detect sponsored location

    var contentId = document.getElementById("sponsored-content"); // id of our sponsored block
    var contentLocation = contendId.getAttribute("data-sp-location"); //custom attribute name
    return contentLocation;

Setup another Tag in GTM Universal Analytics - Event - Sponsored Content Now we want to dynamically populate data about which piece of content it was into the event.

Event Category: Sponsored Impression Event Action: {{detect sponsored name}} Event Label: {{detect sponsored location}}

Opt Interaction: True (we dont want this affecting bounce rate).

Set the rule for tag to {{event}} equals spContent-detected

Now using this we can fire events whenever we want to track impressions of a piece of content. Remember the piece of content can be anything your hello bar, side bar white paper, some plug within your content (like my paypal example) or various offers in a long sales page.

Some stray observations

We can use the same method to trigger an enhanced ecommerce promotion impression as well.

Another alternate way maybe to fire a custom metric + custom dimension combo at the page level.

Lastly a good way to make banner impressions tracking a bit more accurate could be to fire them when they actually come in view. There is a cool jquery library that can help with that.

Let me know how you end up using this.

ps. This post is inspired by a question on G+ Google Analytics community

Hussain Mehmood

Hey I am Hussain Principal Analytics Consultant @ MarketLytics focusing on measurement strategy and analytics implementation. I started marketlytics in 2010 to turn my passion for understanding user behaviour into my day job. I use & extend google analytics, tag manager and kissmetrics everyday to deliver performance measurement & data driven insights.