2015-06-24 14 views
6

La mia configurazione in account Google Analytics:Google Analytics non funziona in iOS

enter image description here

Il mio codice:

- (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 controller della vista:

  • (vuoto) viewWillAppear: (BOOL) animato { [super viewWillAppear: animato ];

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

    }

ha fatto spettacoli in mia 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 

Ma in vista in tempo reale, ho sempre ricevuto 0 utenti attivi. Qualche idea? Grazie

+1

quanto tempo è stato creato l'account? ci vogliono 48-72 ore perché inizi a registrare i dati – DaImTo

+0

Ho lo stesso problema, ha funzionato dopo 48 ore come ha detto @DaImTo? –

risposta

3

--- --- AGGIORNATO

Ecco come ho risolto:

1 - aggiungere gli annunci di questo, e la direttiva # define (se non avete nella vostra applicazione) 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 io uso dispatchInterval come 20 e inizializzare il tracker istanza condivisa:

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

3 - aggiungere questo metodo per l'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- alla fine del applicationDidEnterBackground chiamare il metodo sendHitsInBackground:

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

5 - Aggiungi metodo performFetchWithCompletionHandler e chiamare 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); 
} 

e Google Analytics inizieranno a funzionare dopo questo sforzo. Ci provo in più di un'applicazione. Se avete qualche domanda, per favore fatemelo sapere.

In ogni caso, non mi fido solo di uno strumento analitico, ho bisogno di alcune alternative. Ti suggerisco di usare Flurry e/o Crashlytics (Fabric). Flurry acquisita da Yahoo, Crashlytics acquisita da Twitter. Perché Fabric, perché ha così tante funzioni di cui ti innamorerai!

Qui potete provarli:

Tessuto:https://fabric.io

Flurry:http://www.flurry.com

Potete chiamarli in

didFinishLaunchingWithOptions ... 

    [self yourAnalyticServices]; 

E il codice simile a questo :

- (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]]; 
} 
+0

Grazie per la risposta. Probabilmente lo prenderò in considerazione in un'altra applicazione. Ma il mio cliente richiede Google Analytics in questa applicazione. –

+0

Prego @bagusflyer, aggiorno la mia risposta. – mgyky

+0

Non penso che un suggerimento per usare un altro strumento debba essere considerato una risposta corretta per questa domanda. – Donovan

2

Nel AppDElegate.m di file:

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

In ViewController.h

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

@interface FirstViewController : GAITrackedViewController 


@end 

In ViewController.m

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

Provalo. Questo ha funzionato bene per me. Ho provato più di tre app. E tutti stanno lavorando per il tempo reale. Se il tuo account per l'app è nuovo, potresti dover attendere 24 ore o più per vedere il risultato. A volte ci vuole tempo per visualizzare i dati in tempo reale senza alcun motivo.