X

LATEST #JQUERY NEWS

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.

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.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...