Gameanalytics SDK on Cocos Creator [solved]

Gameanalytics SDK on Cocos Creator [solved]

Hey friends!

I’ve managed to run “Gameanalytics JS SDK” on Cocos Creator for all platforms (not just for HTML5). Tested on Android and Win32. If you can test it for other platforms I would be happy! (23.6 KB)

This is the modified version of “GameAnalytics.node.js” in JS SDK.

Normally it requires window.navigator for several functions but we don’t have in Cocos Creator.
I’ve defined them with regular data:

navigator.appName = "Netscape"
navigator.appVersion = "5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
navigator.userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
navigator.vendor = "Google Inc.";
navigator.platform = "Win32";
navigator.onLine = true;

replaced “localStorage” with “cc.sys.localStorage” and several fixes.


//Require at the beginning of your component script
require("gameanalytics"); // defines for global usage

// then you can use like this:
ga.GameAnalytics.initialize("...", "...");
ga.GameAnalytics.addProgressionEvent(ga.EGAProgressionStatus.Start, "World01", "Level01","Section01");
// you can check any time if it initialized correctly 
if (  ga.GameAnalytics.isSdkReady(true, false) ){
     // do whatever you want
// For example you can put these lines into onLoad function of your component:
// Game goes background, function () {
    ga.GameAnalytics.endSessionImmediate(); // End session

// Game comes foreground, function () {
    ga.GameAnalytics.startSession(); // Start session

Let me know what do you think?

1 Like


Great im going to Test it.
I was wondering it looks like its using web embedded in native so in gameanalytics panel the calls are collected as web or native?



It collects as Javascript SDK.
It needs fixing on navigator.blablabla variables. Now all data collected as windows and unknown device even though I set navigator.platform correctly.



I’ve replaced first section like this:

var navigator_appName       = "";
var navigator_appVersion    = "";
var navigator_userAgent     = "";
var navigator_vendor        = "";
var navigator_platform      = "Win32";
var navigator_online        = true; 

if(cc.sys.platform === cc.sys.WIN32){
    navigator_platform = "Win32";
}else if(cc.sys.platform === cc.sys.ANDROID){
    navigator_platform = "Android";
}else if(cc.sys.platform === cc.sys.IPHONE){
    navigator_platform = "iPhone";
}else if(cc.sys.platform === cc.sys.IPAD){
    navigator_platform = "iPad";
}else if(cc.sys.platform === cc.sys.MACOS){
    navigator_platform = "MacIntel";

It detects platform correct now.



any way of make this work for typescript projects?



I don’t have any experience on typescript :confused: