SDK Features
Debugging
The SDK is designed to be as silent as possible and use very few resources. You will therefore not get much information by default in your development console.
We have 2 different debug log types that can be enabled / disabled (at any time).
- info log
- verbose log
Info log
Short messages will be output when enabled explaining when some action is being performed by the SDK. Sometimes cropping text / values to make it more readable.
Enable info log when implementing the SDK – remember to turn it off in production!
- C++
- Javascript
- Lua
// C++
gameanalytics::cocos2d::GameAnalytics::setEnabledInfoLog(true);
// JS
ga.GameAnalytics.setEnabledInfoLog(true);
-- LUA
ga.GameAnalytics:setEnabledInfoLog(true)
Sample log output:
Info/GameAnalytics: Add DESIGN event: {eventId:someEvent, value:0}
Info/GameAnalytics: Add DESIGN event: {eventId:someOtherEvent, value:100}
Info/GameAnalytics: Add ERROR event: {severity:info, message:This is some in}
Verbose Log
Console output when each event is added (all fields) in JSON string format. This is the data being submitted to the GA servers for each event.
Enable verbose log when troubleshooting events.
- C++
- Javascript
- Lua
// C++
gameanalytics::cocos2d::GameAnalytics::setEnabledVerboseLog(true);
// JS
ga.GameAnalytics.setEnabledVerboseLog(true);
-- LUA
ga.setEnabledVerboseLog(true)
This can result in a lot of text. When troubleshooting/debugging events it is therefore recommended to enable/disable when performing the action that need inspection.
- C++
- Javascript
- Lua
// enable verbose log
gameanalytics::cocos2d::GameAnalytics::setEnabledVerboseLog(true);
// add event you need to troubleshoot / inspect
gameanalytics::cocos2d::GameAnalytics::addDesignEvent("Some:Event", 100);
// disable verbose log
gameanalytics::cocos2d::GameAnalytics::setEnabledVerboseLog(false);
// enable verbose log
ga.GameAnalytics.setEnabledVerboseLog(true);
// add event you need to troubleshoot / inspect
ga.GameAnalytics.addDesignEvent("Some:Event", 100);
// disable verbose log
ga.GameAnalytics.setEnabledVerboseLog(false);
-- enable verbose log
ga.GameAnalytics:setEnabledVerboseLog(true)
-- add event you need to troubleshoot / inspect
ga.GameAnalytics:addDesignEvent("Some:Event", 100)
-- disable verbose log
ga.GameAnalytics:setEnabledVerboseLog(false)
Verify
Enable the Info Log to verify that events are being sent from your game project without any issues being reported.
Events submitted should register after a minor delay in our realtime dashboard in the GameAnalytics tool.
Read more about the realtime dashboard and our data processing.
Session Handling
By default the SDK will handle session start/end automatically, but it is also possible to manually control this yourself.
Be aware that the initialization will always automatically start the first session even with manual session handling.
If you are using ads in your game there is a big possibility that the onPause() and onStop() events will be triggered when the ads are showing which will interfere with the session length for information on how to fix this to look here.
Automatic Session Handling
The automatic session handling will track the focused time the user is spending in your game – from game launch to the user leaving the game.
session start
On Android a new session will start once the game is launched or when the app is resuming if there is no current session.
session end
A session will end once the game is going to homescreen (or is not visible anymore).
It will end the session at once if the application received the onStop event from the game activity.
Manual Session Handling
If you just want to be in control when to start and end sessions) you can enable/disable manual session handling by calling this at any given time:
- C++
- Javascript
- Lua
// C++
gameanalytics::cocos2d::GameAnalytics:: setEnabledManualSessionHandling(true);
// JS
ga.GameAnalytics.setEnabledManualSessionHandling(true);
-- LUA
ga. setEnabledManualSessionHandling(true)
You will then need to call endSession and startSession at the appropriate times.
With manual session handling it is recommended to also call endSession when the game activity event onStop is fired. This will ensure a correct session close when users click the home or on/off button.
Start Session
This will start a new session if:
- manual session handling is enabled
- SDK is initialized (initialize will start a session automatically)
If a current session is active then it will end the current session and start a new one.
- C++
- Javascript
- Lua
// C++
gameanalytics::cocos2d::GameAnalytics::startSession();
// JS
ga.GameAnalytics.startSession();
-- LUA
ga.startSession()
End Session
This will end a session if:
- manual session handling is enabled
- a session is active
- SDK is initialized (initialize will start a session automatically)
- C++
- Javascript
- Lua
// C++
gameanalytics::cocos2d::GameAnalytics::endSession();
// JS
ga.GameAnalytics.endSession();
-- LUA
ga.endSession()
Behind the scenes
This is what happens when the session is starting or ending.
Session start
- Generate new session.
- Add a session start event (a “user” event).
- Start the periodic activation of submitting queued events.
- Next event submit will fix potential missing session_end from earlier sessions
Session end
- Stop the periodic activation of submitting queued events.
- Add a session_end event.
- Submit queued events.
Event Queue
Whenever an event is added (and validated) it will be added to a local database queue.
Server Sync Interval
Every 8 seconds the SDK will start a task for submitting queued events since last submit. This processing is done in a separate low-priority thread that will have minimum impact on performance. The payload is gzipped and will therefore only consume a small amount of bandwidth.
Offline Event Handling
When a device is offline the events are still added to the queue. When the device is online it will submit.
Thread Handling
Almost every piece of this code is run using a dedicated low-priority serial thread queue to avoid UI lag or sudden performance spikes.
The queue will execute each task sequentially. If the SDK add several tasks to the queue then each will be executed in turn. A task could be adding an event or submitting all queued events.
Consider this example with 3 calls:
- C++
- Javascript
- Lua
// Configure build version
gameanalytics::cocos2d::GameAnalytics::configureBuild("alpha 0.1.0");
// Initialize
gameanalytics::cocos2d::GameAnalytics::initialize("12341234123412341234123412341234", "1234123412341234123412341234123412341234");
// Add Design event
gameanalytics::cocos2d::GameAnalytics::addDesignEvent("Some:Event");
// Configure build version
ga.GameAnalytics.configureBuild("alpha 0.1.0");
// Initialize
ga.GameAnalytics.initialize(
"12341234123412341234123412341234",
"1234123412341234123412341234123412341234"
);
// Add Design event
ga.GameAnalytics.addDesignEvent("Some:Event");
-- Configure build version
ga.GameAnalytics:configureBuild("alpha 0.1.0")
-- Initialize
ga.GameAnalytics:initialize("12341234123412341234123412341234", "1234123412341234123412341234123412341234")
-- Add Design event
ga.GameAnalytics:addDesignEvent("Some:Event")
The configureBuild
is required to be called before initialize is completely finished. The design event call is required after initialize is finished. The queuing will make sure that each task is completely finished before proceeding to the next one.