Skip to main content

Getting Started with GameAnalytics Android SDK

This guide explains how to integrate the GameAnalytics SDK into your Android project.

Before You Start​

Pre-requisites​

  • Android Studio
  • Google Play Services installed via the Android SDK manager
  • compileSdkVersion set to 33 or higher in your app module's build.gradle

Supported Platforms​

  • Android API level 16 and above

Install the SDK​

Using Maven​

Add the GameAnalytics Maven repository to your project-level build.gradle:

maven { url 'https://maven.gameanalytics.com/release'; }

Then add the dependency to your app-level build.gradle:

// use latest version instead version number in this example
implementation 'com.gameanalytics.sdk:gameanalytics-android:+'

Manual Installation​

Download the SDK from here.

Using .jar: Unzip and place gameanalytics.jar inside NAME_OF_MODULE/libs/.

Using .aar: Import the gameanalytics.aar via File → New → New Module… → Import .JAR/.AAR Package.


Integrate the SDK​

Add the SDK to Your Project​

Import the GameAnalytics package wherever you need to call the SDK:

import com.gameanalytics.sdk.*;

Using .jar​

android {
compileSdkVersion 33
}

dependencies {
compile 'com.google.android.gms:play-services-base:10.2.1'
compile 'com.android.support:appcompat-v7:24.2.0'
}

Using .aar​

android {
compileSdkVersion 33
}

dependencies {
compile project(':gameanalytics')
compile 'com.android.support:appcompat-v7:24.2.0'
}

Proguard​

If your app uses Proguard, add these rules to your Proguard configuration:

-keep class com.gameanalytics.sdk { *; }
-keep class com.gameanalytics.sdk.** { *; }

-keep class com.gameanalytics.sdk.GAPlatform { *; }
-keep class com.gameanalytics.sdk.GAPlatform.** { *; }
-keep class android.net.ConnectivityManager.** { *; }
-keep class com.google.android.instantapps.InstantApps { *; }
-keepclassmembers class com.google.android.instantapps.InstantApps { *; }

AndroidManifest.xml​

To avoid sessions restarting on orientation changes, add the following to your activity entry in AndroidManifest.xml:

<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Using C++​

If you want to use the native Android SDK from C++ code, call the following before any SDK functions:

GameAnalyticsJNI.nativeSetContext(activity); // from Java code

// or

gameanalytics::jni_setContext(activity); // from C++ code

Also place this inside your JNI_OnLoad function:

gameanalytics::jni_setJavaVM(vm);
caution

You must link against log and android libraries when building.

Configure the SDK​

caution

Configuration and initialization must be called inside the onCreate method of your main Activity class.

Here is a quick configuration example:

import com.gameanalytics.sdk.GameAnalytics;
import com.gameanalytics.sdk.GAPlatform;
import com.gameanalytics.sdk.StringVector;

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// Enable log
GameAnalytics.setEnabledInfoLog(true);
GameAnalytics.setEnabledVerboseLog(true);

// Configure available virtual currencies and item types
GameAnalytics.configureAvailableResourceCurrencies("gems", "gold");
GameAnalytics.configureAvailableResourceItemTypes("boost", "lives");

// Configure available custom dimensions
GameAnalytics.configureAvailableCustomDimensions01("ninja", "samurai");
GameAnalytics.configureAvailableCustomDimensions02("whale", "dolphin");
GameAnalytics.configureAvailableCustomDimensions03("horde", "alliance");

// Configure build version
GameAnalytics.configureBuild("0.1.0");

// Initialize
GameAnalytics.initializeWithGameKey(this, "[game key]", "[secret key]");
}
}

See the Configuration page for the full list of configuration options.

Initialize the SDK​

// Initialize
GameAnalytics.initializeWithGameKey(activity, "[game key]", "[secret key]");

Without Activity​

// Initialize
GameAnalytics.initializeWithGameKey("[game key]", "[secret key]");

When using this approach, call the following inside your Activity's onCreate before any configuration functions:

GAPlatform.initializeWithActivity(this);
info

Don't have any keys yet? Head over here and register your game at the GameAnalytics website!


Sending Events​

Once initialized, you can add events from anywhere in your game code:

// Design event
GameAnalytics.addDesignEventWithEventId("Some:Event");

// Resource event
GameAnalytics.addResourceEventWithFlowType(GAResourceFlowType.Source, "Gems", 400, "IAP", "Coins400");

// Progression event
GameAnalytics.addProgressionEventWithProgressionStatus(GAProgressionStatus.Start, "world01", "stage01", "level01");

See the Event Tracking page for the full list of event types.