Created
February 28, 2014 18:27
-
-
Save bensochar/9276781 to your computer and use it in GitHub Desktop.
GA Tracking Code via data attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global _gaq:false */ | |
var H5BP = H5BP || {}; | |
// Google Analytics event tracking | |
H5BP.track = (function () { | |
'use strict'; | |
$(function () { | |
H5BP.track.init(); | |
}); | |
function _init() { | |
var $rootEl = $('body'); | |
$rootEl.on('click', '[data-ga-category]', function (e) { | |
var $target = $(e.currentTarget); | |
var category = $target.attr('data-ga-category') || undefined; // required | |
var action = $target.attr('data-ga-action') || undefined; // required | |
var label = $target.attr('data-ga-label') || undefined; | |
var value = parseInt($target.attr('data-ga-value'), 10) || undefined; | |
if (category && action) { | |
_event(category, action, label, value); | |
} | |
}); | |
} | |
function _event(category, action, label, value) { | |
//console.log(category + " | " + action + " | " + label + " | " + value); | |
if (_gaq) { | |
_gaq.push(['_trackEvent', category, action, label, value]); | |
} | |
} | |
function _social(network, action, target) { | |
//console.log(network + " | " + action + " | " + target); | |
if (_gaq) { | |
_gaq.push(['_trackSocial', network, action, target]); | |
} | |
} | |
function _page(url) { | |
//console.log(url); | |
if (_gaq) { | |
_gaq.push('_trackPageview', url); | |
} | |
} | |
return { | |
init : _init, | |
event : _event, | |
social : _social, | |
page : _page | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment