We're making some changes to the Google Fit APIs. Learn about how these changes might affect your app. Read our new policy.

Work with Fitness Goals

Goals let Google Fit users track their progress against daily targets they set for themselves. The GoalsClient lets your app read these goals and check a user's progress against their current goal. You can use this data to create a more personalized experience and motivate your users.

Reading a goal

This example shows how to create a new Fitness client and get the user's Heart Points goal (or null, if they don't have a goal set).

val fitnessOptions = FitnessOptions.builder()
    .addDataType(DataType.TYPE_HEART_POINTS, FitnessOptions.ACCESS_READ)
    .build()

val goalsReadRequest = GoalsReadRequest.Builder()
    .addDataType(DataType.TYPE_HEART_POINTS)
    .build()

Fitness.getGoalsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .readCurrentGoals(goalsReadRequest)
    .addOnSuccessListener { goals ->
        // There should be at most one heart points goal currently.
        goals.firstOrNull()?.apply {
            // To what does this goal apply?
            val activity = activityName ?: "All activities"
            Log.i(TAG, "Activity name: $activity")

            // How is the goal measured?
            val objective = when (objectiveType) {
                OBJECTIVE_TYPE_DURATION ->
                    "Duration (s): ${durationObjective.getDuration(TimeUnit.SECONDS)}"
                OBJECTIVE_TYPE_FREQUENCY ->
                    "Frequency : ${frequencyObjective.frequency}"
                OBJECTIVE_TYPE_METRIC ->
                    "Metric : ${metricObjective.dataTypeName} - ${metricObjective.value}"
                else -> "Unknown objective"
            }
            Log.i(TAG, "Objective: $objective")

            // How often does the goal repeat?
            val repeatDetails = if (recurrence != null) {
                val period = when (recurrence.unit) {
                    Recurrence.UNIT_DAY -> "days"
                    Recurrence.UNIT_WEEK -> "weeks"
                    Recurrence.UNIT_MONTH -> "months"
                    else -> "Unknown"
                }
                "Every ${recurrence!!.count} $period"
            } else {
                "Does not repeat"
            }
            Log.i(TAG, "Recurrence: $repeatDetails")
        }
    }

Checking progress

Once you have the user's Heart Points goal, you can use the HistoryClient to check their progress. This example shows how to check how many Heart Points the user has.

val current = Calendar.getInstance()
val request = DataReadRequest.Builder()
    .read(DataType.TYPE_HEART_POINTS)
    .setTimeRange(
        goal.getStartTime(current, TimeUnit.NANOSECONDS),
        goal.getEndTime(current, TimeUnit.NANOSECONDS),
        TimeUnit.NANOSECONDS
    )
    .build()

Fitness.getHistoryClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .readData(request)
    .addOnSuccessListener { response ->
        val heartPointsSet = response.dataSets.first()
        val totalHeartPoints = heartPointsSet.dataPoints.sumBy {
            it.getValue(Field.FIELD_INTENSITY).asFloat().toInt()
        }
        Log.i(TAG, "Total heart points: $totalHeartPoints")
    }

Calculating progress as a percentage

By dividing the total from the checking progress example by the target in the reading a goal example, it is possible to calculate the progress towards the goal as a percentage.