Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmolsen/398492 to your computer and use it in GitHub Desktop.
Save dmolsen/398492 to your computer and use it in GitHub Desktop.
$(function(){
$('#jqt').ajaxComplete(function(){
add_ga(); // add google analytics on each page load
});
$(document).ready(function() {
add_ga(); // add google analytics to the first page
});
function add_ga() {
$('a').each(function(){
if (!$(this).hasClass('ga')) {
if ($(this).attr('data-ga')) {
//$(this).click(function(){console.log('had data ga click 2');});
$(this).click(function(){pageTracker._trackPageview($(this).attr('data-ga'))});
$(this).addClass('ga');
} else {
if ($(this).attr('href') == '#') {
//$(this).click(function(){console.log('back button 2');});
$(this).click(function(){pageTracker._trackPageview('/'+$(this).attr('href')/+'/')});
$(this).addClass('ga');
} else {
//$(this).click(function(){console.log('internal link 2');});
$(this).click(function(){pageTracker._trackPageview($(this).attr('href'))});
$(this).addClass('ga');
}
}
}
});
}
});
@dmolsen
Copy link
Author

dmolsen commented May 12, 2010

Adds an onclick handler to all links without a class of ga in a jQTouch project. Will also add to any new regions added by AJAX. There is a bug somewhere as I have a NaN in Google Analytics results but mainly it appears to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment