问题描述
如何在 iOS 的 PhoneGap 1.2.0 中安装 Google Analytics?
How do I install Google Analytics in PhoneGap 1.2.0 in iOS?
推荐答案
首先,我必须非常感谢 这里.他们的博客有很大帮助,但我仍然需要做更多最终让它工作的事情.
First and foremost I have to give a lot of credit to the guys here. Their blog was of tremendous help but I still had to do a few more things to finally get it to work.
我将逐步完成它,以便您可以在非常基本的级别上使用它,然后您就可以从那里开始学习.我发现即使是最轻微的错误也会把事情搞砸.
I’m going to go through it step by step so that you can get it to work at a very basic level and then you can take it from there. I found that even the slightest mistake can mess things up.
我假设您已经安装了 PhoneGap 1.2.0 并且使用的是 Xcode 4.2
I am assuming you have installed PhoneGap 1.2.0 and are using Xcode 4.2
A 部分
让一个基本的 PhoneGap 应用运行起来.按照 PhoneGap 指南 进行操作.
Get a basic PhoneGap app working. Follow the PhoneGap guide and get that working.
B 部分
下载 PhoneGap Google Analytics 插件.我发现最简单的方法就是从 这里下载 zip 文件.你不需要的东西,但没关系.
Download the PhoneGap Google Analytics Plugin.I find the easiest thing to do is just download the zip file from here. You get a pile of things you don't need but it doesn't matter.
展开下载目录中的文件.进入展开的目录...进入 iPhone 文件夹,您应该会看到 GoogleAnalytics 文件夹.
Expand the file in your download directory. Go into the expanded directory.. into the iPhone folder and you should see the GoogleAnalytics folder.
在 Xcode 中,右键单击项目(顶部的蓝色块)并选择将文件添加到".导航到展开的文件夹.单击GoogleAnalytics"文件夹.确保选中将文件复制到目标组的文件夹(如有必要)"并将其添加到目标中.单击添加".将文件夹中的文件在 Xcode 中拖到 Plugins 文件夹中.
In Xcode, right click on project (the blue block thing at the top) and choose 'Add files to '. Navigate to the expanded folder. Click on the 'GoogleAnalytics' folder. Make sure the 'copy files into destination group's folder (if necessary)' is selected and that its added to the target. Click 'Add'. Drag the files in the folder in Xcode to the Plugins folder.
返回 Finder 并打开项目目录.将 GoogleAnalyticsPlugin.js
从 GoogleAnalytics 文件夹复制到www"文件夹.Xcode 会发出有关重新保存文件的警告.只需单击关闭".
Go back to Finder and open the project directory. Copy the GoogleAnalyticsPlugin.js
from the GoogleAnalytics folder to the 'www' folder. Xcode will give a warning about re-saving the file.. Just click Close.
转到 index.html 文件.在 function onDeviceReady()
中,将以下行添加到函数的开头.
Go to the index.html file. In function onDeviceReady()
add the following line to the start of the function.
window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("您的 UA 代码");
接下来在onDeviceReady
函数下添加如下函数(在同一个脚本块内)
Next add the following function under the onDeviceReady
function (inside the same script block)
function trackpage(id)
{
console.log("start trackpage: " + id);
window.plugins.googleAnalyticsPlugin.trackPageview(id);
console.log("end trackpage: " + id);
}
现在,仍然在 index.html 文件中,找到该行
Now, still in the index.html file, find the line
<li>Check your console log for any white-list rejection errors.</li>
(记住这是phonegap 1.2.0)
(remember this is phonegap 1.2.0)
在它后面添加以下行.
<li class="arrow"><a href="javascript:trackpage('/TEST');">test analytics</a></li>
这提供了一个链接,您可以在测试应用中点击该链接.
This gives a link you can click on in the test app.
注意.您必须在您要跟踪的页面中有一个正斜杠 (/).所以 javascript:trackpage('/TEST')
会起作用,而 javascript:trackpage('TEST')
不会.
NB. You MUST have a forward slash (/) in the page your are tracking. So javascript:trackpage('/TEST')
will work while javascript:trackpage('TEST')
will NOT.
最后还是在index.html中添加下面一行
Finally, still in the index.html add the following line
<script type="text/javascript" charset="utf-8" src="GoogleAnalyticsPlugin.js"></script>
下面
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
先不要尝试运行它.它不会工作.
Don't bother trying to run it yet. It won't work.
C 部分
接下来转到 GoogleAnalytics.h
文件.擦一下就好了..这里有一堆问题.JSON 现在似乎是 JSONKit,PhoneGapCommand 现在是 PGPlugin.. 只需将其粘贴进去.PhoneGap 文档似乎到处都是.
Next goto the GoogleAnalytics.h
file. Just wipe it.. There are a pile of problems here. JSON seems to be now JSONKit and PhoneGapCommand is now PGPlugin.. Just paste this in. The PhoneGap documentation seems to be all over the place.
#import <Foundation/Foundation.h>
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#import <PhoneGap/NSData+Base64.h>
#import <PhoneGap/JSONKit.h>
#else
#import "PGPlugin.h"
#import "NSData+Base64.h"
#import "JSONKit.h"
#endif
#import "GANTracker.h"
@interface GoogleAnalyticsPlugin : PGPlugin<GANTrackerDelegate> {
}
- (void) startTrackerWithAccountID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackPageview:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
D部分
接下来将 Google Analytics 部分添加到项目中.这是 Google 提供的部分,但它方便地包含在 phonegap ios GoogleAnalytics/GoogSDK 插件文件夹中.
Next add the Google Analytics part to the project. This is the part which Google provides but its conveniently included in the phonegap ios GoogleAnalytics/GoogSDK plugin folder.
在 Xcode 中,转到 GoogleAnalytics/GoogSDK
文件夹并将 GANTracker.h
和 libGoogleAnalytics.a
文件拖到 Plugins 目录中.
In Xcode, go to the GoogleAnalytics/GoogSDK
folder and drag the GANTracker.h
and libGoogleAnalytics.a
files to the Plugins directory.
基本上,您需要将它们从蓝色"GoogleAnalytics/GoogSDK
文件夹复制到黄色"文件夹中,以便可以通过头文件找到它们.
Basically you need to copy them from the ‘blue’ GoogleAnalytics/GoogSDK
folder into a ‘yellow’ folder so they can be found by header files.
E 部分
接下来,我们需要将 sqlite(Google Analytics 不在线时存储数据的地方)和 CFNetwork(以便 Google Analytics 上传数据)添加到项目中.
Next we need to add sqlite (where Google Analytics stores data when not online) and CFNetwork (so Google Analytics can upload the data) to the project.
在 Xcode 中单击项目(文件顶部的蓝色部分).单击主要目标.然后单击构建阶段.列表中的第三项是Link Binary with Libraries".请注意,其中有两个.. 选择最上面的一个.打开它并单击+号.添加libsqlite3.0.dylib
,然后添加CFNetwork.framework
.
In Xcode click on the project (the blue thing at the top of the file). Click on the main target. Then click on the Build Phases. The 3rd item on the list is the "Link Binary with Libraries". Note there are two of these.. choose the top one. Open it and click on the + sign. Add libsqlite3.0.dylib
then add CFNetwork.framework
.
F 部分
最后(差不多)我们需要设置插件.转到 PhoneGap.plist
文件.在插件下添加另一个条目,其键为 googleAnalyticsPlugin
(javascript 名称),值为 GoogleAnalyticsPlugin
(Obj-C 部分).
Finally (almost there) we need to setup the plugin. Go to the PhoneGap.plist
file. Under Plugins add another entry with the key as googleAnalyticsPlugin
(the javascript name) and the value as GoogleAnalyticsPlugin
(the Obj-C part).
在 ExternalHosts 下添加一个 *
条目
Under ExternalHosts add a *
entry
现在尝试运行.手指交叉它会起作用.
Now try and run. Fingers crossed it will work.
测试它运行程序的最佳方法,然后使用新的 Google Analytics Real Time (BETA) 模式,因为您可以看到实时跟踪的页面.
The best way to test it to run the program and then use the new Google Analytics Real Time (BETA) mode as you can see pages being tracked live.
您需要更改为新版本的 Google Analytics(移至新版本的顶部应该是一个明显的链接).打开 GA 帐户,转到主页..REAL_TIME...概览.您应该会在几秒钟内看到 TEST 页面轨道.
You need to change to the new version of Google Analytics (it should be an obvious link at the top to move to the new version). Open the GA account, go to Home..REAL_TIME... Overview.You should see the TEST page track coming in within a few seconds.
注意:您无法实时跟踪事件.您需要等待它们被处理.
Note: You can't track events in realtime. You need to wait for them to be processed.
问题
我在查看编译源和将二进制文件与库链接时遇到了一些问题.GoogleAnalytics 部分将显示为红色.如果它们是红色的,则意味着您的项目无法找到它们,我认为您如何解决此问题取决于您的 Xcode 版本,因此您可能需要通过谷歌搜索一下来解决该问题.我所做的是删除 GoogleAnalytics(按 - 符号)然后将文件拖进去.如果它显示为正确的图标"(而不是红色框),它应该可以工作.
I had some problems when looking at the Compile Sources and Link Binary With Libraries. The GoogleAnalytics parts would show up in red. If they are in red it means your project can't find them and I think how you resolve this depends on your version of Xcode so you might need to google about a bit to resolve that. What I did was to remove the GoogleAnalytics (press the - sign) then drag the file in. If it shows up as a "proper icon" (as opposed to a red box) it should have worked.
是否也在 Android 上执行此操作?
我假设如果您使用 Phonegap,您可能也在使用 Android 版本.如果是这种情况,您可能需要查看此比较 Q+A 以了解差异.
I assume if you are using Phonegap you are probably doing an Android version also. If this is case you may want to check out this comparison Q+A for the differences.
这篇关于逐步让 Google Analytics 在 iOS 上的 PhoneGap 1.2.0 中工作(phonegapalytics)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!