1

I'm trying to get google analytics working with my PhoneGap/Cordova app.

By default, the analytics.js lib will not send requests from file:// type urls. I found from this site, and the StackOverflow question that it points to, that I can get the analytics lib to use local storage, rather than cookies, for keeping track of the client ID by configuring GA thusly:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','http://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-1', 'auto', {
        'allowLinker': true,
        'storage': 'none','clientId':localStorage.getItem('gaClientId')
    });
ga(function(t){localStorage.setItem('gaClientId',t.get('clientId'));});

And then I can get the library to send requests from non-http/https urls by adding the following:

ga('set', 'checkProtocolTask', null);

Now I can see, from my browser console, that the library is sending page views out to google-analytics.com similar to the following:

http://www.google-analytics.com/collect?v=1&_v=j31&a=6458293880&t=pageview&_s=1&dl=file%3A%2F%2F%2Fpath%2Fto%2Findex.html&ul=en-us&de=ISO-8859-1&sd=24-bit&sr=1440x900&vp=1283x150&je=1&fl=16.0%20r0&_u=cGAAAAQB~&cid=389492833.14589283847&tid=UA-XXXXX-1&z=1277371227

However, on the google-analytics.com side, Google is ignoring that request once it receives it, and nothing shows up under my account. If I manually change the "dl=file..." part of the above URL to "dl=http...", then Google accepts it, and I immediately see the request under the "Real-Time" tab of my account.

So, it appears I've got the browser side of things set up correctly, but...

  1. how can I configure my google-analytics account to accept the requests the browser sends?
  2. Alternatively, is there a way to configure the browser side of things so that it sends "dl=http://..." rather than "dl=file://...", even though it's coming from a file:// URL?

(I think I might prefer an answer to #2 over #1, if I had to choose one answer.)

I know that there is a cordova plugin for google analytics, but I haven't had much luck getting that one to work, and, even if I got it to work, I need my app to be able to log to multiple google-analytics accounts at once, which I don't think the plugin to can do.

Community
  • 1
  • 1
Troy
  • 21,172
  • 20
  • 74
  • 103
  • why don't you use the analytics plugin instead? Not sure if that plugin supports multiple trackers right now, but it could be done – jcesarmobile Dec 19 '14 at 08:10
  • I cant help with Cordova, but how long ago did you create the account its trying to send to it takes 24-48 hours to start showing data. you could send stuff manually: https://developers.google.com/analytics/devguides/collection/protocol/v1/ – Linda Lawton - DaImTo Dec 19 '14 at 09:51
  • @jcesarmobile - I'm not completely against the plugin, but, in addition to the reasons I listed earlier for not using it, there is also the fact that the data it sends doesn't show up in the "Real-Time" tab (according to it's developers), and the fact that the data from the plugin is often slow at showing up (once you get it working). On the other side, the non-plugin technique is inches away from working and, currently, only has 1 strike against it. It also has the added benefit of working the same way as my mobile site (which it shares code with). It's just the lower-hanging fruit right now. – Troy Dec 19 '14 at 16:42
  • @DaImTo - I created the account a few days ago. It will show other data, just not data from file:// URLs. – Troy Dec 19 '14 at 16:43

1 Answers1

5

After poking around through the minified google code, I found the answer to #2 in the original question:

ga('set', 'checkProtocolTask', function(data) {
    data.set('location', 'http://whatever.host.I.want.com');
});

Overriding the "checkProtocolTask" like this does 2 things:

  1. The default/original "checkProtocolTask" checks that the request is coming from http/https and throws an exception for file:// URLs. Overriding this stops that from happening.
  2. This method (like all of the 14 or so tasks that get iteratively called) is provided a data object that is used to build the request that is sent to google-analytics.com. This data object contains, among other things, a "location" property, which ultimately gets turned into the "dl" querystring param that is sent to google. By changing the "location", and, therefore, the "dl" param, to an http URL, you prevent Google from ignoring the hit.

To change the "dl" param, you could, instead, override the "buildHitTask", which might be a more appropriately-named task for modifying the "hit" that is sent to Google. However, I don't know all of what the original buildHitTask() does, so I would hesitate to replace it unless you investigate it further. I know that "checkProtocolTask" is safe to replace, as it is very clear what it's job is supposed to be.

Troy
  • 21,172
  • 20
  • 74
  • 103