Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Created January 17, 2013 23:13
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 dalethedeveloper/4560788 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/4560788 to your computer and use it in GitHub Desktop.
More robust Google Analytics tracking for Zoey (http://blog.angeloff.name/zoey/), in particular treating single-page <article> tags as pageviews. Tucking the view under the /mobile/[panel id] for simpler segment/search.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22762790-1']);
// DELAY _gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
// Delegate these to events in Zoey
$(document).ready(function(){
$(document).delegate('article','pageshow',function(){
if (typeof _gaq !== "undefined" && _gaq !== null)
_gaq.push(['_trackPageview', '/mobile/' + this.id ]);
});
$(document).delegate('a[rel="external"]','click',function(event){
var href = $(this).attr('href');
if (typeof _gaq !== "undefined" && _gaq !== null)
_gaq.push(['_trackPageview', '/mobile/outbound/' + href]);
});
if( window.location.hash && $(window.location.hash).length == 0 )
window.location.hash = '#home';
// Delay Analytics until now, track initial view (rest will delegate to 'pageshow')
if (typeof _gaq !== "undefined" && _gaq !== null)
_gaq.push(['_trackPageview', '/mobile/' + ( window.location.hash.length ? window.location.hash.substr(1) : '')]);
// Pretty loading graph
$(document).delegate('article','pagebeforehide',function(){ Zoey.loading(true); });
$(document).delegate('article','pageshow',function(){ Zoey.loading(false); });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment