8

I am looking for a good solution to do mobile analytics for Jquery mobile . I did check this question

Flurry Analytics vs Google Analytics on the mobile platform

but these are all solutions for a platform specific/ phone manufacturer specific but jquery mobile works on all platforms irrespective of the manufacturer or operating system. Essentially i am looking for a analytics solution for webapps.

Additional Info:- bango seems expensive at $49/month. Admob wont work since we dont need it for advertising and not for placing ads.

Community
  • 1
  • 1
pal4life
  • 3,210
  • 5
  • 36
  • 57

2 Answers2

2

I'm using the following:

<script>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxx-xx']);

    (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);
    })();

$('[data-role=page]').live('pageshow', function (event, ui) {
    try {

        hash = location.hash;

        if (hash && hash.length > 1) {
            _gaq.push(['_trackPageview', hash.substr(1)]);
        } else {
            _gaq.push(['_trackPageview']);
        }
    } catch(err) {

    }

});
</script>

The 'pageshow' event fires even for the first page, so don't think you want to include the _trackPageview with the GA setup. Also, location.hash will return url with the "#" character so hash.subtr(1) cleans that off which will normalize hash/pushstate visitors.

Update 11/30/11: Added check for hash length for ie bug (from: Paulo Manuel Santos).

Dan
  • 194
  • 1
  • 9
  • I found that in some cases with IE and after clicking the back button the url is rendered with an empty hash (http:blabla/Index#). So I changed it to `if (hash && hash.length > 1)`. – pauloya Nov 29 '11 at 10:44
  • For the `else` I used `location.pathname`. – pauloya Nov 29 '11 at 10:46
  • Anyone tried this solution and found it *didn't* work? This setup works only for the app that I put out as an online demo site, but not for any installations of my app on mobile phones. – Wytze Jan 08 '12 at 16:05
  • 1
    @user316727, check out this for using google analytics in native apps, normal code isn't going to work: http://analytics.blogspot.com/2011/12/google-analytics-enhancements-for.html – Dan Jan 31 '12 at 04:56
1

I use the following bits of code for Google Analytics and it works well:

The following is pretty much the normal Google Analytics setup:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', '**-*****-**']);

(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);
})();

The update for jQuery Mobile is here so that each pseudo-page is logged:

$(document).delegate('[data-role=page]', 'pageshow', function (event, ui) {
    var url = location.href;
    try  {
        if (location.hash) {
            url = location.hash;
        }
        _gaq.push(['_trackPageview', url]);
    } 
    catch(error) {
        // error catch
    }
});
Jasper
  • 75,717
  • 14
  • 151
  • 146
  • Will try this out and send my feedback. – pal4life Aug 16 '11 at 17:50
  • havent tried this yet but Bango Analytics definitely works for Mobile Webapps. – pal4life Sep 02 '11 at 04:32
  • 1
    In the above snippet, one should be careful not to declare a global variable 'url' - thus, define a 'var url' instead. (Not kidding: http://blog.meloncard.com/post/12175941935/how-one-missing-var-ruined-our-launch) – miek Dec 05 '11 at 08:25