Google Tag Manager
Avoiding Anti-Patterns
Advanced Auto-Event Tracking
@fastbloke #SPWK
<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>
<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>
<a href="http://www.example.com" onclick="_gaq.push(blah);"> Click Here</a>
Confession
I <3
Why?
Why?
The temptation of the dark side is strong
Not True!
5 minutes developing without thought now = hours of pain later
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
Coming up...
Ground rules
Example
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>
Use Naming Conventions
Use Naming Conventions
Tag Naming Conventions
Rule Naming Conventions
Macro Naming Conventions
Look where we’re going...
Avoid anti-pattern usage
Current Auto Event Tracking
Click Listeners
Click Listeners
Link Click Listeners
Problem
Problem
Meh...
Problem
Challenge
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
Preserving ‘gtm.element’
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);
Extending Listeners - example
Extending Listeners - example
Extending Listeners - example
Extending Listeners - example
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 |
Testing GTM
Injecting a container tag
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 -->
(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
chrome.tabs.executeScript
GMT Injector Extension
The Container Tag Injector
In summary
BUT
Questions?
FIN