Google Groups

Re: Google analytics not working


TimW Apr 3, 2012 5:16 PM
Posted in group: phonegap
I've only tried this in iOS, but here's how I got it working *without* using a plug-in.

Essentially, Google Analytics won't work if its being used from a file:/// url.  In iOS/PhoneGap this is the case.  In order to solve this problem you must first download the ga.js file from google and include it as part of your local build.  You'll notice the this file is obfuscated.  Search the file for the string "file:" which should occur only once.  When you find it, add an underscore to the beginning (so it becomes "_file:").  This prevents it matching the protocol of the page location (which is "file:").

Now include the modified file in your program rather than the remote ga.js file and you'll find that analytics information will start to appear (check the Realtime display - very helpful of debugging).

Let me know if it works on Android.

On Wednesday, March 14, 2012 7:25:04 AM UTC-7, Massimo Nicolardi wrote:
Hello,

I am trying to install google analytics tracking code on a cordova (1.5.0) application (android) but I have had no success.

I tried to use this code:

<script type="text/javascript">

var _gaq = _gaq || [];

  _gaq.push(['_setAccount', 'UA-xxxxxx-1']);

  _gaq.push(['_setDomainName', 'none']);

   _gaq.push(['_trackPageview','android/home']);

  (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>


but no pageview gets tracked by analytics.
Have anyone succeeded to track pageviews with analytics?

By the way.. I know that there there is a plugin which do the job but it has not been upgraded for the use with cordova and so I don't think I can use it.

Thanks,
Massimo


On Wednesday, March 14, 2012 7:25:04 AM UTC-7, Massimo Nicolardi wrote:
Hello,

I am trying to install google analytics tracking code on a cordova (1.5.0) application (android) but I have had no success.

I tried to use this code:

<script type="text/javascript">

var _gaq = _gaq || [];

  _gaq.push(['_setAccount', 'UA-xxxxxx-1']);

  _gaq.push(['_setDomainName', 'none']);

   _gaq.push(['_trackPageview','android/home']);

  (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>


but no pageview gets tracked by analytics.
Have anyone succeeded to track pageviews with analytics?

By the way.. I know that there there is a plugin which do the job but it has not been upgraded for the use with cordova and so I don't think I can use it.

Thanks,
Massimo