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
»

7 Responsive jQuery Dropdown Menu Plugins

Dropdown Menu Plugins Dropdown menus are mostly used for website ...

article image
»

20 Stylish and Modern jQuery Tooltip Plugins

jQuery Tooltip Plugins Tooltips are a great option for showing extra ...

article image
»

15 Newest jQuery plugins for responsive website

Due to the proliferation of smartphones and tablets, responsive ...

article image
»

How to use Redux with jQuery

State management is one of most integral parts of any software and a ...

article image
»

Fade HTML Table Row on Hover Using jQuery

HTML tables are the ideal choice for displaying data in tabular form. ...

article image
»

Drag and Drop HTML Table Rows Using jQuery

HTML tables are the preferred UI option for displaying data. Sorting, ...

article image
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...