简体   繁体   English

Google Analytics不会跟踪iOS上的视图?

[英]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). 我已将最新的Google AnalyticsSDK添加到我的iOS应用程序(版本2.0 beta 4)中。 I did the same as the guide says and added this code to app delegate: 我做了与导游说的相同,并将此代码添加到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. 一切正常,但由于某种原因,20个屏幕中的3个没有发送给谷歌,我不知道为什么。 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 你的-(void)viewDidLoad覆盖中的[super viewDidLoad]
  2. [super viewDidAppear:animated] in your -(void)viewDidAppear:(BOOL)animated override 你的-(void)viewDidAppear:(BOOL)animated覆盖中的[super viewDidAppear:animated]
  3. [super viewDidUnload] in your -(void)viewDidUnload override 你的-(void)viewDidUnload覆盖中的[super viewDidUnload]
  4. ... (take care of all the other methods that you are overriding from GAITrackedViewController ...(处理从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. 最重要的方法是viewDidLoadviewDidAppear:因为特别是对于实时跟踪viewDidAppear:显示此视图当前可见。

EDIT: Since version 3.0 of the Google Analytics SDK the screenName property should be set before calling the viewDidLoad method of super. 编辑:从Google Analytics SDK 3.0版开始,应在调用super的viewDidLoad方法之前设置screenName属性。

I am using version 3.02 and encountered the same problem. 我使用的是3.02版,遇到了同样的问题。 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. 你肯定想打电话给super因为它会触发发送ga数据。 It appears to me that the screenName needs to be set prior to it. 在我看来,需要在它之前设置screenName

The problem has been fixed with the simplest way. 用最简单的方法解决了这个问题。

The code that I wrote up, is for the AppDelegate . 我写的代码是针对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. 使用跟踪ID将跟踪器初始化一次后,您只需在每个使用它的视图中调用它。 (I'm not sure about that but anyway it worked for me.) (我不确定,但无论如何它对我有用。)

So, in the AppDelegate : 所以,在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 : 在视图控制器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" 在标题中添加: #import "GAI.h"

Now in viewDidLoad: 现在在viewDidLoad中:

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

  2. Than do this: [GAI sharedInstance] defaultTracker]; 比这样做: [GAI sharedInstance] defaultTracker];

  3. Also add this: [self.tracker sendView:@"Some Name"]; 另外添加: [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]; 这可能是一个愚蠢的想法,但对我来说,愚蠢的超级视图确实出现了[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. 另外,我只是一个iOS用户,但是当我查看gaitrackedviewcontroller.h时,要设置的最新屏幕名称是,屏幕名称不是trackedviewname。 So, this is what mine looked like: 所以,这就是我的样子:

  • (void)viewDidAppear:(BOOL)animated { (void)viewDidAppear:(BOOL)animated {

    self.screenName = @"Login Screen"; self.screenName = @“登录屏幕”;

    [super viewDidAppear:animated]; [super viewDidAppear:animated];

} }

Good Luck. 祝好运。 Hope this helps anyone. 希望这有助于任何人。 Stay Classy. 保持优雅。

In 3.10 environment, I had similar problem. 3.10环境中,我遇到了类似的问题。 I have tried other guy way, but no hope. 我尝试过其他的方式,但没有希望。 Finally, I found the ANSWER is at the outside of official instructions. 最后,我发现ANSWER不在官方指令之外。

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. 官方说明没有提到我们需要在默认情况下注册kAllowTraking。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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