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!

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

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:

1 Like

How do i integrate this in the game?
Do i just put this script into my game’s script folder or is there anything else i need to do since while doing so, when i launch the game using simulator, i just get a black screen.
Sorry if my question may seem dumb, i don’t have that much experience in coding, and thank you very much :slight_smile:

Hi there!

Here is the latest version that I’m using:
gameanalytics.zip (24.6 KB)

It includes gameanalytics.js and analytics_control.js files.
Put them somewhere in your assets folder. Create a node and drag analytics_control component script on attributes panel. You can check the code to see how to use it.

Here is the content of component script:

// Game Analytics API
require("gameanalytics"); // defines window.ga for global usage


cc.Class({
    extends: cc.Component,

    properties: {

        Info_Log:true,

        Analytics_Enabled:true,

        Analytics_Initialized:{
            default:false,
            visible:false,
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
        // Game goes background
        cc.game.on(cc.game.EVENT_HIDE, function () {
            this.End_Analytics_Session();
        }.bind(this));

        // Game comes foreground back
        cc.game.on(cc.game.EVENT_SHOW, function () {
            this.Start_Analytics_Session();
        }.bind(this));

        // For offline event logging
        this.Offline_Log_List = [];

        // Load log data if present before
        this.Load_Offline_Log_List();

    },

    start () {
        // Init on start (make sure you have internet connection)
        this.Init_Analytics
    },

    Init_Analytics(){// Force it if you're sure that you need

        if(!this.Analytics_Initialized){

            if(this.Info_Log){
                ga.GameAnalytics.setEnabledInfoLog(true);
            }

            if(!this.Analytics_Enabled){
                ga.GameAnalytics.setEnabledEventSubmission(false);
            }

            // game key etc...
            ga.GameAnalytics.initialize("xxxxxxxxxxxxx", "yyyyyyyyyyyyyy");

            this.Analytics_Initialized = true;

        }

    },

    // GameAnalytics SDK initialized and ready
    Analytics_Ready(){
        return ga.GameAnalytics.isSdkReady(true, false);
    },

    Start_Analytics_Session(){
        ga.GameAnalytics.startSession(); // Start session on game start
    },

    End_Analytics_Session(){
        ga.GameAnalytics.endSessionImmediate(); // End session on game end or went to background
    },

    Get_User_ID(){
        return ga.GameAnalytics.Get_User_ID();
    },

    // Sample Game Events

    Level_Start(event_detail,scene_name){

        scene_name = scene_name || cc.director.getScene().name;

        if(this.Analytics_Ready()){
            ga.GameAnalytics.addProgressionEvent( ga.EGAProgressionStatus.Start, scene_name , event_detail );
        }
        
    },

    Level_Complete(event_detail,scene_name){

        scene_name = scene_name || cc.director.getScene().name;

        if(this.Analytics_Ready()){
            ga.GameAnalytics.addProgressionEvent( ga.EGAProgressionStatus.Complete, scene_name , event_detail);
        }

        
    },

});

Hope it helps :wink:

1 Like

Thank you very much, It is helping me a lot <3 :blush:

Sorry to bother you again, the older version of the code worked fine, but then i switched to the latest version and the scene with the code won’t run either in android or on web and i have this error : smsg is not defined
i did read the code trying to figure out what is smsg but i don’t get it, would you explain please?
Thank you very much :slight_smile:

Oh I forgot them to remove. Find and remove all function calls starts with “smsg.” in gameanalytics.js. I put them to check intrnet connection when needed. Or I’ll fix it and upload here tomorrow.

1 Like

I’ve removed unnecessary function calls. Here is latest version:
gameanalytics.zip (24.8 KB)

1 Like

thank you very much, i’ll test it out :slight_smile: