1 of 48

Google Tag Manager

Avoiding Anti-Patterns

Advanced Auto-Event Tracking

@fastbloke #SPWK

2 of 48

    • Tagging…

3 of 48

    • Tagging…
    • It’s moved on from the early days

4 of 48

    • Tagging…
    • It’s moved on from the early days

<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>

5 of 48

    • Tagging…
    • It’s moved on from the early days

<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>

6 of 48

    • Tagging…
    • It’s moved on from the early days

<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>

7 of 48

Confession

8 of 48

I <3

9 of 48

Why?

      • Measurement with NO CODE
      • Tagging
        • Speed
        • Accuracy
        • Quality
        • Reduced risk
        • Less time
      • More time for REAL analysis

10 of 48

    • This is still possible

11 of 48

Why?

The temptation of the dark side is strong

    • 5 minutes developing = job done?

12 of 48

Not True!

5 minutes developing without thought now = hours of pain later

13 of 48

Regular Face Palms

Key DOM elements with no id

Generally poor markup

Forms that post to themselves

Build THAT goal!

Site search with no keyword

Fail to see on site search metrics

Auto firing events

Bye bye bounce rate

TMS misuse

Using GTM to fix site behaviour to improve measurements

14 of 48

Coming up...

  • We’ll see how GTM doesn’t do some of the things we want it to
    • Auto-event tracking
  • We’ll see how we want to do certain things with GTM in a certain way
    • Development
    • Debugging

  • We’ll see how to do these things
    • the right way
    • without causing future pain, suffering and expense

15 of 48

Ground rules

      • Establish some best practices
        • THINK about measurement upfront
          • During the design & build phase
        • Naming convention
        • Don’t abuse your TMS
        • Use the measurement layer to measure
        • Don’t use the measurement layer to fix your site
        • No code is better than no code
          • Avoid custom HTML tags

16 of 48

Example

      • Good markup
        • clean
        • lightweight
        • SEO
        • Measurement
          • Think in page analytics
          • Uses unique element ID
          • Uses unique parent element ID
      • Think this through early
      • Do it RIGHT
      • Extract massive value from this diligence

17 of 48

Example

<a class="action secondary" href="http://shop.mysite.com/">Buy now</a>

<a class="action secondary"

rel="Name and category of the product" href="http://shop.mysite.com/">Buy now</a>

18 of 48

Use Naming Conventions

  • You will quickly build a large repository of
      • Tags
      • Rules
      • Macros
  • Maintenance and Management
      • Ensure only required entities and the right entities are used
      • Find what you need to use quickly and easily
  • Testing
      • Help the testing team
      • Help yourself
  • Re-use assets!
      • Don’t reinvent the wheel
      • Think DRY
  • Be specific
      • Don’t write an essay!
      • Including reams of detail is counter productive

19 of 48

  • Accounts
    • Name the account according to the site

  • Containers
    • Name the container according to the site and environment

Use Naming Conventions

20 of 48

  • Search by tag type - GTM
  • Search by functional area - Homepage
  • Search by tag function - click

Tag Naming Conventions

21 of 48

  • Search by functional area - Homepage

  • Search by rule function - URL

Rule Naming Conventions

22 of 48

  • Macros - typically general usage
  • Use macro name to explain usage - general or specific
  • Use macro name to explain macro type
  • Define variables/constants
  • Closely related to methods/variables

Macro Naming Conventions

23 of 48

Look where we’re going...

24 of 48

Avoid anti-pattern usage

  • GTM - <2 years old
  • SERIOUS pace of change
  • Not all features supported
      • We need to extend GTM in specific areas
  • Adopt intelligent design patterns now
  • Don’t bake in anti-patterns
  • Smart design now = fewer headaches later

25 of 48

      • Automatic tracking for
        • Clicks
          • Links
          • Buttons
          • Any DOM element
        • Form submissions
        • Timing events
          • Polling

Current Auto Event Tracking

26 of 48

      • Automatically decorate your page(s) with click listener
        • No jQuery (or similar) dependency
      • Any DOM element
      • Use on all pages or fire/block based on rules
      • Use rules to discriminate between element clicks
        • (complexity!)

      • Pattern
        • Build Click Listener Utility tag
        • Avoid jQuery dependency

Click Listeners

27 of 48

      • Utility Click Listener

Click Listeners

28 of 48

      • Automatically decorate your links with click listeners
      • More specific
      • Less flexible

      • Pattern
        • Build Click Listener Utility tag
        • Avoid jQuery dependency

Link Click Listeners

29 of 48

      • What happens on drop downs?

Problem

30 of 48

      • Can’t use linkClick for obvious reasons
      • Click
        • Highly specific rule needed
        • gtm.element.selectedOptions.0.label = currently selected option - not the option being selected

Problem

Meh...

31 of 48

      • What about
        • hover?
        • change?
        • keydown?
        • mouseout?
        • etc.
      • We need more event types to be handled

Problem

32 of 48

      • Adding support for other events is quite easy
        • jQuery
      • Doing this the lazy way is not good
        • Baking in anti-patterns!
      • Brian Kuhn works fast…
      • Chances are they’ll build a solution quickly
        • Adopt this change when it becomes available
        • Do so cleanly
        • Think about it now to reduce effort & risk later

Challenge

33 of 48

Extending Listeners

//no changes here

function addListener(element, type, callback) {

if (element.addEventListener) element.addEventListener(type, callback);

else if (element.attachEvent) element.attachEvent('on' + type, callback);

}

//change as required

var myLinks = document.getElementsByTagName('a');

linkIndex=myLinks.length;

while(--linkIndex >= 0){

addListener(myLinks[linkIndex],'mouseover', function(){

customEvent = {

"event": "gtm.mouseover",

"gtm.element": this,

"gtm.elementClasses": this.className,

"gtm.elementId": this.id,

"gtm.elementTarget": this.target

};

dataLayer.push(customEvent);

});

};

As used in Google documentation for Action Script event listeners

Custom HTML tag template - change as required

34 of 48

    • A gold mine of information
    • Good markup will be decorated with relevant and appropriate meta data to aid measurement
    • Access meta data here
    • Be aware of traversing too far
    • More traversal = more risk

Preserving ‘gtm.element’

35 of 48

    • Use customEvent data structure to make gtm.element available
    • Harder to do this cleanly & consistently using jQuery
    • Used in gtm.js

Preserving ‘gtm.element’

customEvent = {

"event": "gtm.mouseover",

"gtm.element": this,

"gtm.elementClasses": this.className,

"gtm.elementId": this.id,

"gtm.elementTarget": this.target

};

dataLayer.push(customEvent);

36 of 48

Extending Listeners - example

37 of 48

Extending Listeners - example

38 of 48

Extending Listeners - example

39 of 48

Extending Listeners - example

40 of 48

  • New event listeners will be supported in the future
  • What happens when they are?

Extending Listeners - the future

Anti-patterns used

Design patterns used

Upfront thought and effort

5 minutes

1 hour

Thought and effort after

DAYS!

< 1 hour

  • Anti pattern
    • Removal of jQuery - custom HTML
    • Build new listener
    • Changes in multiple tags
  • Design pattern
    • Tweak listener - 1 tag to update

41 of 48

    • Adding tags - fast
    • Testing tags - rigor and discipline required
    • How did we test & debug prior to GTM?
      • Charles
      • Fiddler
      • Live HTTP Headers
      • Web Developer toolkit
      • Chrome debugger
      • WASP
      • etc.
    • All still valid
    • More support from GTM
    • Test tags, rules and macros without deploying containers

Testing GTM

42 of 48

      • You’ve developed a tag
      • You want to test it on a client site
      • They’re slow/reluctant
        • no container tag...
      • How do you get the container in place to prove the concept?
      • Chrome extension...

Injecting a container tag

43 of 48

The Container Tag again

<!-- Google Tag Manager -->

<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-KDV4BF"

height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':

new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=

'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);

})(window,document,'script','dataLayer','GTM-KDV4BF');</script>

<!-- End Google Tag Manager -->

44 of 48

(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':

new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=

'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);

})(window,document,'script','dataLayer','GTM-KDV4BF');

The Container Tag again

    • It’s just a chunk of javascript…
    • So just execute it

chrome.tabs.executeScript

45 of 48

GMT Injector Extension

46 of 48

The Container Tag Injector

  • White noise
  • Timing
  • Proof of concept
  • Debug with caution
  • Choose GA account carefully
    • Let me know if you want to use it...

47 of 48

In summary

  • GTM is a great tool
  • It’s not perfect
  • We keep changing the rules
  • We keep moving the goal posts
  • We can keep up with demands by extending GTM

BUT

  • Correct use of GTM is essential to success
  • Use sound software development principles
    • Use design patterns
    • Don’t bake in anti-patterns
    • Apply thought now to save time later
    • Saving time means saving money
    • Less time wasted on tagging is more time to spend on analysis
  • Use Chrome Extensions to supercharge the tool

48 of 48

Questions?

FIN