Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Google Analytics into a jQueryMobile website

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?

like image 894
Witek Avatar asked Jan 13 '12 09:01

Witek


People also ask

Is jQuery Mobile still relevant?

The team announced that the cross-platform jQuery Mobile project under its umbrella is fully deprecated as of October 7, 2021.

How do I add Google Analytics to my web app?

Go to google.com/analytics. To create an account, click Get started today. If you already have a Google Analytics account, click Sign in to Analytics. Set up Analytics on your website and/or app.


1 Answers

Jon 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/

like image 155
Filip Avatar answered Sep 19 '22 09:09

Filip