Gameanalytics SDK on Cocos Creator [solved]

Gameanalytics SDK on Cocos Creator [solved]
0
#1

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!

gameanalytics.zip (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.

Usage:

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

// then you can use like this:
ga.GameAnalytics.setEnabledInfoLog(true);
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
cc.game.on(cc.game.EVENT_HIDE, function () {
    ga.GameAnalytics.endSessionImmediate(); // End session
}.bind(this));

// Game comes foreground
cc.game.on(cc.game.EVENT_SHOW, function () {
    ga.GameAnalytics.startSession(); // Start session
}.bind(this))

Let me know what do you think?

1 Like

#2

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?

0 Likes

#3

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.

0 Likes

#4

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.

0 Likes

#5

any way of make this work for typescript projects?

0 Likes

#6

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

0 Likes