Google Analytics在iOS中不起作用 [英] Google Analytics doesn't work in iOS

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

问题描述

我在Google Analytics(分析)帐户中的配置:
$ b



我的代码:

   - (BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

GAI * gai = [GAI sharedInstance];
//gai.optOut = YES;
gai.trackUncaughtExceptions = YES; //报告未捕获的异常
gai.logger.logLevel = kGAILogLevelVerbose; //在应用发布之前删除
[[GAI sharedInstance] setDispatchInterval:10.0];
[[GAI sharedInstance] trackerWithTrackingId:@UA-xxxxxx-3];

返回YES;

在视图控制器中:




  • (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

      ID< GAITracker> tracker = [[GAI sharedInstance] defaultTracker]; 
    [tracker set:kGAIScreenName value:@Detail Screen];
    [[GAI sharedInstance] .defaultTracker send:[[GAIDictionaryBuilder createScreenView] build]];

    }




  • 它在我的控制台显示:

      2015-06-24 15:03:36.592 GoogleAnalyticsDemoiOS [ GoogleAnalyticsDemoiOS [9540:264576] VERBOSE:GoogleAnalytics 3.12 + [GAITrackerModel初始化](GAITrackerModel.m:88):idfa class missing,不会收集idfa 
    2015-06-24 15:03:36.619 GoogleAnalyticsDemoiOS [9540:264576] INFO:GoogleAnalytics 3.12 - [GAIReachabilityChecker reachabilityFlagsChanged:](GAIReachabilityChecker.m:159):可达性标志更新:0X000002
    2015-06-24 15:03:36.644 GoogleAnalyticsDemoiOS [9540:264684] VERBOSE:GoogleAnalytics 3.12 - [GAIBatchingDispatcher坚持:](GAIBatchingDispatcher.m:517):保存的命中:{
    parameters = {
    & _crc= 0;
    & _s= 1;
    & _u=.etnoKo9L;
    & _v=mi3.1.2;
    & a= 800078077;
    & aid=com.mycompany.app;
    & an= GoogleAnalyticsDemoiOS;
    & av=1.0;
    & cd=细节屏幕;
    & cid=caf03508-eba6-42f8-9600-3832ce042907;
    & dm=x86_64;
    & ds= app;
    & sr= 750x1334;
    & t= screenview;
    & tid=UA-xxxxxxxx-3;
    & ul= en;
    & v= 1;
    & z= 663648936779959001;
    gaiVersion =3.12;
    };
    timestamp =2015-06-24 07:03:36 +0000;
    }
    2015-06-24 15:03:36.644 Google Analytics(分析)演示文稿[9540:264684] VERBOSE: iAd框架未链接。有关说明,请参阅http://goo.gl/426NGa。
    2015-06-24 15:03:46.642 GoogleAnalyticsDemoiOS [9540:264684] VERBOSE:GoogleAnalytics 3.12 - [GAIRequestBuilder requestGetUrl:payload:](GAIRequestBuilder.m:195):为https://ssl.google构建URLRequest -analytics.com/collect
    2015-06-24 15:03:46.643 GoogleAnalyticsDemoiOS [9540:264684] VERBOSE:GoogleAnalytics 3.12 - [GAIBatchingDispatcher dispatchWithCompletionHandler:](GAIBatchingDispatcher.m:632):发送命中GET :https://ssl.google-analytics.com/collect?av=1.0&cid=caf03508-eba6-42f8-9600-3832ce042907&tid=UA-64362429-3&a=800078077&dm=x86_64&cd=细节+画面& T公司=屏幕视图&安培;援助= com.wow.touch&安培; UL = EN&安培; _U = .etnoKo9L&安培; DS =应用&安培; SR = 750x1334&安培; v = 1和; _s = 1&安培; _crc = 0&安培;一个= GoogleAnalyticsDemoiOS&安培; _v = Google Analytics(分析)3.12 - [GAIBatchingDispatcher didSendHits:response:data:错误:](GAIBatchingDispatcher.m:2 26):命中(HTTP)状态-1
    2015-06-24 15:03:46.710 GoogleAnalyticsDemoiOS [9540:264684]信息:GoogleAnalytics 3.12 - [GAIBatchingDispatcher deleteHits:](GAIBatchingDispatcher.m:529) :hit(s)成功删除
    2015-06-24 15:03:46.711 GoogleAnalyticsDemoiOS [9540:264684]信息:GoogleAnalytics 3.12 - [GAIBatchingDispatcher didSendHits:](GAIBatchingDispatcher.m:237):1 hit(s)发送

    但在实时视图中,我总是有0个活跃用户。任何想法?谢谢

    解决方案

    ---更新---



    这是我如何修正的:

    1 - 在AppDelegate中添加这个属性和#define指令(如果你没有在你的应用程序中):


      #define IS_IOS7_AND_UP([[UIDevice currentDevice] .systemVersion floatValue]> = 7.0)

    @界面AppDelegate()

    //用于在后台发送Google Analytics流量。
    @property(nonatomic,assign)BOOL okToWait;
    @property(nonatomic,copy)void(^ dispatchHandler)(GAIDispatchResult result);

    @end

    2-在viewDidLoad中我使用dispatchInterval作为20和Initialize跟踪器共享实例:

      [GAI sharedInstance] .dispatchInterval = 20; 
    // [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
    //初始化跟踪器。
    [[GAI sharedInstance] trackerWithTrackingId:MY_GoogleAnalyticsID];



    <3> - 为您的AppDelegate添加此方法:

      //此方法在后台发送匹配,直到我们被告知停止后台处理,
    //我们遇到错误,或者我们没有匹配。我们使用此功能发送任何未完成的Google Analytics(分析)
    //数据,因为应用一旦处于后台就不会有机会。
    - (void)sendHitsInBackground {
    self.okToWait = YES;
    __weak AppDelegate * weakSelf = self;
    __block UIBackgroundTaskIdentifier backgroundTaskId =
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
    weakSelf.okToWait = NO;
    }];

    if(backgroundTaskId == UIBackgroundTaskInvalid){
    return;


    self.dispatchHandler = ^(GAIDispatchResult结果){
    //如果最后一次发送成功,并且我们仍然可以保持在后台,那么启动
    // //。
    if(result == kGAIDispatchGood&&&weakSelf.okToWait){
    [[GAI sharedInstance] dispatchWithCompletionHandler:weakSelf.dispatchHandler];
    } else {
    [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId];
    }
    };
    [[GAI sharedInstance] dispatchWithCompletionHandler:self.dispatchHandler];
    }



    $在applicationDidEnterBackground调用sendHitsInBackground方法结束时:

       - (void)applicationDidEnterBackground:(UIApplication *)application {
    //
    //。 。 。
    //
    if(IS_IOS7_AND_UP)
    [self sendHitsInBackground];



    $ b 5添加performFetchWithCompletionHandler方法并调用sendHitsInBackground:

      //如果应用程序在没有网络连接时被发送到后台,我们将使用
    //后台数据获取机制发送任何未完成的Google Analytics数据。请注意,
    //此应用程序在项目的功能部分中启用了后台数据获取。
    - (void)application:(UIApplication *)application performFetchWithCompletionHandler :( void(^)(UIBackgroundFetchResult))completionHandler {
    $ b $ if if(IS_IOS7_AND_UP)
    [self sendHitsInBackground];

    completionHandler(UIBackgroundFetchResultNewData);
    }

    ,Google Analytics(分析)将在此工作后开始工作。我在多个应用程序中尝试此操作。如果您有任何问题,请告诉我。

    无论如何,我不相信只有1个分析工具,需要一些替代方案。我建议你使用Flurry和/或Crashlytics(Fabric)。 Flurry被雅虎收购,Crashlytics通过Twitter收购。为什么Fabric,因为它有很多功能,你会爱上它!



    在这里您可以试用它们: https://fabric.io



    Flurry: < a href =http://www.flurry.com =nofollow> http://www.flurry.com



    你可以在

      didFinishLaunchingWithOptions ... 

    [self yourAnalyticServices];

    您的代码如下所示:

       - (void)yourAnalyticServices {
    // Flurry Analytics
    [Flurry setAppVersion:kAppVersion];
    [Flurry startSession:kFlurryAnalyticsID];
    // [Flurry setCrashReportingEnabled:NO];
    [Flurry setUserID:[NSString stringWithFormat:@%@,kUserUUID]];


    // Google Analytics
    //可选:自动将未捕获的例外发送给Google Analytics。
    // [GAI sharedInstance] .trackUncaughtExceptions = YES;
    //可选:将Google Analytics派送间隔设置为20秒。
    [GAI sharedInstance] .dispatchInterval = 20;
    //可选:将记录器设置为VERBOSE以获取调试信息。
    // [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
    //初始化跟踪器。
    [[GAI sharedInstance] trackerWithTrackingId:MY_GoogleAnalyticsID];

    // Crashlytics
    [Fabric with:[CrashlyticsKit]];
    }


    My configuration in Google Analytics account:

    My code:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        GAI *gai = [GAI sharedInstance];
        //gai.optOut = YES;
        gai.trackUncaughtExceptions = YES;  // report uncaught exceptions
        gai.logger.logLevel = kGAILogLevelVerbose;  // remove before app release
        [[GAI sharedInstance] setDispatchInterval:10.0];
        [[GAI sharedInstance] trackerWithTrackingId:@"UA-xxxxxx-3"];
    
        return YES;
    }
    

    In view controller:

    • (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

      id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
      [tracker set:kGAIScreenName value:@"Detail Screen"];
      [[GAI sharedInstance].defaultTracker send:[[GAIDictionaryBuilder createScreenView] build]];
      

      }

    It did shows in my console:

    2015-06-24 15:03:36.592 GoogleAnalyticsDemoiOS[9540:264576] VERBOSE: GoogleAnalytics 3.12 +[GAITrackerModel initialize] (GAITrackerModel.m:88): idfa class missing, won't collect idfa
    2015-06-24 15:03:36.619 GoogleAnalyticsDemoiOS[9540:264576] INFO: GoogleAnalytics 3.12 -[GAIReachabilityChecker reachabilityFlagsChanged:] (GAIReachabilityChecker.m:159): Reachability flags update: 0X000002
    2015-06-24 15:03:36.644 GoogleAnalyticsDemoiOS[9540:264684] VERBOSE: GoogleAnalytics 3.12 -[GAIBatchingDispatcher persist:] (GAIBatchingDispatcher.m:517): Saved hit: {
        parameters =     {
            "&_crc" = 0;
            "&_s" = 1;
            "&_u" = ".etnoKo9L";
            "&_v" = "mi3.1.2";
            "&a" = 800078077;
            "&aid" = "com.mycompany.app";
            "&an" = GoogleAnalyticsDemoiOS;
            "&av" = "1.0";
            "&cd" = "Detail Screen";
            "&cid" = "caf03508-eba6-42f8-9600-3832ce042907";
            "&dm" = "x86_64";
            "&ds" = app;
            "&sr" = 750x1334;
            "&t" = screenview;
            "&tid" = "UA-xxxxxxxx-3";
            "&ul" = en;
            "&v" = 1;
            "&z" = 663648936779959001;
            gaiVersion = "3.12";
        };
        timestamp = "2015-06-24 07:03:36 +0000";
    }
    2015-06-24 15:03:36.644 GoogleAnalyticsDemoiOS[9540:264684] VERBOSE: GoogleAnalytics 3.12 __70-[GAIBatchingDispatcher checkIAdCampaignAttributionWithHitParameters:]_block_invoke (GAIBatchingDispatcher.m:749): iAd campaign tracking disabled because the iAd framework is not linked. See http://goo.gl/426NGa for instructions.
    2015-06-24 15:03:46.642 GoogleAnalyticsDemoiOS[9540:264684] VERBOSE: GoogleAnalytics 3.12 -[GAIRequestBuilder requestGetUrl:payload:] (GAIRequestBuilder.m:195): building URLRequest for https://ssl.google-analytics.com/collect
    2015-06-24 15:03:46.643 GoogleAnalyticsDemoiOS[9540:264684] VERBOSE: GoogleAnalytics 3.12 -[GAIBatchingDispatcher dispatchWithCompletionHandler:] (GAIBatchingDispatcher.m:632): Sending hit(s) GET: https://ssl.google-analytics.com/collect?av=1.0&cid=caf03508-eba6-42f8-9600-3832ce042907&tid=UA-64362429-3&a=800078077&dm=x86_64&cd=Detail+Screen&t=screenview&aid=com.wow.touch&ul=en&_u=.etnoKo9L&ds=app&sr=750x1334&v=1&_s=1&_crc=0&an=GoogleAnalyticsDemoiOS&_v=mi3.1.2&ht=1435129416641&qt=10001&z=663648936779959001
    2015-06-24 15:03:46.710 GoogleAnalyticsDemoiOS[9540:264576] INFO: GoogleAnalytics 3.12 -[GAIBatchingDispatcher didSendHits:response:data:error:] (GAIBatchingDispatcher.m:226): Hit(s) dispatched: HTTP status -1
    2015-06-24 15:03:46.710 GoogleAnalyticsDemoiOS[9540:264684] INFO: GoogleAnalytics 3.12 -[GAIBatchingDispatcher deleteHits:] (GAIBatchingDispatcher.m:529): hit(s) Successfully deleted
    2015-06-24 15:03:46.711 GoogleAnalyticsDemoiOS[9540:264684] INFO: GoogleAnalytics 3.12 -[GAIBatchingDispatcher didSendHits:] (GAIBatchingDispatcher.m:237): 1 hit(s) sent
    

    But in Realtime view, I always got 0 active users. Any idea? Thanks

    解决方案

    --- UPDATED ---

    Here is how I fixed :

    1 - add this properties and #define directive (if you don't have in your application) in AppDelegate :

    #define IS_IOS7_AND_UP                      ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
    
    @interface AppDelegate ()
    
    // Used for sending Google Analytics traffic in the background.
    @property (nonatomic, assign)   BOOL okToWait;
    @property (nonatomic, copy)     void (^dispatchHandler)(GAIDispatchResult result);
    
    @end
    

    2- in viewDidLoad I use dispatchInterval as 20 and Initialize the tracker shared instance :

    [GAI sharedInstance].dispatchInterval = 20;
    //[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
    // Initialize tracker.
    [[GAI sharedInstance] trackerWithTrackingId:MY_GoogleAnalyticsID];
    

    3 - add this method for your AppDelegate :

    // This method sends hits in the background until either we're told to stop background processing,
    // we run into an error, or we run out of hits.  We use this to send any pending Google Analytics
    // data since the app won't get a chance once it's in the background.
    - (void)sendHitsInBackground {
        self.okToWait = YES;
        __weak AppDelegate *weakSelf = self;
        __block UIBackgroundTaskIdentifier backgroundTaskId =
        [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            weakSelf.okToWait = NO;
        }];
    
        if (backgroundTaskId == UIBackgroundTaskInvalid) {
            return;
        }
    
        self.dispatchHandler = ^(GAIDispatchResult result) {
            // If the last dispatch succeeded, and we're still OK to stay in the background then kick off
            // again.
            if (result == kGAIDispatchGood && weakSelf.okToWait ) {
                [[GAI sharedInstance] dispatchWithCompletionHandler:weakSelf.dispatchHandler];
            } else {
                [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId];
            }
        };
        [[GAI sharedInstance] dispatchWithCompletionHandler:self.dispatchHandler];
    }
    

    4- in the end of applicationDidEnterBackground call sendHitsInBackground method :

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        //
        // . . .
        //
        if (IS_IOS7_AND_UP)
            [self sendHitsInBackground];
    }
    

    5 - Add performFetchWithCompletionHandler method and call sendHitsInBackground :

    // In case the app was sent into the background when there was no network connection, we will use
    // the background data fetching mechanism to send any pending Google Analytics data.  Note that
    // this app has turned on background data fetching in the capabilities section of the project.
    - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
        if (IS_IOS7_AND_UP)
            [self sendHitsInBackground];
    
        completionHandler(UIBackgroundFetchResultNewData);
    }
    

    and Google Analytics will start working after this effort. I try this in more than one application. If you have any question, please let me know.

    Anyway, I don't trust only 1 analytic tool, need some alternatives. I suggest you use Flurry and/or Crashlytics (Fabric). Flurry acquired by Yahoo, Crashlytics acquired by Twitter. Why Fabric, because it has so many features that you'll fall in love with!

    Here you can try them :

    Fabric : https://fabric.io

    Flurry : http://www.flurry.com

    You can call them in

    didFinishLaunchingWithOptions ...
    
      [self yourAnalyticServices];
    

    And your code looks like this :

    - (void)yourAnalyticServices {
        //Flurry Analytics
        [Flurry setAppVersion:kAppVersion];
        [Flurry startSession:kFlurryAnalyticsID];
    //    [Flurry setCrashReportingEnabled:NO];
        [Flurry setUserID:[NSString stringWithFormat:@"%@", kUserUUID]];
    
    
        //Google Analytics
        // 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 Logger to VERBOSE for debug information.
        //[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
        // Initialize tracker.
        [[GAI sharedInstance] trackerWithTrackingId:MY_GoogleAnalyticsID];
    
        //Crashlytics
        [Fabric with:@[CrashlyticsKit]];
    }
    

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

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