如何在没有插件的情况下将 Google Analytics 与 Phonegap 一起使用? [英] How to use Google Analytics with Phonegap without a plugin?

查看:31
本文介绍了如何在没有插件的情况下将 Google Analytics 与 Phonegap 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,我想从用户那里获得分析.我尝试使用 Phonegap 插件,但我没有成功尝试实现它.

I am making an app and I want to get analytics from the users. I tried to use the Phonegap Plugin, but I didn't have any luck trying to implement it.

我想知道是否可以通过将应用程序视为普通网页并将一些 javascript 放在我的页面头部来获取 Google Analytics.

I was wondering if it was possible to get Google Analytics by treating the app like a normal webpage and putting some javascript in the head of my page.

有没有更好的方法来做到这一点?Phonegap Google Analytics 是否比我正在尝试做的要好得多?

Is there a better way to do this? and is the Phonegap Google Analytics THAT much better than what I'm trying to do?

推荐答案

Google Analytics 现在可以在混合应用中使用本地存储.

Google Analytics 现在有一个此处解释的选项 使用 LocalStorage 而不是 cookie,还有一个 hack 可以让它在 webviews (file:// urls) 中工作.因此,您可以不使用我之前建议的代码,而是这样做:

[edit] Google Analytics now works with localstorage in hybrid apps.

Google Analytics now have an options explained here to use LocalStorage instead of cookies and there is also a hack to make it work in webviews (file:// urls). So instead of using the code I suggested before, you can just do this :

// THIS IS FOR LOCALSTORAGE
var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
ga('create', 'UA-XXXXX-Y', {
  'storage': 'none',
  'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function(tracker) {
  localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});

// THIS IS FOR FILE URL SUPPORT
ga('set', 'checkProtocolTask', function(){ /* noop */});

// And then as usual...
ga('send', 'pageview');

以前的回答内容:

Alex 建议的 pokki 解决方案运行良好,只需进行一些调整即可消除对 Pokki 的需求.

previous answer content :

The pokki solution suggested by Alex is working fine with a few adjustments to remove the need of Pokki.

我在这里为这个清理过的版本创建了一个 git 项目:

I created a git project for this cleaned-up version here :

https://github.com/ggendre/GALocalStorage

在 android 4.1 和 ios6 上运行良好,我将很快测试更多设备.希望这可以帮助 !:)

Works great on android 4.1 and ios6, I will test more device very soon. Hope this helps ! :)

这篇关于如何在没有插件的情况下将 Google Analytics 与 Phonegap 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆