Skip to content

Instantly share code, notes, and snippets.

@averyvery
Created October 3, 2012 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save averyvery/3824172 to your computer and use it in GitHub Desktop.
Save averyvery/3824172 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Easy GA debugging - to test, add ?ga_debug to the URL</title>
<script>
var _gaq = _gaq || [];
var _ga_debug = window.location.href.indexOf('ga_debug') !== -1;
_gaq.push(['_setAccount', 'UA-00000000-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
var start = 'https:' == document.location.protocol ? 'https://ssl' : 'http://www';
// load the debug version of GA if ?ga_debug is set
var file = (_ga_debug ? 'u/ga_debug' : 'ga') + '.js';
ga.src = start + '.google-analytics.com/' + file;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<a href="/support" data-track-event="Header|Click|Support">Support (Link)</a>
<a href="#" data-track-event="Video|Close Window">Support (Onpage action)</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="tracking.js"></script>
</body>
</html>
(function() {
var E = eventTracking = {
// track clicks on all 'data-track-event' links
init: function() {
$('body').on('click', 'a[data-track-event]', E.track);
_ga_debug && $('a[data-track-event]').each(E.highlight);
},
track: function(event) {
var $this = $(this);
var href = $this.attr('href');
var data = E.getData($this);
event.preventDefault();
_gaq.push(data);
E.goesSomewhere(href) && setTimeout(E.send(href), 100);
},
// parse data from a Piped|String|For|Tracking
getData: function($this) {
var data = $this.attr('data-track-event').split('|');
data.unshift('_trackEvent');
return data;
},
// does this link go somewhere, or is it an onpage anchor?
goesSomewhere: function(href) {
return href && href[0] !== '#';
},
// forward the user to the link url
send: function(href) {
return _ga_debug
? function() { window.console && console.log && console.log('-> User forwarded to ' + href) }
: function() { window.location = href; };
},
// style the links, add titles to create a simple tooltip
highlight: function() {
var $this = $(this);
E.lightOutline.apply(this);
$this
.attr('title', $this.data('track-event'))
.hover(E.darkOutline, E.lightOutline);
},
lightOutline: function() {
$(this).css('outline', '3px solid rgba(0, 200, 200, 0.5)')
},
darkOutline: function() {
$(this).css('outline', '5px solid rgba(0, 200, 200, 0.75)')
},
};
E.init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment