iOS 中的 Google Analytics(不工作) [英] Google Analytics in iOS (Not Working)

查看:19
本文介绍了iOS 中的 Google Analytics(不工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施谷歌分析..你们能帮帮我吗

-(void) setGoogleAnalytics{//初始化跟踪器.self.tracker = [[GAI sharedInstance] trackerWithName:@"ipad app"trackingId:kTrackingID];NSDictionary *appDefaults = @{kAllowTracking: @(YES)};[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];//用户必须能够选择退出跟踪[GAI 共享实例].optOut =![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];//可选:自动向 Google Analytics 发送未捕获的异常.[GAI sharedInstance].trackUncaughtExceptions = YES;//可选:将 Google Analytics 调度间隔设置为例如20 秒.[GAI sharedInstance].dispatchInterval = 5;//可选:将 Logger 设置为 VERBOSE 以获取调试信息.[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];[[GAI sharedInstance] setTrackUncaughtExceptions:YES];}

并调用它

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{[自己设置GoogleAnalytics];//////}

在我的 ViewController 实现中

 [self dispatchEvent:@"Purchase Done"];[self trackViewName:NSStringFromClass([self class])];-(void) trackViewName:(NSString *) strClassName{[[GAI sharedInstance] defaultTracker];self.screenName=[NSString stringWithFormat:@"%@",strClassName];[self.tracker send:[[NSDictionary alloc] initWithObjectsAndKeys:strClassName,@"ViewName", nil]];[[GAI sharedInstance] 调度];}- (void)dispatchEvent:(NSString *)strButtonText{id<GAITracker>tracker = [[GAI sharedInstance] defaultTracker];[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"//事件类别(必填)action:@"button_press"//事件动作(必填)label:strButtonText//事件标签值:nil] 构建]];//事件值 = [[GAI sharedInstance] defaultTracker];[[GAI sharedInstance] 调度];}

我应该下载哪个版本的 google Analytics,目前我已经下载了 google GoogleAnalyticsServicesiOS_3.01.zip(推荐),因为我不想使用测试版 GoogleAnalyticsiOS_2.0beta4.zip

解决方案

UPDATE -- Google Analytics SDK for iOS v3

所以我使用的是 v3,没有任何问题:

我在 AppDelegate 中实现了它.在 .h 文件中:

#import "GAI.h"@property (nonatomic,assign) id<GAITracker>追踪器;//我没有使用 ARC(分配)

.m:

#import "GAIDictionaryBuilder.h"#import "GAIFields.h"//谷歌分析[GAI sharedInstance].trackUncaughtExceptions = YES;[GAI sharedInstance].dispatchInterval = 0;tracker = [[GAI sharedInstance] trackerWithTrackingId:@"yourGAID"];

然后写一个这样的方法:

- (void) sendGoogleAnalyticsView:(NSString*)viewName{[tracker set:kGAIScreenName value:viewName];[跟踪器发送:[[GAIDictionaryBuilder createAppView] build]];[[GAI sharedInstance] 调度];//这将强制跟踪您的视图.}

<小时>

旧答案:

请参阅此链接下方的此答案,如果您按照我在此答案中所说的方法进行操作,则它必须有效

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//可选:自动向 Google Analytics 发送未捕获的异常.[GAI sharedInstance].trackUncaughtExceptions = YES;//可选:将 Google Analytics 调度间隔设置为例如20 秒.[GAI sharedInstance].dispatchInterval = 0;//可选:将 Logger 设置为 VERBOSE 以获取调试信息.[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];//初始化跟踪器.id<GAITracker>tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];}

要手动发送屏幕视图,请在跟踪器上设置屏幕字段值,然后发送命中:

//如果跟踪器尚未初始化,则可能返回 nil//属性 ID.id tracker = [[GAI sharedInstance] defaultTracker];//此屏幕名称值将在跟踪器上保持设置并随同发送//命中直到它被设置为一个新值或为零.[跟踪集:kGAIScreenName值:@"主屏幕"];[跟踪器发送:[[GAIDictionaryBuilder createAppView] build]];

或自动屏幕测量:

<块引用>

使用屏幕自动测量视图GAITrackedViewController 类.拥有每个视图控制器扩展 GAITrackedViewController 并添加一个名为 screenName 的属性.此属性将用于设置屏幕名称字段.

<代码>////MyViewController.h//在 ViewController 中使用自动屏幕跟踪的示例.//#import "GAITrackedViewController.h"//为自动屏幕扩展提供的 GAITrackedViewController//测量.@interface AboutViewController : GAITrackedViewController@结尾////MyViewController.m//#import "MyViewController.h"#import "AppDelegate.h"@implementation MyViewController- (void)viewDidLoad {[超级viewDidLoad];//设置屏幕名称.self.screenName = @"主屏幕";}//其余的 ViewController 实现.@结尾

事件跟踪:

链接

<块引用>

要将事件发送到 Google Analytics,请使用GAIDictionaryBuilder.createEventWithCategory:action:label:value: 和发送命中,如本例所示:

//如果跟踪器尚未使用属性初始化,则可能返回 nil//ID.id<GAITracker>= [[GAI sharedInstance] defaultTracker];[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"//事件类别(必填)action:@"button_press"//事件动作(必填)label:@"play"//事件标签值:nil] 构建]];//事件值

I am trying to implement google analytics.. could you guys please help me

-(void) setGoogleAnalytics{

    // Initialize tracker.
    self.tracker = [[GAI sharedInstance] trackerWithName:@"ipad app"
                                              trackingId:kTrackingID];

    NSDictionary *appDefaults = @{kAllowTracking: @(YES)};

    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    // User must be able to opt out of tracking

    [GAI sharedInstance].optOut =
    ![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];

    // 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 = 5;

    // Optional: set Logger to VERBOSE for debug information.
    [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

    [[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}

and calling it in

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
            [self setGoogleAnalytics];
    //
    //
    //
}

Inside My ViewController implementation

 [self dispatchEvent:@"Purchase Done"];

[self trackViewName:NSStringFromClass([self class])];


 -(void) trackViewName:(NSString *) strClassName{
        [[GAI sharedInstance] defaultTracker];
        self.screenName=[NSString stringWithFormat:@"%@",strClassName];
        [self.tracker send:[[NSDictionary alloc] initWithObjectsAndKeys:strClassName,@"ViewName", nil]];
        [[GAI sharedInstance] dispatch];

    }

- (void)dispatchEvent:(NSString *)strButtonText{

    id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

    [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                          action:@"button_press"  // Event action (required)
                                                           label:strButtonText          // Event label
                                                           value:nil] build]];    // Event value  = [[GAI sharedInstance] defaultTracker];

    [[GAI sharedInstance] dispatch];
}

Which version of google analytics, I should download currently I have downloaded google GoogleAnalyticsServicesiOS_3.01.zip (Recommended) as I dont want to work with the beta version GoogleAnalyticsiOS_2.0beta4.zip

解决方案

UPDATE -- Google Analytics SDK for iOS v3

So I'm using v3, and there is not any problem:

I'm implemented it in the AppDelegate. In .h file:

#import "GAI.h"
@property (nonatomic,assign) id<GAITracker> tracker; // I'm not using ARC (assign)

.m:

#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"

// GOOGLE ANALYTICS
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 0;
tracker = [[GAI sharedInstance] trackerWithTrackingId:@"yourGAID"];

And write a method like this:

- (void) sendGoogleAnalyticsView:(NSString*)viewName{
    [tracker set:kGAIScreenName value:viewName];
    [tracker send:[[GAIDictionaryBuilder createAppView] build]];
    [[GAI sharedInstance] dispatch]; // this will force track your views.
}


Old answer:

See this answer below this link, if you do it the same as I told in this answer it must work

Another stack-overflow answered question about google-analytics

and use these methods:

[GAI sharedInstance].optOut = YES;
[GAI sharedInstance].dispatchInterval = 0;
[GAI sharedInstance].trackUncaughtExceptions = YES;
    tracker = [[GAI sharedInstance] trackerWithTrackingId:@"YOUR TRACKERID"];


[tracker sendView:@"Your View name"];

[tracker sendEventWithCategory:@"YOUR CATEGORY" withAction:@"YOUR ACTION" withLabel:nil withValue:nil];

Download GoogleAnalyticsiOS_2.0beta4.zip from this link this will contain those classes what you need, and it will work perfectly. Be careful, google analytics got a lead time, to show you information, about real time. And not real time datas will show only a day after

EDIT for 3.0:

I found some probably useful things for you:

We have just come across this issue and this is slightly out of date so here is an updated answer. The issue we were having after following the instructions on the Google Analytics website, they instruct you to add the following files GAI.h, GAIDictionaryBuilder.h, GAILogger.h, GAITrackedViewController.h, GAITracker.h and libGoogleAnalytics_debug.a library. What they completely forget to include on the website instructions is the one where you have to include libGoogleAnalyticsServices.a library. This is included in the zipped download but there is no instructions to indicate to include this in the debug version.

Note : In the readme.txt libGoogleAnalyticsServices.a is just referred to as libGoogleAnalytics.a Google have failed to update their documentation to include the new name or the correct instructions that indicate this is required in debug.

Files and Libraries that most be included

GAI.h
GAIDictionaryBuilder.h
GAIFields.h
GAILogger.h
GAITrackedViewController.h
GAITracker.h
libGoogleAnalytics.a // Also know as libGoogleAnalyticsServices.a
libGoogleAnalytics_debug.a

plus information:

I'm pretty sure google has not yet provided a arm64 version of their libGoogleAnalyticsServices.a, which is really annoying ...it has been weeks since the public the release of Xcode 5GM.

For now, I guess only build for armv7, armv7s or remove google analytics until they get their head out of their pants.

Here is a iOS Getting Started Guide. for implement it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // 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 = 0;

  // Optional: set Logger to VERBOSE for debug information.
  [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

  // Initialize tracker.
  id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];

}

To manually send a screen view, set the screen field values on the tracker, then send the hit:

// May return nil if a tracker has not already been initialized with a
// property ID.
id tracker = [[GAI sharedInstance] defaultTracker];

// This screen name value will remain set on the tracker and sent with
// hits until it is set to a new value or to nil.
[tracker set:kGAIScreenName
       value:@"Home Screen"];

[tracker send:[[GAIDictionaryBuilder createAppView] build]];

Or Automatic Screen Measurement:

Automatically measure views as screens using the GAITrackedViewController class. Have each of your view controllers extend GAITrackedViewController and add a property called screenName. This property will be used to set the screen name field.

//
// MyViewController.h
// An example of using automatic screen tracking in a ViewController.
//
#import "GAITrackedViewController.h"

// Extend the provided GAITrackedViewController for automatic screen
// measurement.
@interface AboutViewController : GAITrackedViewController

@end


//
// MyViewController.m
//
#import "MyViewController.h"
#import "AppDelegate.h"

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set screen name.
    self.screenName = @"Home Screen";
}

// Rest of the ViewController implementation.
@end

Event tracking:

link

To send an event to Google Analytics, use GAIDictionaryBuilder.createEventWithCategory:action:label:value: and send the hit, as in this example:

// May return nil if a tracker has not already been initialized with a property
// ID.
id<GAITracker> = [[GAI sharedInstance] defaultTracker];

[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                      action:@"button_press"  // Event action (required)
                                                       label:@"play"          // Event label
                                                       value:nil] build]];    // Event value

这篇关于iOS 中的 Google Analytics(不工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆