开发者

iPhone Google Analytics SDK delegate and multiple accounts problem

开发者 https://www.devze.com 2023-02-16 23:39 出处:网络
I am implementing google Analytics SDK in my iPhone application. I had it working with following code:

I am implementing google Analytics SDK in my iPhone application. I had it working with following code:

AppDelega开发者_运维技巧te .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


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 has support for using multiple tracking accounts in the same application, in its iOS SDK v2

See this SO question and my answer there.

0

精彩评论

暂无评论...
验证码 换一张
取 消