Phonegap谷歌分析根本没有跟踪

Ton*_*bet 12 javascript google-analytics cordova

这是我的main.js文件

/* 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以每当我想跟踪我使用的任何页面时:

_track('/top-rated/');

我可以看到这个日志:

google analytics: /top-rated/

但我无法在实时谷歌分析页面中看到日志

顺便说一句,我在config.xml中

<access origin=".*"/>
Run Code Online (Sandbox Code Playgroud)

Ram*_*eya 13

GA在设备中不起作用的原因是因为谷歌分析期望协议是HTTP或HTTPS,但是当您加载应用程序时,协议是file:///,因为您在WebView中从设备打开文件.

在这种情况下,GA不允许存储和关闭cookie.另外还有一个名为analytics.js的新版ga.js(也称为Universal Analytics),它基本上是一个高级版本.您可以在这里找到版本之间的差异 - http://www.cardinalpath.com/which-version-of-google-analytics-is-right-for-you-determine-whether-you-should-upgrade-to-万向分析/

如果您想继续使用ga.js,那么您可以使用它 -
https://github.com/ggendre/GALocalStorage
这是一个解决文件:///问题的GitHub项目.

如果您想使用Universal Analytics,那么您可以使用它 -
http://www.blastam.com/blog/index.php/2013/07/ga-universal-analytics-phonegap-mobile-apps/
这是一个指南使其与Phonegap兼容