This my main.js file
/* Google Analytics */
(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);
})();
var _gaq = _gaq || [];
function _track(page){
try {
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); /* But with my correct one */
console.log('google analytics:' + page);
if (page != '') {
_gaq.push(['_trackPageview', 'Mobile: '+page]);
} else {
_gaq.push(['_trackPageview'],'Mobile');
}
} catch(err) {
console.log(err);
}
}
So Any time I want to track any page I use:
_track('/top-rated/');
And I can see this log:
google analytics: /top-rated/
But I can't see the log in the real time google analytics page
By the way I have in my config.xml
<access origin=".*"/>
The reason why GA does not work in device is because google analytics expects the protocol to be HTTP or HTTPS, but when you load the app the protocol is file:/// since you are opening a file from the device in WebView.
In such a case GA does not allow cookies to be stored & shuts down. Also there is a new version of ga.js called analytics.js(also called Universal Analytics), which is basically an advanced version. You can find the difference between the versions here - http://www.cardinalpath.com/which-version-of-google-analytics-is-right-for-you-determine-whether-you-should-upgrade-to-universal-analytics/
If you would like to continue using ga.js then you can use this -
https://github.com/ggendre/GALocalStorage
It is a GitHub project which resolves the file:/// problem.
If you would like to use Universal Analytics then you can use this -
http://www.blastam.com/blog/index.php/2013/07/ga-universal-analytics-phonegap-mobile-apps/
It is a guide to make it compatible with Phonegap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With