Events in Android Games

This guide shows you how to collect player gameplay data for game analytics using the events APIs provided by Google Play games services. The APIs can be found in the com.google.android.gms.games.event and com.google.android.gms.games.

Before you begin

If you haven't already done so, you might find it helpful to review the events game concepts.

Before you start to code using the events APIs:

Getting the events client

To start using the events APIs, your game must first obtain an EventsClient object. You can do this by calling the Games.getEventsClient() method and passing in the activity.

Submitting events

You can add code in your game to notify Google Play games services whenever an event of interest to your game occurs.

To send an event update, call EventsClient.increment() with the eventId value and an integer incrementAmount that is equal to or greater than 0.

  • The eventId is generated by Google Play games services when you first define the event in the Google Play Console and is used to uniquely identify this event in your game.
  • You can use the incrementAmount input to specify the player's quantitative progress towards completing some game-specific goal. For example, if the event your game wants to track is 'Defeat 500 bug-eyed monsters', the incrementAmount value can be the number of monsters that the player killed in a single battle.

Here's an example of how to submit an event with an increment amount of 1:

public void submitEvent(String eventId) {
  PlayGames.getEventsClient(this)
      .increment(eventId, 1);
}

Retrieving events

You can retrieve all events data stored in Google's servers for your game, by calling EventsClient.load(). In the method call, pass in a boolean value to indicate if Google Play games services should clear the locally cached data on the user's device.

To retrieve data for specific events that you defined in the Google Play Console, call EventsClient.loadByIds() and pass in an array of event IDs in the input parameters.

The following snippet shows how you can query Google Play games services for the list of all events for your game:

public void loadEvents() {
  PlayGames.getEventsClient(this)
      .load(true)
      .addOnCompleteListener(new OnCompleteListener<AnnotatedData<EventBuffer>>() {
        @Override
        public void onComplete(@NonNull Task<AnnotatedData<EventBuffer>> task) {
          if (task.isSuccessful()) {
            // Process all the events.
            for (Event event : task.getResult().get()) {
              Log.d(TAG, "loaded event " + event.getName());
            }
          } else {
            // Handle Error
            Exception exception = task.getException();
            int statusCode = CommonStatusCodes.DEVELOPER_ERROR;
            if (exception instanceof ApiException) {
              ApiException apiException = (ApiException) exception;
              statusCode = apiException.getStatusCode();
            }
            showError(statusCode);
          }
        }
      });
}