目录

1. Scenario

The events feature allows you to collect specific data generated by your players during gameplay and store them on the Huawei game server for AppGallery Connect to use as feedback to conduct game analytics.

You can flexibly define what event data your game should submit, for example, players have paid a specific amount, players reach a certain level, and players perform a specific game action.

You can improve your game based on submitted events. For example, if the number of users who purchase a game prop is far lower than expected, the developer can adjust the game props expense and interest.

An event can contain the following basic attributes.

Attribute

Description

ID

A unique string generated by AppGallery Connect to identify an event.

Name

Name of an event. You can define it when configuring an event.

Description

Description of an event.

Type

Type of an event. Three event types are supported now:

  • Currency sink: Select this type for events that track consumption of a currency, for example, "gold coins spent".

  • Currency output: Select this type for events that track sources of a currency, for example, "gold coins earned".

  • None: Select this type for events not related to the currency.

Value

Value of an event, which is set when events are submitted instead of when events are defined.

For the event "gold coins spent", if the value is 500, it means that the player spent 500 gold coins.

You can define the data type for an event value as number or time when defining an event. For a value of the number type, you can also define the number of decimal places and unit. AppGallery Connect automatically adds a unit for the value of a submitted event based on the data type setting.

Icon

Icon associated with an event. The icon must be of the resolution 512 x 512 px, and in PNG or JPG format. Avoid using any texts that need to be localized in the icon.

Initial state

Initial state of an event. You can set the attribute when defining an event to determine whether to display or hide the event.

List order

Order of an event among all events. You need to specify the attribute when configuring an event.

2. Before You Start

Note:
You are advised to provide the function for users who have enabled Game Services to disable it. CallGamesClient.cancelGameService to implement the function.

3. Initializing the EventsClient Instance

Before using any events API capabilities, call Games.getEventsClient() to obtain the EventsClient instance. Sample code is as follows:

  1. EventsClient client = Games.getEventsClient(this, mAuthHuaweiId);

The value of mAuthHuaweiId passed in the API is the AuthHuaweiId object obtained when a user signs in to the game. If the sign-in session has expired, call the HuaweiIdAuthService.getSignInIntent method to sign the user in to the game again or call the HuaweiIdAuthService.silentSignIn method to implement silent sign-in and obtain AuthHuaweiId.

4. Submitting Events

When an event of interest to your game occurs, call the EventsClient.grow method with the eventId and growAmount parameters to submit event data to the Huawei game server.

The following is the sample code for submitting an event:

  1. client.increment(eventId, growAmount);

5. Obtaining Events

You can display events to users during gameplay.

For both methods, either to display all events or only specific events, you can pass in the forceReload parameter of the boolean type to indicate whether to obtain data stored on the Huawei game server or locally cached data.

The following sample code shows how to obtain all events:

  1. Task<List<Event>> task = client.getEventList(forceReload);

The following sample code shows how to obtain specified events:

  1. Task<List<Event>> task = client.getEventListByIds(forceReload, eventIds);

The following sample code shows how a list of specified events is returned:

  1. task.addOnSuccessListener(new OnSuccessListener<List<Event>>() {
  2.         @Override
  3.         public void onSuccess(List<Event> data) {
  4.            if (data == null) {
  5.                Log.w("Event", "event is null");
  6.                return;
  7.            }
  8.            for (Event event : data) {
  9.                Log.i("Event", "event id:" + event.getEventId());
  10.            }
  11.        }
  12.    }).addOnFailureListener(new OnFailureListener() {
  13.        @Override
  14.        public void onFailure(Exception e) {
  15.            if (e instanceof ApiException) {
  16.                String result = "rtnCode:"
  17. + ((ApiException) e).getStatusCode();
  18.                Log.e("Event", result);
  19.            }
  20.        }
  21.    });