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

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

问题描述

   - (void)setGoogleAnalytics {

//初始化跟踪器。
self.tracker = [[GAI sharedInstance] trackerWithName:@ipad app
trackingId:kTrackingID];

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

[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
//用户必须能够退出跟踪

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

//可选:自动将未捕获的例外发送给Google Analytics。
[GAI sharedInstance] .trackUncaughtExceptions = YES;

//可选:将Google Analytics派送间隔设置为例如20秒。
[GAI sharedInstance] .dispatchInterval = 5;

//可选:将记录器设置为VERBOSE以获取调试信息。
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

[[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}

并在
中进行调用

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

在我的ViewController实现里面

  [self dispatchEvent:@Purchase Done]; 

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

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


$ b - (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 //事件标签
value:nil] build]]; //事件值= [[GAI sharedInstance] defaultTracker];

[[GAI sharedInstance] dispatch];
}

目前,我应该下载Google Analytics(分析)服务iOS_3的哪个版本。 01.zip(推荐),因为我不想使用测试版GoogleAnalyticsiOS_2.0beta4.zip

解决方案

UPDATE - Google AnalyticsSDK for iOS v3 所以我使用v3,并没有任何问题:



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

  #importGAI.h
@property(nonatomic,assign)id< GAITracker> ;跟踪器; //我没有使用ARC(转让)

.m:

  #importGAIDictionaryBuilder.h
#importGAIFields.h

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

然后编写一个像这样的方法:

<$ pca p> - (void)sendGoogleAnalyticsView:(NSString *)viewName {
[tracker set:kGAIScreenName value:viewName];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
[[GAI sharedInstance] dispatch]; //这将强制跟踪您的观点。





$ b旧的答案: p>

在这个链接下面看到这个答案,如果你这样做,就像我在这个答案中所说的一样,它必须工作


要向Google Analytics发送活动,请使用
GAIDictionaryBuilder.createEventWithCategory:action:label:value:和
发送命中,如本例中:



  //如果跟踪器尚未用属性$初始化,可能会返回nil 
// ID。
id< GAITracker> = [[GAI sharedInstance] defaultTracker];

[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@ui_action// Event category(required)
action:@button_press// Event action(required)
label :@play//事件标签
value:nil] build]]; //事件值


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天全站免登陆