Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

iphone - Google Analytics in iOS (Not Working)

I am trying to implement google analytics.. could you guys please help me

-(void) setGoogleAnalytics{

    // Initialize tracker.
    self.tracker = [[GAI sharedInstance] trackerWithName:@"ipad app"
                                              trackingId:kTrackingID];

    NSDictionary *appDefaults = @{kAllowTracking: @(YES)};

    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    // User must be able to opt out of tracking

    [GAI sharedInstance].optOut =
    ![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];

    // Optional: automatically send uncaught exceptions to Google Analytics.
    [GAI sharedInstance].trackUncaughtExceptions = YES;

    // Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
    [GAI sharedInstance].dispatchInterval = 5;

    // Optional: set Logger to VERBOSE for debug information.
    [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

    [[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}

and calling it in

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
            [self setGoogleAnalytics];
    //
    //
    //
}

Inside My ViewController implementation

 [self dispatchEvent:@"Purchase Done"];

[self trackViewName:NSStringFromClass([self class])];


 -(void) trackViewName:(NSString *) strClassName{
        [[GAI sharedInstance] defaultTracker];
        self.screenName=[NSString stringWithFormat:@"%@",strClassName];
        [self.tracker send:[[NSDictionary alloc] initWithObjectsAndKeys:strClassName,@"ViewName", nil]];
        [[GAI sharedInstance] dispatch];

    }

- (void)dispatchEvent:(NSString *)strButtonText{

    id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

    [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                          action:@"button_press"  // Event action (required)
                                                           label:strButtonText          // Event label
                                                           value:nil] build]];    // Event value  = [[GAI sharedInstance] defaultTracker];

    [[GAI sharedInstance] dispatch];
}

Which version of google analytics, I should download currently I have downloaded google GoogleAnalyticsServicesiOS_3.01.zip (Recommended) as I dont want to work with the beta version GoogleAnalyticsiOS_2.0beta4.zip

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem with 3.01. My problem was actually in the Google Analytics admin section.

I had a profile setup for mobile (which was actually setup as if it were a website) which worked with version 1.x. However it appears that Google has now implented "mobile" profiles as well. I believe that the 3.x mobile SDKs cannot track to "Web" profiles.

Create a new profile by following the instructions here. And then use the new tracking Id and it should start tracking.

Note: Do not set the dispatchInterval to 0, as others have suggested, this also prevented tracking, set it to 1. This fixed everything for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...