Track App Usage Data with Amazon Mobile Analytics

Amazon Mobile Analytics allows you to measure app usage and app revenue. By tracking key trends such as new vs. returning users, app revenue, user retention, and custom in-app behavior events, you can make data-driven decisions to increase engagement and monetization for your app.

The tutorial below explains how to integrate Mobile Analytics with your app.

Project Setup#


You must complete all of the instructions on the Set Up the AWS Mobile SDK for Android page before beginning this tutorial.

Create an App in the Mobile Analytics Console#

Go to the Mobile Analytics Console and create an app. Note the appId value, as you'll need it later.


To learn more about working in the console, see the Mobile Analytics User Guide.

Set Permissions in Your Android Manifest#

In AndroidManifest.xml, set the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Initialize MobileAnalyticsManager#

Define a static reference to the MobileAnalyticsManager in the onCreate() method of your main activity:

private static MobileAnalyticsManager analytics;

For this particular example, let's also create two constants that we'll use later in a custom event:

private static final int STATE_LOSE = 0;
private static final int STATE_WIN = 1;

In the activity’s onCreate() method, create an instance of MobileAnalyticsManager. You’ll need to replace "cognitoId" and "appId" to their respective values as shown from the Mobile Analytics console. The appId is used to group your data in the console.

protected void onCreate(Bundle savedInstanceState) {

    try {
        analytics = MobileAnalyticsManager.getOrCreateInstance(
    } catch(InitializationException ex) {
            Log.e(this.getClass().getName(), "Failed to initialize Amazon Mobile Analytics", ex);

By default, the MobileAnalyticsManager client initializes with WAN delivery enabled.

Track Session Events#

Override the activity’s onPause() and onResume() methods to record session events:

 * Invoked when the Activity loses user focus.
protected void onPause() {
    if(analytics != null) {
        //Attempt to send any events that have been recorded to the Mobile Analytics service

protected void onResume() {
    if(analytics != null)  {

For each activity in your application, you will need to record session events in the onPause() and onResume() methods.

Add Monetization Events#

The AWS Mobile SDK for Android provides a MonetizationEventBuilder that lets you create events for Amazon purchases, Google Play purchases, and virtual store purchases. The MonetizationEventBuilder class can be extended if you need to record monetization events from other purchase frameworks.

To learn more about adding monetization events, see the API reference guide for MonetizationEventBuilder.

Record Custom Events#

The Mobile Analytics client lets you create and record custom events. For example, if our app were a game, we might create a custom event to be submitted when the user completes a level. In your main activity, add the following method, which creates and records a custom event.

* This method gets called when the player completes a level
* @param levelName the name of the level
* @param difficulty the difficulty setting
* @param timeToComplete the time to complete the level in seconds
* @param playerState the winning/losing state of the player
public void onLevelComplete(String levelName, String difficulty, double timeToComplete, int playerState) {

    //Create a Level Complete event with some attributes and metrics(measurements)
    //Attributes and metrics can be added using with statements
    AnalyticsEvent levelCompleteEvent = analytics.getEventClient().createEvent("LevelComplete")
        .withAttribute("LevelName", levelName)
        .withAttribute("Difficulty", difficulty)
        .withMetric("TimeToComplete", timeToComplete);

    //attributes and metrics can also be added using add statements
    if (playerState == STATE_LOSE)
        levelCompleteEvent.addAttribute("EndState", "Lose");
    else if (playerState == STATE_WIN)
        levelCompleteEvent.addAttribute("EndState", "Win");

    //Record the Level Complete event