Skip to main content

Configuration

The Defold SDK is configured in the [gameanalytics] section of game.project. Settings that must be known before the SDK starts (build version, allowed custom dimensions, resource currencies and item types) only live there. Everything else can also be changed from Lua at any time.

Set the build version​

The build version separates your builds in the dashboard. Use a 3 digit version in the format [major].[minor].[patch]. There is one entry per platform:

[gameanalytics]
build_ios = 1.0.0
build_android = 1.0.0
build_html5 = 1.0.0
build_osx = 1.0.0
build_windows = 1.0.0
build_linux = 1.0.0

On iOS and Android you can send the app's own version instead:

[gameanalytics]
auto_detect_app_version = 1

User ID​

The SDK generates a random user id on first launch and keeps it across sessions. This is the right choice for almost every game, and no configuration is needed.

local id = gameanalytics.getUserId()

Set a custom user ID​

If your game already has its own identifier, you can use it as the GameAnalytics user id. It impacts every metric, so use it from the start of the game's lifetime: introducing it in a game that is already live counts existing players as new users.

Enable it in game.project. The SDK then waits for the id instead of starting automatically:

[gameanalytics]
use_custom_id = 1

Then set the id from your script, which also starts the SDK. No events can be sent before this call:

gameanalytics.configureUserId("my_custom_id")
caution

The custom user id cannot be changed after the SDK has started.

Set an optional External User ID​

An external user id is appended to every event so you can cross-reference GameAnalytics data with your own systems. It has no influence on any metric and can be changed at any time, before or after initialization:

gameanalytics.setExternalUserId("1234")

local externalId = gameanalytics.getExternalUserId()

getExternalUserId returns an empty string when none was set.

Configure Privacy​

To stop sending events, for example until the player has given consent, disable event submission. The SDK still receives remote configs while submission is off:

gameanalytics.setEnabledEventSubmission(false)

Event submission is enabled by default. Pass true to resume. It can also be set from game.project:

[gameanalytics]
enable_event_submission = 0
info

On web builds setEnabledEventSubmission has no effect.

Resources​

Resource events track your in-game economy. Before you can send one, the currencies and item types they use must be registered in game.project. A resource event using a value outside these lists is rejected by the SDK.

Currencies​

Register up to 20 currencies as a comma separated list, with no space between entries. Each currency may only contain letters ([A-Za-z]):

[gameanalytics]
resource_currencies = gold,gems

Item Types​

Register up to 20 item types the same way. Each item type may only contain alphanumeric characters:

[gameanalytics]
resource_item_types = boost,lives

With both in place you can send resource events:

gameanalytics.addResourceEvent({
flowType = "Source",
currency = "gold",
amount = 100,
itemType = "boost",
itemId = "daily_reward",
})

Set custom dimensions​

GameAnalytics supports up to 3 custom dimensions: Custom01, Custom02 and Custom03. For each one you must declare the values it can take, up to 20 per dimension, comma separated with no spaces:

[gameanalytics]
custom_dimensions_01 = ninja,samurai
custom_dimensions_02 = whale,dolphin
custom_dimensions_03 = horde,alliance

During the game you set the active value of each dimension. Once set it persists across sessions and is attached to every event category automatically. Pass an empty string to reset a dimension:

gameanalytics.setCustomDimension01("ninja")
gameanalytics.setCustomDimension02("dolphin")
gameanalytics.setCustomDimension03("horde")

-- reset custom dimension 3
gameanalytics.setCustomDimension03("")
tip

To attach a dimension to the first session start event, set it before the SDK starts: enable manual_initialize = 1, set the dimension, then call gameanalytics.initialize().

tip

For more information about custom dimensions go here.

Custom Event Fields​

Custom event fields are a set of up to 50 key-value pairs you can attach to any event. Values can be strings or numbers. They are not shown in the GameAnalytics dashboards, they are only available through Datasuite (Data Warehouse or Data Export).

Fields that should go with every event are set once as a JSON string and can be changed at any time:

gameanalytics.setGlobalCustomEventFields(json.encode({ test = 1000, tests = "hello_world" }))

Every event function also takes a customFields option for fields attached to that event only. By default they replace the global fields; pass mergeFields = true to merge the two instead:

gameanalytics.addDesignEvent({
eventId = "shop:opened",
customFields = json.encode({ source = "main_menu" }),
mergeFields = true,
})
tip

Find more information on Custom Event Fields and Data Export following the links.

Session handling​

A session is the focused time a player spends in your game, from launch to leaving it. By default the SDK handles sessions automatically: a session starts when the SDK starts (or when the app returns to the foreground without a running session) and ends when the app goes to the background. On desktop platforms the extension ends the session and flushes the remaining events when the game quits.

You can read the session times at any point:

local current = gameanalytics.getElapsedSessionTime() -- seconds
local previous = gameanalytics.getElapsedTimeForPreviousSession() -- seconds
local total = gameanalytics.getElapsedTimeFromAllSessions() -- seconds
info

On web builds the session times return 0. On Android getElapsedTimeForPreviousSession returns 0.

Manual session handling​

Automatic session handling is recommended and sufficient for nearly every game. Only take it over when there is no other option: inaccurate session handling results in skewed metrics.

Enable it in game.project so it applies from the first session:

[gameanalytics]
use_manual_session_handling = 1

It can also be switched at runtime:

gameanalytics.setEnabledManualSessionHandling(true)

Your game is then responsible for starting and ending sessions:

-- starts a new session: only when manual session handling is enabled and no session is running
gameanalytics.startSession()

-- ends the running session and flushes the queued events
gameanalytics.endSession()
info

With manual session handling it is recommended to also call endSession when the game is fully paused, so the session closes correctly when the player hits home or switches the device off.

Error submission​

The SDK can report crashes automatically as error events. This is off by default so it does not interfere with Defold's own error reporting. Enable it in game.project:

[gameanalytics]
enable_error_reporting = 1

A maximum of 10 error events are sent automatically per launch, to avoid degraded performance on games that throw a lot of exceptions.

info

On Android automatic error reporting needs the androidx.core library, which games usually get from another extension.

For errors the game catches itself, send an error event:

gameanalytics.addErrorEvent({
severity = "Warning",
message = "Could not load the level data",
})