I am trying to implement google analytics.. could you guys please help me
-(void) setGoogleAnalytics{
// Initialize tracker.
self.tracker = [[GAI sharedInstance] trackerWithName:@"ipad app"
trackingId:kTrackingID];
NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// User must be able to opt out of tracking
[GAI sharedInstance].optOut =
![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
// 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 = 5;
// Optional: set Logger to VERBOSE for debug information.
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
[[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}
and calling it in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self setGoogleAnalytics];
//
//
//
}
Inside My ViewController implementation
[self dispatchEvent:@"Purchase Done"];
[self trackViewName:NSStringFromClass([self class])];
-(void) trackViewName:(NSString *) strClassName{
[[GAI sharedInstance] defaultTracker];
self.screenName=[NSString stringWithFormat:@"%@",strClassName];
[self.tracker send:[[NSDictionary alloc] initWithObjectsAndKeys:strClassName,@"ViewName", nil]];
[[GAI sharedInstance] dispatch];
}
- (void)dispatchEvent:(NSString *)strButtonText{
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:strButtonText // Event label
value:nil] build]]; // Event value = [[GAI sharedInstance] defaultTracker];
[[GAI sharedInstance] dispatch];
}
Which version of google analytics, I should download currently I have downloaded google GoogleAnalyticsServicesiOS_3.01.zip (Recommended) as I dont want to work with the beta version GoogleAnalyticsiOS_2.0beta4.zip
I had the same problem with 3.01. My problem was actually in the Google Analytics admin section.
I had a profile setup for mobile (which was actually setup as if it were a website) which worked with version 1.x. However it appears that Google has now implented "mobile" profiles as well. I believe that the 3.x mobile SDKs cannot track to "Web" profiles.
Create a new profile by following the instructions here. And then use the new tracking Id and it should start tracking.
Note: Do not set the dispatchInterval to 0, as others have suggested, this also prevented tracking, set it to 1. This fixed everything for me.
UPDATE -- Google Analytics SDK for iOS v3
So I'm using v3, and there is not any problem:
I'm implemented it in the AppDelegate. In .h file:
#import "GAI.h"
@property (nonatomic,assign) id<GAITracker> tracker; // I'm not using ARC (assign)
.m:
#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"
// GOOGLE ANALYTICS
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 0;
tracker = [[GAI sharedInstance] trackerWithTrackingId:@"yourGAID"];
And write a method like this:
- (void) sendGoogleAnalyticsView:(NSString*)viewName{
[tracker set:kGAIScreenName value:viewName];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
[[GAI sharedInstance] dispatch]; // this will force track your views.
}
Old answer:
See this answer below this link, if you do it the same as I told in this answer it must work
Another stack-overflow answered question about google-analytics
and use these methods:
[GAI sharedInstance].optOut = YES;
[GAI sharedInstance].dispatchInterval = 0;
[GAI sharedInstance].trackUncaughtExceptions = YES;
tracker = [[GAI sharedInstance] trackerWithTrackingId:@"YOUR TRACKERID"];
[tracker sendView:@"Your View name"];
[tracker sendEventWithCategory:@"YOUR CATEGORY" withAction:@"YOUR ACTION" withLabel:nil withValue:nil];
Download GoogleAnalyticsiOS_2.0beta4.zip from this link this will contain those classes what you need, and it will work perfectly. Be careful, google analytics got a lead time, to show you information, about real time. And not real time datas will show only a day after
EDIT for 3.0:
I found some probably useful things for you:
We have just come across this issue and this is slightly out of date so here is an updated answer. The issue we were having after following the instructions on the Google Analytics website, they instruct you to add the following files
GAI.h
,GAIDictionaryBuilder.h
,GAILogger.h
,GAITrackedViewController.h
,GAITracker.h
andlibGoogleAnalytics_debug.a
library. What they completely forget to include on the website instructions is the one where you have to includelibGoogleAnalyticsServices.a
library. This is included in the zipped download but there is no instructions to indicate to include this in the debug version.Note : In the readme.txt
libGoogleAnalyticsServices.a
is just referred to aslibGoogleAnalytics.a
Google have failed to update their documentation to include the new name or the correct instructions that indicate this is required in debug.Files and Libraries that most be included
GAI.h
GAIDictionaryBuilder.h
GAIFields.h
GAILogger.h
GAITrackedViewController.h
GAITracker.h
libGoogleAnalytics.a // Also know as libGoogleAnalyticsServices.a
libGoogleAnalytics_debug.a
plus information:
I'm pretty sure google has not yet provided a arm64 version of their
libGoogleAnalyticsServices.a
, which is really annoying ...it has been weeks since the public the release of Xcode 5GM.For now, I guess only build for armv7, armv7s or remove google analytics until they get their head out of their pants.
Here is a iOS Getting Started Guide. for implement it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 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 = 0;
// Optional: set Logger to VERBOSE for debug information.
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
// Initialize tracker.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
}
To manually send a screen view, set the screen field values on the tracker, then send the hit:
// May return nil if a tracker has not already been initialized with a
// property ID.
id tracker = [[GAI sharedInstance] defaultTracker];
// This screen name value will remain set on the tracker and sent with
// hits until it is set to a new value or to nil.
[tracker set:kGAIScreenName
value:@"Home Screen"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
Or Automatic Screen Measurement:
Automatically measure views as screens using the
GAITrackedViewController
class. Have each of your view controllers extendGAITrackedViewController
and add a property called screenName. This property will be used to set the screen name field.
//
// MyViewController.h
// An example of using automatic screen tracking in a ViewController.
//
#import "GAITrackedViewController.h"
// Extend the provided GAITrackedViewController for automatic screen
// measurement.
@interface AboutViewController : GAITrackedViewController
@end
//
// MyViewController.m
//
#import "MyViewController.h"
#import "AppDelegate.h"
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set screen name.
self.screenName = @"Home Screen";
}
// Rest of the ViewController implementation.
@end
Event tracking:
link
To send an event to Google Analytics, use GAIDictionaryBuilder.createEventWithCategory:action:label:value: and send the hit, as in this example:
// May return nil if a tracker has not already been initialized with a property
// ID.
id<GAITracker> = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:@"play" // Event label
value:nil] build]]; // Event value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With