Integrating Google analytics to cocos2d-x game

Since releasing my first game as an indie developer, I have been struggling to find out what people are thinking about the game. A few questions hits my mind:

  • What is their behavior in each and every stage.
  • Are they finding it hard or difficult to play.
  • Any specific stage they are stuck at and not able to finish (annoyed).
  • How many times they are losing or wining in a stage
  • Are they even able to finish the complete game.
  • Are they visiting your store and do they know that there is an in app purchase available
To me these data are valuable and they give me an opportunity to improve the game and have a better chance at selling it. Without these information it is like flying a plane in the blind and depending on your luck. We are not big companies, we are either sole developers or team of few enthusiastic entrepreneurs who want to create some awesome games :). We all know how hard this is. Even if we end up creating a reasonable game, competition against big companies are unsustainable. BUT, we can try, lets try.

There may be few frameworks to achieve this, I have decided to go with Google analytics. Lets get started.

  • Get an account for Google analytics HERE
  • Download the iOS SDK from HERE
Add the SDK to your cocos2d-x project.

Google analytics SDK for XCode

In your file add the following code inside didFinishLaunchingWithOptions method including the existing code:

#import "GAI.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
	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 = 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-XXXXXXXX-1"];//you get this key from your analytics account
	return YES;

In your add the following method:

-(void)setCurrentSceneWith:(NSString *)screenName{
    id tracker = [[GAI sharedInstance] defaultTracker];
    [tracker set:kGAIScreenName value:screenName];
    [tracker send:[[GAIDictionaryBuilder createScreenView] build]];

//Initialise vc
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
	if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
		vc = self;
	return self;

Now the trick is to create an cocos2d-x wrapper class so that you can call this objective c method. In your add a C++ method which will call the above objective c method:

static RootViewController * vc;

void setScreenName(std::string screenName){
    NSString* name = [NSString stringWithCString:screenName.c_str() encoding:[NSString defaultCStringEncoding]];
    [vc setCurrentSceneWith:name];

Add a new class named analyticsInterface.cpp (or whatever you want to call it) in your project.

//This is same name as set in the above code in the class.
void setScreenName(std::string screenName);

//Just pass the screen name
void callSetScreenName(std::string screenName){

From your GameLayer.cpp simply call the above method and pass the current screen name:

#include "analyticsInterface.h"
bool GameLayer::init(){
		setScreenName("Stage 1");

Thats it! Your Google Analytics account will show how many players are visiting your current layer. Below is a sample captured from my own device when I was testing my app before submitting to the app store.

Google analytics sample from What The Hell?! app

For your reference, please check out the code here:

Posted in Tutorials Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


Join 1 other subscriber