Search code examples
iosgoogle-analytics-api

Google analytics ios screen views


I've already integrated google analytics SDK v3 in my iOS application and I need now to track the screen views. Here is what I'm currently seeing on their website:

enter image description here Here is the code I'm using(Manual Screen Measurement):

+ (void)setupGoogleAnalytics
{
    // User must be able to opt out of tracking
    [GAI sharedInstance].optOut = NO;
    // Initialize Google Analytics with a 120-second dispatch interval. There is a
    // tradeoff between battery usage and timely dispatch.
    [GAI sharedInstance].dispatchInterval = 120;
    [GAI sharedInstance].trackUncaughtExceptions = YES;
    [[GAI sharedInstance].logger setLogLevel:kGAILogLevelVerbose];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    [[GAI sharedInstance] trackerWithName:[infoDictionary objectForKey:@"CFBundleDisplayName"] trackingId:GOOGLE_ANALYTICS_TOKEN];
    [[GAI sharedInstance].defaultTracker set:kGAIAppVersion value:[infoDictionary objectForKey:@"CFBundleShortVersionString"]];

}

 + (void)dispatchLogView:(NSString *)viewName Category:(NSString *)category  action:(NSString *)action label:(NSString *)label value:(NSNumber*)value withProperties:(NSDictionary *)dictProp
{
    NSMutableDictionary *event = [[GAIDictionaryBuilder createEventWithCategory:category
                                            action:action
                                             label:label
                                             value:value] build];
    [[GAI sharedInstance].defaultTracker send:event];

    [[GAI sharedInstance].defaultTracker set:kGAIScreenName value:viewName];
    [[GAI sharedInstance].defaultTracker set:kGAIDescription value:viewName];
    [[GAI sharedInstance].defaultTracker send:[[GAIDictionaryBuilder createAppView] build]];

    [[GAI sharedInstance] dispatch];
}

SetupGoogleAnalytics is called on didFinishLaunchingWithOptions. After making these changes, I'm still seeing zero under the "Screen Views"&"Screens/Session" sections. Honestly, I don't know what I'm missing to have it working properly. Here is the link I used to integrate it https://developers.google.com/analytics/devguides/collection/ios/v3/screens#manual

Does anyone encounter the same problem before? Does anyone have any input on this?

Thanks in advance for your help...


Solution

  • I have tried the following method in order to be able to track the screens on Google Analytics. You may try it out to see if it works for you.

    First of all, make sure that the header files and also the project has been configured properly according to the guide provided by Google: Google Analytics SDK for iOS v3 - Getting Started

    Next, under the viewDidLoad for the screen that you want to track:-

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        id tracker = [[GAI sharedInstance] defaultTracker];
        [tracker set:kGAIScreenName value:@"Home Screen"];
        [tracker send:[[GAIDictionaryBuilder createAppView] build]];
    }
    

    Under the viewDidAppear:-

    -(void)viewDidAppear:(BOOL)animated{
    
        self.screenName = @"Home Screen";
        [super viewDidAppear:animated];
    }
    

    It is no mistaken. self.screenName must be placed before [super viewDidAppear:animated]; in order for the screen to be tracked on Google Analytics. It is weird but it can only work in some special situations on my own project. May be it will work on your project too.

    Note: The screen name for both the viewDidload and viewDidAppear should be the same.