一步一步让Google Analytics(分析)在iOS上使用PhoneGap 1.2.0(phonegapalytics) [英] Step by step to get Google Analytics working in PhoneGap 1.2.0 on iOS (phonegapalytics)

查看:168
本文介绍了一步一步让Google Analytics(分析)在iOS上使用PhoneGap 1.2.0(phonegapalytics)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS中在PhoneGap 1.2.0中安装Google Analytics(分析)?

解决方案

首先,很多信用给家伙在这里。他们的博客获得了巨大的帮助,但我仍然需要做更多的事情才能让它工作。



我要经历它一步一步,使你可以得到它在一个非常基本的水平工作,然后你可以从那里拿走。我发现即使是最轻微的错误,也可以把事情搞砸。



我假设你已经安装了PhoneGap 1.2.0并且正在使用Xcode 4.2






PART A



使基本PhoneGap应用程式正常运作。按照 PhoneGap指南进行操作。






PART B



下载PhoneGap Google Analytics Plugin。
我发现最简单的办法是从这里下载zip文件。你得到了一堆东西你不需要,但它没有关系。



展开下载目录中的文件。进入扩展目录..进入 iPhone 文件夹,您应该会看到 GoogleAnalytics 文件夹。



,右键单击项目(顶部的蓝色块),然后选择添加文件到。导航到展开的文件夹。点击GoogleAnalytics文件夹。确保选中将文件复制到目标组的文件夹(如有必要)并将其添加到目标。点击添加。将Xcode中的文件夹中的文件拖动到Plugins文件夹。



返回Finder并打开项目目录。将 GoogleAnalyticsPlugin.js 从GoogleAnalytics文件夹复制到www文件夹。 Xcode会给出关于重新保存文件的警告。只需点击关闭。



转到index.html文件。 function onDeviceReady()将以下行添加到函数的开头。



window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID(YOUR UA CODE);



接下来在 onDeviceReady 函数(在同一个脚本块中)

  function trackpage(id)
{
console.log(start trackpage:+ id);
window.plugins.googleAnalyticsPlugin.trackPageview(id);
console.log(end trackpage:+ id);
}



现在,仍然在index.html文件中,找到

 < li>检查您的控制台日志是否存在白名单拒绝错误。 

(请记住这是phonegap 1.2.0)



在其后添加以下行。

 < li class =arrow>< a href = javascript:trackpage('/ TEST');>测试分析< / a>< / li> 

这将提供一个链接,您可以在测试应用程序中点击。



注意。您必须在您跟踪的网页中有正斜杠(/)。因此 javascript:trackpage('/ TEST')将工作,而 javascript:trackpage('TEST')



最后,仍然在index.html中添加以下行

 < script type =text / javascriptcharset =utf-8src =GoogleAnalyticsPlugin.js>< / script> 

以下

 < script type =text / javascriptcharset =utf-8src =phonegap-1.2.0.js>< / script> 

不要试图运行它。它不会工作。






PART C



接下来转到 GoogleAnalytics.h 文件。只是擦它..这里有一堆问题。 JSON似乎现在是JSONKit和PhoneGapCommand现在是PGPlugin ..只是粘贴这里。PhoneGap文档似乎是在所有的地方。

  #import< Foundation / Foundation.h> 

#ifdef PHONEGAP_FRAMEWORK
#import< PhoneGap / PGPlugin.h>
#import< PhoneGap / NSData + Base64.h>
#import< PhoneGap / JSONKit.h>
#else
#elport
#importPGPlugin.h
#importNSData + Base64.h
#importJSONKit.h
#endif

#importGANTracker.h

@interface GoogleAnalytics插件: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插件文件夹中。



在Xcode中,转到 GoogleAnalytics / GoogSDK 文件夹,并将 GANTracker.h libGoogleAnalytics.a 文件拖到Plugins目录。



基本上,您需要将它们从blue GoogleAnalytics / GoogSDK 文件夹复制到黄色它们可以通过头文件找到。






PART E



接下来,我们需要添加sqlite(在Google Analytics(分析)未在线时存储数据)和CFNetwork(以便Google Analytics(分析)可以上传数据)到项目。



该项目(蓝色的东西在文件的顶部)。点击主目标。然后点击Build Phases。列表中的第三项是链接二进制与库。注意有两个这些..选择顶部一个。打开它,点击+号。添加 libsqlite3.0.dylib 然后添加 CFNetwork.framework






PART F



最后(几乎有)我们需要设置插件。转到 PhoneGap.plist 文件。在插件下,添加键为 googleAnalyticsPlugin (javascript名称)和值 GoogleAnalyticsPlugin C部分)。



在ExternalHosts下添加 * 条目



现在尝试运行。手指越过它会工作。



测试它以运行程序,然后使用新的Google Analytics(分析)实时(测试版)模式,因为您可以看到实时跟踪的网页。



您需要更改为新版Google Analytics(分析)(应该是顶部的一个明显链接,以便移至新版本)。打开GA帐户,转到首页.REAL_TIME ...概述。
您应该会在几秒钟内看到TEST页面轨道。



注意:您无法实时跟踪事件。您需要等待他们处理。



ISSUES



在查看编译源链接二进制文件与库时会遇到一些问题。 GoogleAnalytics零件会显示为红色。如果它们是红色的,这意味着你的项目找不到它们,我想你如何解决这取决于你的Xcode的版本,所以你可能需要google一下来解决它。我做的是删除GoogleAnalytics(按 - 符号),然后拖动文件。如果它显示为一个正确的图标(而不是一个红色框),它应该工作。



我认为如果您使用Phonegap你可能也在做一个Android版本。如果是这种情况,您可能需要查看此比较 Q + A的差异。


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.

I am assuming you have installed PhoneGap 1.2.0 and are using Xcode 4.2


PART A

Get a basic PhoneGap app working. Follow the PhoneGap guide and get that working.


PART B

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.

Expand the file in your download directory. Go into the expanded directory.. into the iPhone folder and you should see the GoogleAnalytics folder.

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.

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.

Go to the index.html file. In function onDeviceReady() add the following line to the start of the function.

window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("YOUR UA CODE");

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);
}

Now, still in the index.html file, find the line

<li>Check your console log for any white-list rejection errors.</li>

(remember this is phonegap 1.2.0)

add the following line after it.

<li class="arrow"><a href="javascript:trackpage('/TEST');">test analytics</a></li>

This gives a link you can click on in the test app.

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.

Finally, still in the index.html add the following line

<script type="text/javascript" charset="utf-8" src="GoogleAnalyticsPlugin.js"></script> 

below

<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.


PART C

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


PART D

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.

In Xcode, go to the GoogleAnalytics/GoogSDK folder and drag the GANTracker.h and libGoogleAnalytics.a files to the Plugins directory.

Basically you need to copy them from the ‘blue’ GoogleAnalytics/GoogSDK folder into a ‘yellow’ folder so they can be found by header files.


PART E

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.

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.


PART F

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).

Under ExternalHosts add a * entry

Now try and run. Fingers crossed it will work.

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.

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.

ISSUES

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.

Doing it on Android also?

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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