iOS - GANTracker无需发送

时间:2012-10-20 17:34:02

标签: objective-c ios google-analytics

我正在尝试在我的iOS应用中安装Google AnalyticsSDK以跟踪网页浏览量和事件。按照提供的文件,我似乎无法跟踪工作,我想知道任何人都可以帮助我吗?以下信息可能是相关的 - 我将尝试尽可能具有描述性。

  • 我的目标是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);
}

这会记录为成功?:5失败?:0所以没有失败

我也看到了一条日志消息......无需发送。登录Google Analytics我发现没有访问。查看实时分析也表明没有发生任何事情。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

只有当我尝试这个步骤时它才有效:

1- at appdelegate .h class #import“GAI.h”

2- at appdelegate in function“didFinishLaunchingWithOptions”添加此代码

 // 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用“:GAITrackedViewController”替换这部分“:uiviewcontroller”

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

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

  • 另请注意:您必须在google analytics上创建适用于移动设备的应用,注意应用程序的ID以在IOS应用程序中正确添加