Cocos2d-X is a game engine framework based on Cocos2d-iPhone, which consists of expanding supported platforms, with multiple choice of programming languages thats shares the same API structure. Programmers can choose to use their preferred language to achieve their targeted platform Native branch written in C++, with a little Java and Objective-C. Lua and javascript are binded as scripting language. HTML5 branch, also known as cocos2d-html5, is written in Javascript, that focuses on desktop browsers XNA port, is written in C#, focus on Windows Phone 7 and XNA. All the branches above are released under MIT License. With cocos2d-x framework, developers can easily create or port their games onto iOS, Android, Bada, BlackBerry Qnx, Marmalade, WindowsXP/Windows7, Linux and Windows8 Metro.

Post tutorial Report RSS 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:

Posted by on - Advanced Server Side Coding

Someone wrote: Download the source code here:
Redtgames.com

  • 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 AppController.mm file add the following code inside didFinishLaunchingWithOptions method including the existing code:

cpp 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 RootViewController.mm add the following method:

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

//Initialise vc

objc code:
- (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 yourRootViewController.mm add a C++ method which will call the above objective c method:
static RootViewController * vc;

cpp code:
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.

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

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

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

Someone wrote: Download the source code here:
Redtgames.com

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Follow Report Profile
Company
cocos2d-x team
Contact
Send Message
Homepage
Cocos2d-x.org
Licence
MIT
Release date
Engine watch
Follow
Tutorial
Browse
Tutorials
Report
Report
Share
Related Engines
cocos2d-x
cocos2d-x MIT
Related Groups
cocos2d-x team
cocos2d-x team Developer