iPhone Google Analytics SDK 委托和多帐户问题
我正在我的 iPhone 应用程序中实施 google Analytics SDK。我让它与以下代码一起工作:
AppDelegate .m :
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-xxxxxxx-1"
dispatchPeriod:10
delegate:nil];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking 2, %@", error);
}else {
NSLog(@"OK");
}
在我的分析帐户中,我得到了想要的结果。然后我决定(不要问我为什么)也尝试将我的跟踪数据发送到第二个分析帐户。对于好奇的人来说:一个帐户用于网页和 iPhone 应用程序统计,另一个帐户应该仅用于 iPhone。
我巧妙的计划是创建第一个共享跟踪器,调度它,停止它,并对第二个共享跟踪器执行相同的操作:
AppDelegate .h:
@interface AppDelegate : NSObject <UIApplicationDelegate, GANTrackerDelegate>
//implementation
AppDelegate .m:
//1st tracking account
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-xxxxxxx-1"
dispatchPeriod:10
delegate:self];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking, %@", error);
}else {
NSLog(@"1. GAnalytics: OK");
}
[[GANTracker sharedTracker] stopTracker];
//2nd tracking account
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-zzzzzzzz-1"
dispatchPeriod:10
delegate:self];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking, %@", error);
}else {
NSLog(@"2. GAnalytics: OK");
}
[[GANTracker sharedTracker] stopTracker];
- (void)trackerDispatchDidComplete:(GANTracker *)tracker
eventsDispatched:(NSUInteger)eventsDispatched
eventsFailedDispatch:(NSUInteger)eventsFailedDispatch{
NSLog(@"For the love of Got, why don't you say something?");
}
我添加了委托方法,以便获得一些关于正在调度的内容的线索,并查明 SDK 是否正在发出两个不同的请求,但似乎我无法调用我的委托方法! 删除第二个跟踪器的代码也没有帮助 我还尝试将调度周期设置为 0(以及手动调度调用的 -1),但我也没有运气……
所以,我的问题是: 如何实施 2 个 gAnalytics 帐户以及 如何让我的委托方法做它应该做的事情 - 在调度后被调用:)
提前致谢, 卢卡
I am implementing google Analytics SDK in my iPhone application. I had it working with following code:
AppDelegate .m :
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-xxxxxxx-1"
dispatchPeriod:10
delegate:nil];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking 2, %@", error);
}else {
NSLog(@"OK");
}
In my analytics account I was getting the wanted results. Then I decided (don't ask me why) to try to send my tracking data to 2nd analytics account too. For curious ones: One account is used for web page and iPhone app stats and the other one is supposed to be just for iPhone.
My ingenious plan was to create 1st sharedTracker, dispatch it, stop it and do the same for the second one:
AppDelegate .h:
@interface AppDelegate : NSObject <UIApplicationDelegate, GANTrackerDelegate>
//implementation
AppDelegate .m:
//1st tracking account
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-xxxxxxx-1"
dispatchPeriod:10
delegate:self];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking, %@", error);
}else {
NSLog(@"1. GAnalytics: OK");
}
[[GANTracker sharedTracker] stopTracker];
//2nd tracking account
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-zzzzzzzz-1"
dispatchPeriod:10
delegate:self];
if (![[GANTracker sharedTracker] trackPageview:string withError:&error]) {
NSLog(@"Error happened with google analytics tracking, %@", error);
}else {
NSLog(@"2. GAnalytics: OK");
}
[[GANTracker sharedTracker] stopTracker];
- (void)trackerDispatchDidComplete:(GANTracker *)tracker
eventsDispatched:(NSUInteger)eventsDispatched
eventsFailedDispatch:(NSUInteger)eventsFailedDispatch{
NSLog(@"For the love of Got, why don't you say something?");
}
I added the delegate method in order to get some clue what's being dispatched, and to find out if SDK is making two different requests, but it seems I can't get my delegate method invoked!
Removing the second tracker's code doesn't help either
I also tried putting the dispatch period to 0 (and -1 with manual dispatch call) but I had no luck with this either…
So, my questions are:
how to implement 2 gAnalytics accounts and
how to make my delegate method do what it's supposed to do - get called after dispatch :)
Thanks in advance,
Luka
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一开始就提出了同样的要求,即能够将页面浏览量、事件等发布到两个不同的 Google Analytics 帐户上。但问题是,[GANTracker sharedTracker] 是一个单例对象,你总是得到该对象的相同或单个实例,因此你不能真正拥有两个 shareTracker 实例。
同样在您的情况下,您试图查看是否调用回调方法,它不会被调用,因为您正在调用 [[GANTracker sharedTracker] stopTracker] 这将阻止事件的分派发生。如果您注释掉该行,则应该调用您的回调方法。
如果您查看“文档”下的应用程序目录,您会发现 Google Analytics 将所有数据存储在名为 googleanalytics.sql 的 sqlite 数据库中。您可以打开它并查看其中的表格。转到该目录并输入“sqlite3 googleanalytics.sql”,如果您知道 sqlite 命令,则可以导航表和内容。这些表都没有引用帐户 ID,所以我的猜测是,除非您做了一些非常聪明的怪事,否则您无法真正发布到两个不同的帐户。
I started out exactly with the same requirement of being able to post pageviews, events etc onto two different Google Analytics accounts. But the problem is that, [GANTracker sharedTracker] is a singleton object and you always get the same or single instance of the object back, so you cannot really have two instances of the sharedTracker.
Also in your case, where you are trying to see whether the call back method is called, it will not be called because you are calling [[GANTracker sharedTracker] stopTracker] and this will prevent from dispatch of the events to happen. If you comment out that line, your callback method should get called.
If you look in the app directory under Documents, Google analytics stores all the data in a sqlite database called googleanalytics.sql. You can open it and see the tables in it. Go to that directory and type "sqlite3 googleanalytics.sql" and if you know sqlite commands, you can navigate the tables and stuff. None of the tables have a reference to the Account ID, so my guess is that, unless you do some really smart quirks, you cannot really post to two different Accounts.
Google 支持在同一应用程序中使用多个跟踪帐户,其 iOS SDK v2
请参阅这个问题和我的答案那里。
Google has support for using multiple tracking accounts in the same application, in its iOS SDK v2
See this SO question and my answer there.