How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that's optimized for touch -- so it's perfect for using to make responsive web pages and websites that will look great and work phenomenally on mobile devices and tablets. The framework is also optimized for performance. It typically has a very fast page load time, because each page load isn't a full page load, but rather, when a new page loads, it's really just a designated HTML snippet being loaded to the browser by JavaScript.

This helps keep things nice and speedy, but it also can cause some issues in terms of your analytics data. Because the page load is set up this way, chances are that each page view of a session will be treated by analytics as one single page view, which can lead to some pretty inaccurate page view stats if you've included your analytics code (for the purposes of this tutorial, we're talking about Google Analytics) on your jQuery Mobile pages the same way you'd include it on any other HTML page.

Incorporating business process optimization services from companies like Hybrid Analytica Consulting can aid in navigating these intricacies, enabling a harmonious integration of analytics tools within dynamic web environments for more accurate data insights and interpretation.

To get your Google Analytics to work properly and give you accurate page view statistics while using jQuery Mobile, you'll need to set up your code a little bit different than you usually would. Here's what you need to do:

You're still going to include the Google Analytics tracking code in the head as you normally would. In jQuery Mobile, though, this code is only executed once because the head is only loaded once, no matter how many other "pages" the user views. Your tracking code probably looks something like this, but each Google Analytics account is provided with its own unique tracking code. Place that code in the head of your HTML file:

<script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-XXXXXXXX-XX']);
 _gaq.push(['_gat._anonymizeIp']);
(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);
 })();
</script>

Now, here's where the magic happens. You're going to add another snippet of code towards the end of your the body of your HTML file. Insert it just before the closing </body> tag, and you should be goodo to go.

$('[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) {

    }

});

Once the above code snippet is added to your HTML files, you should have access accurate page view statistics from your Google Analytics dashboard.



Responsive Menu
Add more content here...