Google Analytics不适用于iOS

时间:2015-06-24 07:08:02

标签: ios google-analytics

我在Google Analytics帐户中的配置:

enter image description here

我的代码:

curl -w "\n" --cookie cookie.txt \
  -H "Accept: application/json" \
  -F "deployment-name=rest-test" \
  -F "enable-duplicate-filtering=false" \
  -F "deploy-changed-only=false" \
  -F "process.bpmn=@$PROCESS" \
  $API/engine/engine/default/deployment/create

在视图控制器中:

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

    - (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;
    }
    

    }

它确实在我的控制台中显示:

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

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

2 个答案:

答案 0 :(得分:3)

---更新---

以下是我修复的方法:

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

#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
在viewDidLoad中使用dispatchInterval作为20并初始化跟踪器共享实例:

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

3 - 为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-在applicationDidEnterBackground结束时调用sendHitsInBackground方法:

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

5 - 添加performFetchWithCompletionHandler方法并调用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);
}

并且谷歌分析将在这项工作之后开始工作。我在多个应用程序中尝试这个。如果您有任何疑问,请告诉我。

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

在这里你可以试试:

面料: https://fabric.io

乱舞: 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
    // 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]];
}

答案 1 :(得分:2)

AppDElegate.m 文件中:

#import "AppDelegate.h"
#import "GAI.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   [GAI sharedInstance].trackUncaughtExceptions  = YES;

   [GAI sharedInstance].dispatchInterval = 1;

   [[[GAI sharedInstance] logger]setLogLevel:kGAILogLevelVerbose];

   id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"TrackingId"];

   [GAI sharedInstance].defaultTracker = tracker;

   return YES;
}

ViewController.h

#import <UIKit/UIKit.h>
#import "GAITrackedViewController.h"

@interface FirstViewController : GAITrackedViewController


@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.screenName = @"RED Screen";
}

试一试。这对我很有用。我尝试了三个以上的应用程序。而且所有人都在实时工作。如果您的您的应用的帐户是新的,那么您可能需要等待24小时或更长时间才能看到结果。有时需要时间来显示实时数据而无需任何理由。