There has been a previous Question about using Google Analytics with jquery Mobile with a successful answer provided here. However this was for versions of jQuery prior to 1
The code Google gives on the Tracking Info page you will look something like this, depending on what features you've chosen:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setDomainName', 'foo.bar']);
_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);
})();
And you're told to put that immediately after the opening . You should keep it in the same place with jQuery Mobile (tested on v1.3) by removing the
_trackPageview
line and adding a simple snippet to the end of that code block, giving:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setDomainName', 'foo.bar']);
(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);
})();
$(document).on('pageshow', ':jqmData(role="page")', function(evt, data) {
var url = $(this).jqmData('absUrl');
try {
if (url) {
url = $.mobile.path.parseUrl(url)
_gaq.push(['_trackPageview', url.pathname + url.search + url.hash]);
}
else {
_gaq.push(['_trackPageview']);
}
}
catch(e) {}
});