julianharty / testing-heuristics

Experiments to see if we can establish evidence of the value of testing heuristics
MIT License
3 stars 0 forks source link

Google analytics code #10

Open ISNIT0 opened 7 years ago

ISNIT0 commented 7 years ago

For basic analytics, we need a code from analytics.google.com

I'm happy to make a 'project', but you may prefer it to be on your account? @julianharty

julianharty commented 7 years ago

Done, and details of the account sent by email. Thanks for the suggestion.

julianharty commented 7 years ago

Possible ways to implement GA so it works in a WebView

Here are various writings and ideas on ways we can use Google Analytics inside a 'WebView' e.g. in a ZIM file for Kiwix.

BTW: We may want to consider ways to detect which OS the Web content is being rendered in. Can we detect the code is running in an Android WebView vs iOS? Then can we detect the platform version (or infer it?), or is the content being served via kiwix-serve and/or kiwix-apache? etc. etc.

 Various articles to read and digest

http://stackoverflow.com/questions/28953605/how-to-implement-google-analytics-in-hybrid-mobile-apps (ideas for incorporating JavaScript to make the calls which will go directly to GA servers)

https://justins-tech.blogspot.co.uk/2015/02/adding-google-analytics-to-cordovaphone.html writes about their experiences trying danwilson's ga plugin (mentioned in the above SO topic). He's provided a proof-of-concept explanation here https://github.com/blast-analytics-marketing/phonegap-google-universal-analytics e.g. see https://github.com/blast-analytics-marketing/phonegap-google-universal-analytics/blob/master/index.html

http://stackoverflow.com/questions/25243278/how-to-track-webview-content-with-google-analytics-in-android (to make sure events are triggered correctly)

https://techcrunch.com/2014/04/17/google-analytics-now-lets-you-track-web-and-app-data-in-a-single-view/ A non-technical article from 2014 confirming that data from both web and app can be viewed together i.e. we're unlikely to break the reporting aspect.

Potential gotchas

https://www.thyngster.com/how-to-track-an-intranet-or-an-hybrid-app/ (from 2+ years ago) nonetheless the challenges and approach he describes may be relevant for us. In the example on github above I think they've applied a similar approach (e.g. setting protocol checking to none).

I hope we don't need to get involved in the following approach https://github.com/angulartics/angulartics-google-analytics/issues/57

For future consideration

http://stackoverflow.com/questions/15324538/using-google-analytics-with-hybrid-mobile-app a brilliant answer on how to integrate the WebView with the Native App's use of GA. We can't use this without modifying Kiwix-Android (for instance) however that might be viable with a custom, instrumented version of Kiwix-Android (with the User opting-in, etc).

https://firebase.google.com/docs/analytics/android/webview if we switch to using Firebase, here's what Google recommends - again it would require mods to the hosting app (kiwix-android and/or kiwix-ios). BTW: From reading the source code, it seems practical to tweak this and use it for GA. Google provides the source for a demo app https://github.com/firebase/analytics-webview Again this might be a good place to start future experiments from...

ISNIT0 commented 7 years ago

For now, I've just hard-coded the Google Analytics code. I guess it's probably best to get something working, rather than something perfect :)

julianharty commented 7 years ago

Extract from the Google cache of https://www.thyngster.com/how-to-track-an-intranet-or-an-hybrid-app/ as the original site wasn't available.

As we all know (do we?) Google Analytics uses cookies in order to work. This is, if for some reason the cookie can’t be set it won’t give any error but hits won’t be fired at all. A cookie is usually set for a FQHN (Fully Qualified Host Name). So if for example we’re going to track our intranet and we access it using an URL like : http://intranet/ Google Analytics is likely not going to work (it will depend on the browser that is accessing the page.

There’s another situation where we may have problems using the official analytics.js. For example when using a different protocol than http/https, an example of this if when we load an html file from the local file system ( file://index.html ). You may think this is not a usual way to access a webpage, but it’s the way the most Hybrid Apps work. A native app for a mobile is usually build using some well known web technologies like: HTML5, JavaScript and CSS, and uses a browser engine (not the browser itself) to load the main content locally (using the file:// protocol), and here we’ll hit the second issue, Universal Analytics has an internal task named checkProtocolTask, it basically aborts the request if the current protocol does match http/https.

Another example of devices using html files loaded from the local filesystem will be the SmartTV’s for example.

Fortunately Universal Analytics gives us the possibility to change those behaviours in order to manage those situations.

Tracking a non FQHN domain name

Even if it may work for some domains we better assure that it will run for all browsers, for this we need to set cookie’s domain to ‘none’ ,

ga('create', 'UA-XXXXXXX-YYY','none'}); or ga('create', 'UA-XXXXXXX-YYY', {'cookieDomain': 'none'});

Tracking a locally loaded page

In this case we’ll need to set the storage engine to none, and disabling the protocol check:

   ga('create', 'UA-123123123-123',{
      'storage': 'none', 
      'clientId':'{{UUIDv4}}'
  });
  ga('set', 'checkProtocolTask', null);
  ga('set', 'appName', 'MyApp Name');
  ga('set', 'appVersion', '1.2.2');
  ga('send', 'appview', {
        'screenName': 'My Home Screen'
  });

Let’s take to the 2 important line in the code above;

'storage': 'none'

This line disables the default cookie storage system, and set’s it to none.

 'clientId':'{{UUIDv4}}'

As we have disabled the storage engine, we’ll need to manually set the clientId (the cid parameter within our hits) , usually most devices offers a way to gan Unique Identifier, or we could grab the mac address and convert it to a UUIDv4 value 🙂

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

This last line disables the protocol checking task, so the requests are not aborted because it doesn’t match the http/https protocols.