4

我正在尝试在我的 iOS 应用中安装 Google Analytics SDK 来跟踪页面浏览量和事件。遵循提供的文档后,我似乎根本无法进行跟踪,我想知道有人可以帮助我吗?以下信息可能是相关的 - 我会尽量做到描述性。

  • 我的目标是 iOS 4.3 及更高版本的所有设备。
  • 我已将 GANTracker.h 包含在我的 project-prefix.pch 文件中,并且构建正常

下面是我的 appdelegate.m 代码中的一个示例,它在 didFinishLaunchingWithOptions 方法中调用

/*  Google Analytics tracking code  */

[[GANTracker sharedTracker] setSampleRate:100];

[[GANTracker sharedTracker]setDebug:NO];

[[GANTracker sharedTracker] setDryRun:NO];

[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-111111-1"
                                       dispatchPeriod:kGANDispatchPeriodSec
                                             delegate:self];
NSLog(@"Dispatch%@", [[GANTracker sharedTracker] dispatch] ? @"ed Successfully": @" Failed");

NSError *error;
if (![[GANTracker sharedTracker] setCustomVariableAtIndex:1
                                                     name:@"iPhone"
                                                    value:@"iv1"
                                                withError:&error])
{
    NSLog(@"There was an error setting this custom variable\n Description: %@\n", [error localizedDescription]);
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]);
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]);
}

if (![[GANTracker sharedTracker] trackEvent:@"Loading"
                                     action:@"App Finished Launching"
                                      label:@"appDidFinishLaunchingWithOptions"
                                      value:-1
                                  withError:&error])
{
    NSLog(@"There was an error in tracking events\n Description: %@\n", [error localizedDescription]);
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]);
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]);
}

NSString *pageUrlString = [[NSString stringWithFormat:@"http://ios.organisation.tld/appentrypoint"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
if(![[GANTracker sharedTracker] trackPageview:pageUrlString withError:&error])
{
    NSLog(@"There was an error in tracking initial app entry\n Description: %@\n", [error localizedDescription]);
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]);
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]);

}

在我的应用程序视图控制器中,我想跟踪 pageViews 并按以下方式进行操作:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSError *trackError;
NSString *pageUrlString = [[NSString stringWithFormat:@"/aboutsection"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
if(![[GANTracker sharedTracker] trackPageview:pageUrlString withError:&trackError])
{
    //Handle error here
    NSLog(@"There was an error tracking this pageview\n Description: %@\n", [trackError localizedDescription]);
    NSLog(@"Failure reason: %@\n", [trackError localizedFailureReason]);
    NSLog(@"May we suggest: %@\n", [trackError localizedRecoverySuggestion]);
}
}

回到我的 appdelegate.m 我还设置了以下委托

- (void)trackerDispatchDidComplete:(GANTracker *)tracker eventsDispatched:(NSUInteger)hitsDispatched eventsFailedDispatch:(NSUInteger)hitsFailedDispatch
{
    NSLog(@"Google analytics dispatch\n Succeeded?:\n %i, \n Failed?: %i", hitsDispatched, hitsFailedDispatch);
}

这会记录为 Succeeded?: 5 Failed?: 0 所以那里没有失败

我还看到一条日志消息...没有可发送的信息。登录到谷歌分析我看不到访问。查看实时分析也显示没有发生任何事情。

我在这里错过了什么吗?

4

1 回答 1

0

它仅在我尝试以下步骤时才有效:

1- 在 appdelegate .h 类 #import "GAI.h"

2- 在函数“didFinishLaunchingWithOptions”中的 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 = NO;
    // Create tracker instance.
    [[GAI sharedInstance] trackerWithTrackingId:"your tracking application id from google analytics"];

3- 在任何 uiviewcontroller 类。h #import "GAI.h"

4- at .h 将这部分“:uiviewcontroller”替换为“:GAITrackedViewController”

5- at .m add in view 确实加载了这一行

[[[GAI sharedInstance] defaultTracker] trackView:"your view title"];
  • 请注意第 5 点,必须以这种方式实现,我认为 GA 库在任何其他方式和文档中也存在问题。我尝试并搜索了很多关于这个问题的方法,但偶然它只能通过这种方式工作,除了它是在 GA 屏幕上显示效果的最快方式。

  • 另注:您必须在谷歌分析中为移动设备创建应用程序,注意应用程序的 ID 以在 IOS 应用程序中添加正确

于 2013-04-15T15:59:29.797 回答