jQueryMobile loads its first page as every site does. The usual Google Analytics integration works - the request ist tracked. But following pages are loaded asynchronously and user clicks are not tracked.
How to integrate Google Analytics into a jQueryMobile website, so that all page clicks are tracked?
jQueryMobile loads its first page as every site does. The usual Google Analytics integration works - the request ist tracked. But following pages are loaded asynchronously and user clicks are not tracked.
How to integrate Google Analytics into a jQueryMobile website, so that all page clicks are tracked?
Share Improve this question asked Jan 13, 2012 at 9:46 WitekWitek 6,4627 gold badges48 silver badges68 bronze badges 1- 1 Here's an updated answer with correct event binding, using jQuery Mobile 1.3 – Walf Commented Sep 11, 2013 at 8:30
3 Answers
Reset to default 15 +50Jon Gales has written a great article on this.
http://www.jongales.com/blog/2011/01/10/google-analytics-and-jquery-mobile/
Here's the code he recommends using:
$('[data-role=page]').live('pageshow', function (event, ui) {
try {
_gaq.push(['_setAccount', 'YOUR_GA_ID']);
hash = location.hash;
if (hash) {
_gaq.push(['_trackPageview', hash.substr(1)]);
} else {
_gaq.push(['_trackPageview']);
}
} catch(err) {
}
});
Update
Since live
is now deprecated you should use the on
event instead, if you´re using jQuery 1.7+. http://api.jquery.com/on/
For those having problems with Phonegap and google analytics :
Google code uses cookies and it doesn't work with file:// urls wich is what Phonegap uses. The Pokki team made a good implementation that uses localStorage in place of cookies. I made a fork on github to remove the need of pokki, so here is a solution that works with phonegap as a standalone library
https://github.com/ggendre/GALocalStorage
hope this will help someone else :)
http://code.google.com/apis/analytics/docs/mobile/overview.html http://analytics.blogspot.com/2009/10/google-analytics-now-more-powerful.html
These are references, please read for your knowladge.