Game Stats

This document describes how to use Google Play Games Services Game Stats in C++ games. This document assumes you have set up your project as described in Set Up Google Play Games Services. You can find the Game Stats API in the PgsGameStatsClient.

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:

Get Game Stats client

To start using the achievements API, your game must first obtain an PgsGameStatsClient object. You can do this by calling the PgsPlayerGameEvent_create method and passing in the activity.

Client side integration

The client side integration for PlayerGameEvent would involve you building the event object in C++ 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

C++


// Build the PlayerGameEvent
PgsPlayerGameEvent* player_event = PgsPlayerGameEvent_create("matchCompleted");
if (player_event != nullptr) {
   PgsPlayerGameEvent_putString(player_event, "matchId", "Match_A");
   PgsPlayerGameEvent_putString(player_event, "gameMode", "Battle_B");
   PgsPlayerGameEvent_putString(player_event, "mapId", "Location_XYZ");
   PgsPlayerGameEvent_putBoolean(player_event, "isWinner", true);
   PgsPlayerGameEvent_putLong(player_event, "playerElimination", 2);
}
Build progressUpdate Player Game Event

C++


PgsPlayerGameEvent* progress_event = PgsPlayerGameEvent_create("progressUpdate");
if (progress_event != nullptr) {
   PgsPlayerGameEvent_putLong(progress_event, "currentProgress", 52);
}
Record Event And Upload

C++


// Record the event
PgsGameStatsClient_recordEvent(client, player_event);

// This submits the events to PGS
PgsGameStatsClient_requestEventsUpload(client);

// Clean up handle to avoid leaks
PgsPlayerGameEvent_destroy(player_event);

// Clean up client handle when PGS operations are finished
PgsGameStatsClient_destroy(client);