Search code examples
iosgoogle-analyticsanalyticsgoogle-analytics-api

error with adding google analytics


i have been trying to add google analytics to my app in development by following an online text tutorial which was ok at first but right at the end i get an error on this code... (specifically the code in the .m file)

Two things:

When i go on google analytics, i dont register any information and the line of code right below registers an error when i try to compile and run

Another thing is in all this code is there a section im supposed to put the tracker ID from google analytics?

P.S is it ok to include a link tot he tutorial i was following so that you guys know what im talking about? i dont wanna get banned so thought i would ask first...

 - (IBAction)tappedButtonOne:(id)sender {
     id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

 [tracker sendEventWithCategory:@"MyFirstScreen"
                     withAction:@"ButtonPress"
                     withLabel:@"ButtonOne"
                     withValue:nil];
} 

here is the code i have in the .h

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "GAITrackedViewController.h"

@interface ViewController : GAITrackedViewController <ADBannerViewDelegate>

@property (retain, nonatomic) IBOutlet ADBannerView *banner;
@property (retain, nonatomic) IBOutlet UITextView *txtinfo;
- (IBAction)tappedButtonOne:(id)sender;

@end

and here is the code i have in the .m

}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc {
[_banner release];
[_txtinfo release];
[super dealloc];
}
- (IBAction)tappedButtonOne:(id)sender {
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

[tracker sendEventWithCategory:@"MyFirstScreen"
                    withAction:@"ButtonPress"
                     withLabel:@"ButtonOne"
                     withValue:nil];
 }
 @end

Solution

  • Another thing is in all this code is there a section im supposed to put the tracker ID from google analytics?

    Yes, you need to initialize the tracker in your application delegate.

    See the Google Analytics SDK for iOS - Getting Started guide, especially Section 2 where it explains:

    To initialize the tracker, import the GAI.h header in your application delegate .m file and add this code to your application delegate's application:didFinishLaunchingWithOptions: method:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // 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 = 20;
    
      // Optional: set Logger to VERBOSE for debug information.
      [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
    
      // Initialize tracker.
      id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
    
    }
    

    It's here that you need to provide the tracker with your tracking ID by initializing the tracker with the trackerWithTrackingId method.

    The rest of your code looks fine, and should work as expected when you've added the code above to your application delegate.

    Note: It can take up to 24 hours for events to show up in Google Analytics after they have been sent by your app. If there's still nothing showing after 1 day, then you might need to check your code again.