目录

1. Use Case

The player statistics APIs allow you to obtain various statistics about the current player from Huawei game server so that you can learn about players' playing habits, game progress, and payment capabilities to tailor game experiences for different players accordingly. For example, you can encourage a player who has not signed in for a long time to re-engage with your game by means such as offering an attractive reward.

You can collect the following statistics about players:

        1: no spending

        2: less than US$10

        3: US$10 or more and less than US$50

        4: US$50 or more and less than US$300

        5: US$300 or more and less than US$1000

        6: more than US$1000

2. Before You Start

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

3. Initializing the GamePlayerStatisticsClient Instance

Before using player statistics API capabilities, call Games.getGamePlayerStatsClient to obtain the GamePlayerStatisticsClient instance.

  1. GamePlayerStatisticsClient playerStatsClient = Games.getGamePlayerStatsClient(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. Obtaining Player Statistics

You can call the GamePlayerStatisticsClient.getGamePlayerStatistics method to obtain player statistics. You can make your game interact with players based on obtained player statistics.

  1. Task<GamePlayerStatistics> task = playerStatsClient.getGamePlayerStatistics(ISREALTIME);
  2. task.addOnSuccessListener(new OnSuccessListener<GamePlayerStatistics>() {
  3.    @Override
  4.    public void onSuccess(GamePlayerStatistics gamePlayerStatistics) {
  5.        if (gamePlayerStatistics == null) {
  6.            showLog("playerStatsAnnotatedData is null, inner error");
  7.            return;
  8.        }
  9.        StringBuilder sb = new StringBuilder();
  10.        sb.append("IsRealTime:" + ISREALTIME);
  11.        sb.append("\n---AverageSessionLength: " + gamePlayerStatistics.getAverageOnLineMinutes() + "\n---");
  12.        sb.append("DaysSinceLastPlayed: " + gamePlayerStatistics.getDaysFromLastGame() + "\n---");
  13.        sb.append("NumberOfPurchases: " + gamePlayerStatistics.getPaymentTimes() + "\n---");
  14.        sb.append("NumberOfSessions: " + gamePlayerStatistics.getOnlineTimes() + "\n---");
  15.        sb.append("TotalPurchasesAmountRange: " + gamePlayerStatistics.getTotalPurchasesAmountRange());
  16.        showLog(sb.toString());
  17.    }
  18. }).addOnFailureListener(new OnFailureListener() {
  19.    @Override
  20.    public void onFailure(Exception e) {
  21.        if (e instanceof ApiException) {
  22.            String result = "rtnCode:" + ((ApiException) e).getStatusCode();
  23.            showLog(result);
  24.        }
  25.    }
  26. });

You can make your game interact with players based on obtained player statistics.