user519569
user519569

Reputation: 83

Google Analytics tracking doesn't work for me on iOS and my GANTrackerDelegate never gets called

I've implemented Google Analytics in my iOS app, but nothing ever gets logged to analytics. I've been running it for a month, so I know there's something that's not working.

I added a GANTrackerDelegate to trace why my dispatch calls are failing, but it never gets called. I know dispatch is getting called. Does anyone know? Here's my class:

#import <Foundation/Foundation.h>
#import "GANTracker.h"

@interface trackerDelegate : NSObject<GANTrackerDelegate> {

}
@end


#import "trackerDelegate.h"

@implementation trackerDelegate

#pragma mark GANTrackerDelegate

- (void)trackerDispatchDidComplete:(GANTracker *)tracker
                  eventsDispatched:(NSUInteger)eventsDispatched
              eventsFailedDispatch:(NSUInteger)eventsFailedDispatch {
    NSLog(@"events dispatched: %d, events failed: %d", eventsDispatched, eventsFailedDispatch);
}
@end

Here's my tracker class. Note, I can trace all of these things getting called, and I know that dispatch is called repeatedly and returns 'YES', but the delegate routine never gets called. It might be a coding thing, but I'm trying to see if the dispatch calls are failing or not. The id I'm using is valid because it works for Android.

tracker.h

@interface Tracker : NSObject {

}

+ (void) startTracking;
+ (void) endTracking;
+ (void) dispatch;
+ (void) trackPageView : (NSString *) pageId;
+ (void) trackEvent : (NSString *) categoryId
     : (NSString *) actionID
     : (NSString *) labelID
     : (int) tvalue;


@end

tracker.m

#import "Tracker.h"
#import "trackerDelegate.h"

@implementation Tracker

static BOOL trackingOn = false;
static BOOL dirty = false;
trackerDelegate *tg = nil;

+ (void) startTracking
{
 if (trackingOn){
  return;
 }
 @try{
  if (!tg)
  {
   tg = [[trackerDelegate alloc] init];
  }
  
  [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-VALID-GOOGLEID"
              dispatchPeriod:-1
              delegate:tg];
  
  trackingOn = true;
 }
 @catch (NSException*)
 {
  trackingOn = false;
 }
}

+ (void) endTracking
{
  @try{
 if (trackingOn)
 {
  [[GANTracker sharedTracker] stopTracker];
 }
 trackingOn = false;
  }
  @catch (NSException *){
 trackingOn = false;
  }
}

+ (void) dispatch
{
 if (!dirty){
  return;
 }
 [self startTracking];
 @try
 {
  if (![[GANTracker sharedTracker] dispatch]) {
   trackingOn = false;
   NSLog(@"Google anaytics dispatch failed");
   return;
  }
  dirty = false;
 }
 @catch (NSException *){
  trackingOn = false;
 }
}



+ (void) trackPageView : (NSString *) pageId
{
 [self startTracking];
 @try{
  if (trackingOn){
   NSError *error;
   if (![[GANTracker sharedTracker] trackPageview:pageId
             withError:&error]) {
       trackingOn = false;
    NSLog(@"Google anaytics track pageview failed");
    return;
   }
   dirty = true;
  }
 }
 @catch (NSException *){
  trackingOn = false;
 }
}

+ (void) trackEvent : (NSString *) categoryId
     : (NSString *) actionID
     : (NSString *) labelID
     : (int) tvalue
{
 [self startTracking];
 @try
 {
  if (trackingOn){
   NSError *error;
   if (![[GANTracker sharedTracker] trackEvent:categoryId
             action:actionID
              label:labelID
              value:tvalue
             withError:&error]) 
   {
    trackingOn = false;
    NSLog(@"Google anaytics track event failed");
    return;
   }
   dirty = true;
  }
 }
 @catch (NSException *){
  trackingOn = false;
 }
}
@end

Upvotes: 1

Views: 2119

Answers (1)

thoughtbreaker
thoughtbreaker

Reputation: 161

First of all you should initiate the Google Analytics account using the Web-property ID given to you at the time of creating an account in Google Analytics site.

Then you must add the following code to start the tracking and set the dispatch time. Then only you could view the analytics count in your account in Google Analytics online account. Here is the initiating code for that.

[[GANTracker sharedTracker]startTrackerWithAccountID:@"UA-xxxxxx-yy"
                                      dispatchPeriod:10
                                            delegate:nil];

You should also make sure, you have included the following code in the dealloc in the AppDelegate.

[GANTracker sharedTracker] stopTracker];

Hope this will work fine. Thanks.

Upvotes: 1

Related Questions