How can you check your users' unlocked achievements (Google Play Game Services)?

| | August 8, 2015

Is it possible to see how many of your users have unlocked certain achievements on the Google Play Developer console? This information would be very useful. I guess that one could achieve this through integration with Google Analytics but I was wondering if there was a simpler way to get this data.

2 Responses to “How can you check your users' unlocked achievements (Google Play Game Services)?”

  1. Callback approach

    Apart from some small errors in the accepted answer I prefer the callback approach to get the results so we don’t have to perform all of this in the background.

    To simplify comparison I used the same GameHelper. This GameHelper object is a singleton containing a reference to the GoogleApiClient object.

    Updated example code:

        GameHelper mHelper;        // GameHelper should be accessible for this to work
        boolean fullLoad = false;  // set to 'true' to reload all achievements (ignoring cache)
    
        // load achievements
        Games.Achievements.load( mHelper.getGoogleApiClient(), fullLoad ).setResultCallback(new ResultCallback<Achievements.LoadAchievementsResult>() {
            @Override
            public void onResult(Achievements.LoadAchievementsResult loadAchievementsResult) {
                for( Achievement achievement : loadAchievementsResult.getAchievements() ){
    
                    // here you can work with the achievement objects
                    // ...
                    boolean unlocked = (achievement.getState() == Achievement.STATE_UNLOCKED);
                }
            }
        });
    
  2. The Google Play Games service actually provides an API for doing just that. I have provided an example for querying the state of all achievements in this answer on SO – but I will provide that information here as well.

    The following method will retrieve and iterate all achievements, and should be run asynchronously (using AsyncTask or in a separate thread):

    public void loadAchievements()  {
       GameHelper mHelper;        // GameHelper should be accessible for this to work
    
       boolean fullLoad = false;  // set to 'true' to reload all achievements (ignoring cache)
       float waitTime = 60.0f;    // seconds to wait for achievements to load before timing out
    
       // load achievements
       PendingResult p = Games.Achievements.load( mHelper.getApiClient(), fullLoad );
       Achievements.LoadAchievementsResult r = (Achievements.LoadAchievementsResult)p.await( waitTime, TimeUnit.SECONDS );
       int status = r.getStatus().getStatusCode();
       if ( status != GamesStatusCodes.STATUS_OK )  {
          r.release();
          return;           // Error Occurred
       }
    
       // process the loaded achievements
       AchievementBuffer buf = r.getAchievements();
       int bufSize = buf.getCount();
       for ( int i = 0; i < bufSize; i++ )  {
          Achievement ach = buf.get( i );
    
          // here you now have access to the achievement's data
          String id = ach.getAchievementId();  // the achievement ID string
          boolean unlocked = ach.getState == Achievement.STATE_UNLOCKED;  // is unlocked
          boolean incremental = ach.getType() == Achievement.TYPE_INCREMENTAL;  // is incremental
          if ( incremental )
             int steps = ach.getCurrentSteps();  // current incremental steps
       }
       buf.close();
       r.release();
    }
    

    This assumes you are using BaseGameUtils (and mHelper is already connected). The API documentation should provide any further information you need. You can also do this with callbacks if you don’t want to manually run it asynchronously.

    You should of course change the internals of the loop which are processing the results to do something more useful, since the example only illustrates how to access the information. For your question you may only need to check for unlocked achievements and count them.

Leave a Reply