简体   繁体   中英

Google Analytics won't track a view on iOS?

I've added the newest Google Analytics SDK to my iOS application (version 2.0 beta 4). I did the same as the guide says and added this code to app delegate:

// 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 debug to YES for extra debugging information.
[GAI sharedInstance].debug = YES;
// Create tracker instance.
self.tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-TRACKINGID-2"];//UA-33873963-13 - My testing google analytics trackingId

[self.tracker setSessionStart:YES];
[self.tracker setSessionTimeout:60];

Now, on each view I've added this:

self.trackedViewName = @"Main Menu Screen";

Everything works fine, but for some reason 3 out of the 20 screens is not sending themselves to Google and I don't have a clue why. I've searched all over the net, but no one has came across this issue. I figured that if someone familiar with this issue, it's on stack-overflow.

Please help me. Thanks!

I had the same problem some time ago. I bet that you don't call the following methods in your overrides:

  1. [super viewDidLoad] in your -(void)viewDidLoad override
  2. [super viewDidAppear:animated] in your -(void)viewDidAppear:(BOOL)animated override
  3. [super viewDidUnload] in your -(void)viewDidUnload override
  4. ... (take care of all the other methods that you are overriding from GAITrackedViewController

Concretely, your methods should look like the following:

-(void)viewDidLoad
{
  // call this at the beginning of the overridden methods
  self.screenName = @"Some Name"; 
  [super viewDidLoad];

  // your remaining code here
}

-(void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

  // your remaining code here
}

The most important methods are viewDidLoad and viewDidAppear: since especially for real time tracking the viewDidAppear: shows that this view is currently visible.

EDIT: Since version 3.0 of the Google Analytics SDK the screenName property should be set before calling the viewDidLoad method of super.

I am using version 3.02 and encountered the same problem. The following fixed my problem:

-(void) viewDidAppear:(BOOL)animated {
   self.screenName = @"My Screen"; // set screenName prior to calling super
   [super viewDidAppear:animated];

   ...
}

You definitely want to to call super because it triggers sending the ga data. It appears to me that the screenName needs to be set prior to it.

The problem has been fixed with the simplest way.

The code that I wrote up, is for the AppDelegate . After the tracker has been initialized once with tracking ID, you just need to call it in each view that you are using it. (I'm not sure about that but anyway it worked for me.)

So, in the AppDelegate :

// 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 debug to YES for extra debugging information.
[GAI sharedInstance].debug = YES;
// Create tracker instance.
self.tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-TRACKINGID-2"];//UA-33873963-13 - My testing google analytics trackingId

[self.tracker setSessionStart:YES];
[self.tracker setSessionTimeout:60];

Inside the view controllers viewDidLoad :

[GAI sharedInstance] defaultTracker];
[self.tracker sendView:@"Some name"];

After doing this, it worked perfectly.

So, I found the answer to this issue:

In header add this: #import "GAI.h"

Now in viewDidLoad:

  1. Add self.trackedViewName = @"Some Name";

  2. Than do this: [GAI sharedInstance] defaultTracker];

  3. Also add this: [self.tracker sendView:@"Some Name"];

Than it will work just fine.

So.. This is for the noobies out there.. (myself included)..

This might be a dumb thing to think of, but for me was putting the stupid super view did appear [super viewDidAppear:animated]; after you call to set the screen name.

As an aside, I'm just an iOS user, but when I looked into the gaitrackedviewcontroller.h the latest screen name to set is, well, screenname not trackedviewname. So, this is what mine looked like:

  • (void)viewDidAppear:(BOOL)animated {

    self.screenName = @"Login Screen";

    [super viewDidAppear:animated];

}

Good Luck. Hope this helps anyone. Stay Classy.

In 3.10 environment, I had similar problem. I have tried other guy way, but no hope. Finally, I found the ANSWER is at the outside of official instructions.

After adding two lines, it works.

 NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
 [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

Official instructions doesn't mention we need register kAllowTraking in defaults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM