• Tracking Clicks and Impressions In Google Analytics

    by  • Last modified:July 1, 2015 • Google Analytics • 83 Comments

    June 2015:
    This code was a proof of concept. You should be using Universal Analytics now, and this code, but more importantly, you really should use DoubleClick by Google to do this. It’s a much better way to track your internal campaigns. For this reason, comments are now closed.

    Internal Banner Ads and Google Analytics
    One thing of the many things that Google does well is integrate with Google Adwords. No other Analytics package can really compare with the ease to which it does this because they are so well inter-linked. So, to me, it has always been a shame, Google Analytics does not come with an easy way to track on-site banners or advertising. For example if you want to calculate how many clicks a banner has on one of your pages or if you want to know how many impressions an advert has, you are basically out of luck. If you have a Magento Enterprise site with banner ads or if you have a simple site that sells space you are pretty much have couple of choices: Install an AdServer, like OpenX or some other service. The reality is, though, you have Google Analytics for a reason: To track user behavior. Why now pile another tool into the mix?

    Tracking Banner Clicks and Impressions with Google Analytics
    With some creative tinkering and leveraging Event Tracking, I am proud to share with you a great way to track banner ads for your site. Not only can you get the number of clicks (which you could already get), I have found a way for you to also calculate the number of impressions of a banner. Also with the beauty of events, you can assign a value, so you can charge a cost per impressions or a cost per click and easily create an easy way to charge your customer. Even better with Event tracking you can create goals, custom reports, filter to a profile and even more.

    Below is the code that does all the magic

    <a href="http://localhost/someurl" onClick="_gaq.push(['_trackEvent', 'Banner', 'Click', 'BANNERNAME',1.00,true]);" > <img style="border: 0px" src="http://localhost/someurltoapic.jpg" alt="SOMEALTEXT" /></a>
    <img width=0 height=0 src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="_gaq.push(['_trackEvent', 'Banner', 'Impression', 'BANNERNAME',2.00,true]);"/>

    What does the code do?
    The first bit of the code, you want to replace “http://localhost/someurl” with the link you want users to visit. Then we hit the event tracking portion which looks like this

    _gaq.push(['_trackEvent', 'Banner', 'Click', 'BANNERNAME',1.00,true]);

    As you recall from an earlier article, an event is composed of few bits of information:

    _gaq.push(['_trackEvent' ('category', 'action', 'label', 'value', 'noninteraction']);

    So for us, it works as the following:

    • Category will be “Banner”
    • Action label for this is “Click”
    • Label should be the “Banner Name” and possbily the location of the ad (top) or size (728×90)
    • Value can be left blank or 0. If you like, it can also be your cost per click (CPC)
    • Non-Interaction, we have set as true. As we do not want this interaction to interfere with the bounce rate. As Google notes.

    The next bit of code tracks the impression, by sending an event when a fake transparent gif on load. The code looks as follows

    _gaq.push(['_trackEvent', 'Banner', 'Impression', 'BANNERNAME',2.00,true])

    So for us, it will almost be same as the set up for clicks:

    • Category will be “Banner” like before
    • Action label for this is “Impression”
    • Label should be the same “Banner Name” as before, as well as possibly the location of the ad (top) or size (728×90)
    • Value can be left blank or 0. If you like, it can also be your cost per cost per 1000 (CPM)
    • Non-Interaction, we have set as true.

    Once done it will provide you a lovely report in Google Analytics like below:

    click-impression-google analytics

    Now you can create a custom report, goals, email reports, or even a profile filtered just for ads.You can even get more creative with array and rotating banner, and get totally rocking with tracking banner ads and impressions. To help your creative juices, I have come up with a script that does just that. You can get it for free right here.

    A couple of final words
    While I don’t think it should be an issue for most site, for high traffic site I recommend you read Google Analytics Implementation Considerations about events. In short, Google Analytics records, the first 10 event hits in a session sent to Google Analytics are tracked immediately. Thereafter tracking is rate limited, so it can only record one event hit per second. As the number of events in a session approaches the collection limit, additional events might not be tracked. For this reason, I also recommend you check out my implementation for this solution in Piwik. With Piwik, you can use their PHP API to create a lovely report including CTR and customize it to your needs.

    I should also add one piece of information that has caused confusion. This solution only works on your site. Tracking impressions for your banners on other websites will not work. This code works by sending the data to your Google Analytics install. It will not work on third party sites. To track this behavior outside of your website you will need an adserver.


    About The Author: Adrian has over 12 years experience in Digital Marketing and Analytics. He currently works as a Marketing Head of Community at Vanilla Forums, a modern forum software platform that allows clients to connect and engage their communities and customers. Adrian has extensive experience with Google and Piwik (Matomo) Analytics. He lives and works in Montreal.