This guide shows you how to use the Game Stats APIs in an Android application
to unlock and display Game Stats in your game. The APIs can be found
in the com.google.android.gms.games
and com.google.android.gms.games.playergameevent
packages.
Before you begin
If you haven't already done so, you might find it helpful to review the Game Stats game concepts.
Before you start to code using the Game Stats API:
Follow the instructions for installing and setting up your app to use Google Play Games Services in the Set Up Google Play services SDK guide.
Define the Game Stats that you want your game to unlock or display, by following the instructions in the Google Play Console guide.
Get a Game Stats client
To start using the Game Stats API, your game must first obtain an
GameStatsClient
object. You can do this by calling the
Games.getGameStatsClient()
method and passing in the activity.
Client side integration
The client side integration for PlayerGameEvent would involve you
building the event object in Java SDK, at the trigger points and sending them
using the upload functions provided by the SDK.
Similarly, for the progressUpdate event, record updated progress using the
currentProgress property with additional defined properties and send them
using upload functions provided by the SDK.
Events
The client side integration for PlayerGameEvent are:
Build Player Game Event
Java
// Build the PlayerGameEvent
PlayerGameEvent event = new PlayerGameEvent.Builder("matchCompleted")
.addProperty("matchId", "Match_A")
.addProperty("gameMode", "Battle_B")
.addProperty("mapId", "Location_XYZ")
.addProperty("isWinner", true)
.addProperty("playerElimination", 2)
.build();
List events = new ArrayList<>();
// ... populate list ...
Build progressUpdate Player Game Event
Java
// Build the PlayerGameEvent
PlayerGameEvent event = new PlayerGameEvent.Builder("progressUpdate")
.addProperty("currentProgress", 52)
// Add other properties if needed
.build();
List events = new ArrayList<>();
// ... populate list ...
Record Event And Upload
Java
// Buffers the event locally.
client.recordEvent(event);
// Buffers the events locally, (List version)
client.recordEvents(events);
// This submits the events to PGS
client.requestEventsUpload();