Translate

Export your Fitbit Data in a Google Spreadsheet


Export Your Fitbit Data into a Google Spreadsheet: A Step-by-Step Guide

Fitbit is one of the most popular health and fitness tracking platforms, offering detailed insights into your daily activities, heart rate, sleep patterns, and much more. However, sometimes the data displayed in the Fitbit dashboard is not sufficient for in-depth analysis or integration with other tools. That’s where exporting your Fitbit data to a Google Spreadsheet comes in handy.

In this guide, you'll learn how to export your Fitbit data directly into a Google Sheet using Fitbit’s API and Google Apps Script. This method is ideal for those who want to customize their fitness reports, create visual dashboards, or analyze long-term trends in a spreadsheet format.


Why Export Fitbit Data to Google Sheets?

There are several advantages to exporting your Fitbit data to Google Sheets:

  • Custom Analysis: Perform calculations, create pivot tables, or apply conditional formatting for personal insights.

  • Data Backup: Store your health data independently from Fitbit’s ecosystem.

  • Integration: Combine Fitbit data with other sources like diet logs, calendar events, or productivity metrics.

  • Visualization: Create custom charts and dashboards using Google Sheets or Google Data Studio.


Step 1: Set Up a Fitbit Developer Application

Before accessing your data programmatically, you need to register an application with Fitbit.

  1. Go to the Fitbit Developer Portal.

  2. Log in using your Fitbit account.

  3. Click on "Create App".

  4. Fill out the required fields:

    • Application Name: Choose any name.

    • OAuth 2.0 Application Type: Choose Personal.

    • Redirect URI: Use https://script.google.com/macros/d/YOUR_SCRIPT_ID/usercallback (you'll update YOUR_SCRIPT_ID later).

    • OAuth 2.0 Scopes: Select all the data you want access to, such as activity, heartrate, sleep, and profile.

  5. Save the app details. You will receive a Client ID and Client Secret.


Step 2: Create a Google Apps Script Project

  1. Open Google Apps Script and create a new project.

  2. Rename the project to something like "Fitbit Data Export".

  3. Paste the following sample script:

var CLIENT_ID = 'YOUR_CLIENT_ID';
var CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
var REDIRECT_URI = ScriptApp.getService().getUrl();

function authorize() {
  var service = getService();
  if (service.hasAccess()) {
    Logger.log("Already authorized");
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log("Open the following URL and authorize the script: %s", authorizationUrl);
  }
}

function getService() {
  return OAuth2.createService('fitbit')
    .setAuthorizationBaseUrl('https://www.fitbit.com/oauth2/authorize')
    .setTokenUrl('https://api.fitbit.com/oauth2/token')
    .setClientId(CLIENT_ID)
    .setClientSecret(CLIENT_SECRET)
    .setCallbackFunction('authCallback')
    .setPropertyStore(PropertiesService.getUserProperties())
    .setScope('activity heartrate sleep profile')
    .setParam('expires_in', '31536000');
}

function authCallback(request) {
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput("Success! You can close this tab.");
  } else {
    return HtmlService.createHtmlOutput("Denied. You can close this tab");
  }
}

function getFitbitData() {
  var service = getService();
  if (!service.hasAccess()) {
    Logger.log("Authorization required.");
    return;
  }

  var today = new Date();
  var date = Utilities.formatDate(today, Session.getScriptTimeZone(), "yyyy-MM-dd");
  var url = 'https://api.fitbit.com/1/user/-/activities/date/' + date + '.json';
  var response = UrlFetchApp.fetch(url, {
    headers: {
      Authorization: 'Bearer ' + service.getAccessToken()
    }
  });

  var data = JSON.parse(response.getContentText());
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.clearContents();
  sheet.appendRow(["Date", "Steps", "Calories", "Distance", "Floors"]);

  sheet.appendRow([
    date,
    data.summary.steps,
    data.summary.caloriesOut,
    data.summary.distances.find(d => d.activity === 'total').distance,
    data.summary.floors
  ]);
}

Step 3: Enable OAuth2 Library

  1. Go to Extensions > Apps Script > Libraries.

  2. Search for OAuth2 by Google and select the one with the identifier: 1B7dB6Dha_VnC9vcoU-UU6zF2z1t3o2yZ5bK3Z3_dHLtT0W2iJ5kKp_9w.

  3. Add it to your project.


Step 4: Deploy as Web App

  1. In the Apps Script editor, click Deploy > Manage deployments.

  2. Click New deployment.

  3. Select Web app as the deployment type.

  4. Set who has access to: Anyone.

  5. Deploy the project and copy the URL. Replace YOUR_SCRIPT_ID in your Fitbit app with this full callback URL.


Step 5: Authorize and Run

  1. Run the authorize function in the script editor.

  2. Open the authorization URL in the Logs tab.

  3. Authorize the app and grant permissions.

  4. After authorization, run the getFitbitData function.


Step 6: View Your Data in Google Sheets

Once you run the script, your Google Sheet will automatically populate with daily activity data like steps, calories burned, distance covered, and floors climbed.

You can modify the script to retrieve:

  • Sleep data (/1.2/user/-/sleep/date/{date}.json)

  • Heart rate data (/1/user/-/activities/heart/date/{date}/1d.json)

  • Weekly or monthly summaries


Automate the Process (Optional)

To automate daily data import:

  1. Go to Triggers in Apps Script.

  2. Click on Add Trigger.

  3. Choose the getFitbitData function and set it to run time-driven, e.g., every day at 7 AM.


Conclusion

Exporting your Fitbit data into a Google Spreadsheet gives you powerful control over how you view and use your health information. Whether you're a data nerd or just want to track progress more clearly, this method lets you tailor your fitness tracking like never before. Plus, once it’s set up, the system can run automatically in the background—keeping your health data right where you want it: in your hands.