可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
I had the same problem some time ago. I bet that you don't call the following methods in your overrides:
[super viewDidLoad]
in your -(void)viewDidLoad
override
[super viewDidAppear:animated]
in your -(void)viewDidAppear:(BOOL)animated
override
[super viewDidUnload]
in your -(void)viewDidUnload
override
- ... (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.
回答2:
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.
回答3:
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.
回答4:
So, I found the answer to this issue:
In header add this: #import "GAI.h"
Now in viewDidLoad:
Add self.trackedViewName = @"Some Name";
Than do this:
[GAI sharedInstance] defaultTracker];
Also add this:
[self.tracker sendView:@"Some Name"];
Than it will work just fine.
回答5:
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.
回答6:
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.