Reputation: 2955
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:
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...
Upvotes: 3
Views: 7114
Reputation: 797
Swift 3
let tracker = GAI.sharedInstance().defaultTracker
tracker?.set("Home Screen", value: "Home Screen")
let screenTracker: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker?.send(screenTracker as! [AnyHashable: Any])
Upvotes: 1
Reputation: 856
hello @Maystro you have to set a date at GoogleAnalytics account TODAY, and then you can see your current events there. if you don't then. I read that you said " Thanks for you input, it was the case. I need to wait for at least 12 h to see the updated data on Google analytics page. – Maystro Jun 27 at 6:34 "
and please apply
[GAI sharedInstance].dispatchInterval = 10;
For quick response each 10sec. and check following block which you applied in your AppDelegate.m by calling
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self startGoogleAnalytics];
....//your other code
}
- (void)startGoogleAnalytics
{
NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// User must be able to opt out of tracking
[GAI sharedInstance].optOut =
![[NSUserDefaults standardUserDefaults] boolForKey:@"allowTracking"];
// Initialise Google Analytics with a 120-second dispatch interval. There is a
// tradeoff between battery usage and timely dispatch.
[GAI sharedInstance].dispatchInterval = 10;
[GAI sharedInstance].trackUncaughtExceptions = YES;
[[GAI sharedInstance] trackerWithName:@"XYZ_Tracker"
trackingId:@"UA-XXXX-Y"];
}
and then send your screen status using
self.screenName = @"Your screen name that should show up in Google Analytics";
or
[[tracker set:kGAIScreenName value:@"Your screenName"]build]];
Hope! Your requirement will be solve by above steps
Upvotes: 0
Reputation: 11161
General Setup
Taken from the Google Analytics iOS documentation.
You might want to add some of them to your didFinishLaunchingWithOptions
// 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. Replace with your tracking ID.
[[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
Additionally you have two options to track view controller events.
Option A - Implementing automatic screen measurement
Let google analytics automatically measure views in your app, have your view controllers extend GAITrackedViewController
. Set a property called screenName
with the name of the screen that you want to appear in your reporting.
#import "GAITrackedViewController.h"
@interface HomeViewController : GAITrackedViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.screenName = @"Your screen name that should show up in Google Analytics";
}
Option B - manually tracking screen events
Manually trigger an event for each of your desired ViewController.
Add the following to your view controller's viewDidAppear
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Your screen name that should show up in Google Analytics"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
Upvotes: 2
Reputation: 10505
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.
Upvotes: 4
Reputation: 2694
I did it like this and it works. For each of my ViewController's viewDidAppear:
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"XXX view"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
And I add this in AppDelegate's didFinishLauching
[GAI sharedInstance].trackUncaughtExceptions = YES;
[[GAI sharedInstance].logger setLogLevel:kGAILogLevelVerbose];
[GAI sharedInstance].dispatchInterval = 20;
[[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXXXXXX-X"];
The kGAILogLevelVerbose logs its activity sending to server every 20s, and so that you can see it. This is for debug purpose in your case only, make sure to comment it out before you submit your app.
In addition, you should be able to see your screen on Real-Time screenView that there's 1 user when you're debugging.
Good Luck.
Upvotes: 2