javascript - Phonegap 谷歌分析根本不跟踪

标签 javascript cordova google-analytics

这是我的 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);
    }
}

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

_track('/top-rated/');

我可以看到这个日志:

谷歌分析:/top-rated/

但是我在实时google analytics页面看不到日志

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

<access origin=".*"/>

最佳答案

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

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

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

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

关于javascript - Phonegap 谷歌分析根本不跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020856/

相关文章:

javascript - 响应式 Adwords 广告

ios - 权限通知推送

android - Cordova 中的后台 Firebase 推送通知

javascript - 谷歌分析 : Events are not tracked

firebase - 项目已链接但未导出数据

javascript - meteor CSV 导出

javascript - jQuery UI 模态对话框在 JSP 上立即关闭

google-analytics - 我的 Google Analytics(分析)跟踪代码在哪里

javascript - 未为寻宝脚本保存 Cookie

javascript - 在浏览器中使用 Azure 移动应用 JavaScript 客户端访问经过身份验证的表