我正在制作一个应用程序,我想从用户那里获得分析。我尝试使用Phonegap插件,但我没有任何机会尝试实现它。
我想知道是否有可能获得谷歌分析的应用程序作为一个正常的网页,并把一些javascript在我的网页头部。
有没有更好的方法来做这件事?Phonegap Google Analytics真的比我想做的要好得多吗?
发布于 2013-01-04 17:37:55
编辑Google Analytics现在可以在混合应用程序中使用本地存储。
Google Analytics现在有了一个使用LocalStorage而不是cookies的options explained here,也有一个黑客可以让它在webviews (file:// urls)中工作。因此,您可以这样做,而不是使用我之前建议的代码:
// 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的需求。
我在这里为这个经过清理的版本创建了一个git项目:
https://github.com/ggendre/GALocalStorage
在Android4.1和ios6上运行良好,我很快就会测试更多的设备。希望这能有所帮助!:)
发布于 2012-06-14 13:02:40
查看视频,看看它的实际效果:
http://screencast.com/t/6vkWlZOp
经过一番研究,我找到了一个解决方案。我在Phonegap Google Group上偶然发现了这个帖子:https://groups.google.com/forum/#!msg/phonegap/uqYjTmd4w_E/YD1QPmLSxf4J (感谢TimW和Dan Levine!)在这个线程中,我发现可以在没有插件的情况下使用Google Analytics。你所要做的就是从Google http://www.google-analytics.com/ga.js下载ga.js文件(只需将页面保存到你的www文件夹中)
然后修改ga.js文件,向其添加一个字符。在文件文件中搜索单词“ga.js:”并将其替换为"_file:“。
在我链接到上面的帖子中,"TimW“解释了原因:
从本质上讲,谷歌分析将不会工作,如果它是从一个file:///
使用。在iOS/PhoneGap中就是这种情况。为了解决这个问题,您必须首先从谷歌下载ga.js文件,并将其包含在本地构建中。你会注意到这个文件是模糊的。在文件中搜索字符串" file :“,该字符串应该只出现一次。找到后,在开头加上下划线(这样它就变成了"_file:")。这会阻止它与页面位置的协议(即"file:")相匹配。
将一个字符添加到ga.js文件后,只需在页面顶部包含以下内容:
<script type="text/javascript" src="ga.js"></script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-YOUR_ID_HERE']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_trackPageview', 'NAME_OF_PAGE']);
</script>我在模拟器上测试了它,我得到了一个证据,证明它正在使用谷歌分析中的实时视图工作。该模拟器在iOS 5.0上运行。我的手机仍然使用iOS 4.2,当我在我的设备上测试它时,它没有显示在实时跟踪上。
在帖子中,有人提到了安卓4.0+的同样问题……希望在未来会有更好的解决方案,但就目前而言,这是为我的应用程序获取基本分析的最简单和最简单的方法。它不能进行离线跟踪,但无论如何这都有点吓人。
尽管iOS 4和安卓系统的用户在市场上只占少数(见饼图):
http://static7.businessinsider.com/image/4fd65fac6bb3f7925700000f/chart-of-the-day-ios-vs-android-june-2012.jpg
我仍然想从所有操作系统中获取数据。
发布于 2017-02-14 09:47:19
现在是2017年2月,不再需要编辑analytics.js,也不再需要一个库或插件,或者至少我不需要它们。过去几年中说过的许多事情都被弃用或只是过时了,所以这里是我最新的综合指南。
1. config.xml文件
在您的config.xml中,您必须允许跨站点请求:
<access origin="https://www.google-analytics.com" />
HTML2.
在您的CSP meta标签中,如果您选择拥有一个,则还必须允许对Google的调用。它可能看起来像这样:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-inline' 'unsafe-eval' https://ssl.gstatic.com https://www.google-analytics.com;">
javascript 3.
这是一个webapp的注释代码,它既可以在浏览器中运行,也可以在Cordova打包的应用中运行。如果你不关心浏览器,你可以忽略else块。
// the default GA code, nothing to change
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
var fields = {
// note: you can use a single tracking id for both the app and the website,
// don't worry it won't mix the data. More about this in the 3rd section
trackingId: 'UA-XXXXXXXX-Y'
};
// if we are in the app (the protocol will be file://)
if(document.URL.indexOf('http://') !== 0){
// we store and provide the clientId ourselves in localstorage since there are no
// cookies in Cordova
fields.clientId = localStorage.getItem('ga:clientId');
// disable GA's cookie storage functions
fields.storage = 'none';
ga('create', fields);
// prevent tasks that would abort tracking
ga('set', {
// don't abort if the protocol is not http(s)
checkProtocolTask: null,
// don't expect cookies to be enabled
checkStorageTask: null
});
// a callback function to get the clientId and store it ourselves
ga(function(tracker){
localStorage.setItem('ga:clientId', tracker.get('clientId'));
});
// send a screenview event
ga('send', {
// these are the three required properties, check GA's doc for the optional ones
hitType: 'screenview',
// you can edit these two values as you wish
screenName: '/index.html',
appName: 'YourAppName'
});
}
// if we are in a browser
else {
ga('create', fields);
// send a pageview event
ga('send', {
// this is required, there are optional properties too if you want them
hitType: 'pageview'
});
}3.您的GA帐户
App类型的视图。如果你不需要从网站上监控你的web应用的流量,你可以在这里停止阅读,否则继续阅读。我假设你使用一个帐户来跟踪网站和应用程序。
screenview命中。有一个官方指南hereWebsite类型的第二个视图。在其上应用自定义过滤器"Application?=> no“。App类型的第三个视图。默认情况下(没有过滤器),它将显示所有数据。附加说明
<access>和CSP中不再需要http协议了*.google-analytics.com是行不通的。虽然这一策略在Chrome (56)中有效,但在科尔多瓦(5.6.0)的Crosswalk插件
https://stackoverflow.com/questions/11026916
复制相似问题