Export Google Fit Daily Steps, Weight and Distance to a Google Sheet

Updated on Friday, December 29, 2023

Google Fit Daily Step Export

Google Fit is a great way to keep track of your daily step count without needing to carry a Fitbit or other dedicated tracker. It's not easy to get that data out though, as far as I can tell the only way is Google Takeout which is not made for automation. Luckily there is an API and you can do almost anything with Google Sheets.

If you're looking to export your step count, weight and distance this post has everything you need, just follow the instructions below to get your spreadsheet up and running. This is also a good primer on using OAuth2 with Google Apps Script and should be a decent starting point for a more complex Google Fit integration. If you have any questions or feedback please leave a comment below.

To get started you need a Google Sheet, an apps script project attached to the sheet and a Google API Project that will provide access to the Fitness API. That might sound intimidating but it should only take a few minutes to get everything up and running.

In Google Drive create a new spreadsheet and call it whatever you like. Rename the first tab to 'Metrics'. Enter 'Date' in cell A1, 'Steps' in B1, 'Weight' in C1 and 'Distance' in D1. To grab history as well create another tab called 'History' with the same headers. Next select 'Script editor...' from the Tools menu which will open a new apps script project.

Give the apps script project a name and then select 'Libraries...' from the Resources menu. Next to 'Add a library' enter 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF and click Add. This will find the Google OAuth2 library. Choose the most recent version (24 at the time of writing) and click Save. Then select 'Project properties' from the File menu and make a note of the Script ID (a long series of letters and numbers).

Open the Google API Console. Create a new project and name it something like 'Google Fit Sheet'. From the Dashboard click Enabled APIs and services and find and select the Fitness API. Then go to Credentials and create an OAuth Client ID. You'll be asked to create a consent screen, the only field you need to enter is the product name (i.e. 'My Fit App'). Then choose Web Application as the application type. You need to set the name and the authorized redirect URL. The redirect URL is https://script.google.com/macros/d/{SCRIPTID}/usercallback replacing {SCRIPTID} with the actual Script ID you made a note of above. After adding this make a note of the Client ID and Client Secret.

Go back to the apps script project and paste the code below into the Code.gs window:

Right at the top of the code there are spaces to enter the Client ID and Client Secret from the API Console. Enter these and save the project.

Switch back to your Google Sheet and reload. After reloading there will be a Google Fit menu item. First select Authorize... You'll get a screen to authorize the script and then a sidebar with a link. Click the link to authorize the script to access your Google Fit data. You can then close the sidebar and select Get Metrics for Yesterday from the Google Fit menu. You should see a new row added to the spreadsheet with yesterday's date and fitness data.

The final step is to automate pulling in the data. Go back to the apps script project and select Current project's triggers from the Edit menu. Add a trigger to run getMetrics() as a time driven day timer - I recommend between 5 and 6am. You can also click notifications to add an email alert if anything goes wrong, like your Google Fit authorization expiring (in which case you just need to come back and authorize from the Google Fit menu again.

At this point you're all set. Every day the spreadsheet will automatically update with your step count from the day before. You can add charts, moving averages, export to other systems, pull in your weight or BMI, etc. I want to add a seven day moving average step count to this blog somewhere as a semi-public motivational tool... watch this space.

Note that weight will be blank in the spreadsheet for days with no weight data. Google Fit doesn't return the last known weight, only the known value for days where an update was recorded.

If you are looking to extend this sample to other data types then this API explorer page is very helpful for finding data types that the API documentation doesn't list.

A couple of times working on this script I got my authorization in a bad state and started getting a 400 error response from the API. If this happens run your Google Fit app, click the Profile icon at the bottom and then the Settings icon at the top right. Click Manage connected apps and then disconnect the script from Google Fit. Finally run the Reset Settings option from the menu in the sheet and then authorize again.

I updated this post on Jan 21, 2019 to extend the sample to handle weight and distance as well as steps. I also improved the history function to handle many days in one API call rather than a quick hack I added earlier that pulled a day at a time. I'd recommend using the code above rather than anything included in comments below (at least comments before this update).

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Export Google Fit Daily Steps, Weight and Distance to a Google Sheet #code #software #fit #appsscript #google #sheets #drive Detailed instructions for setting up an automatic daily export of your step count from Google Fit to a Google Sheet. Uses Google Sheets, Apps Script and the Google Fitness API. )

Winter Solstice 2017

Winter Solstice 2017

It's the start of Winter in the Northern Hemisphere. Rendered in Catfood Earth (Windows, Android).

(Previously, Previously, Previously, Previously)

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Winter Solstice 2017 #code #winter #solstice #earth #summer #southern #hibernal Winter Solstice 2017 rendered in Catfood Earth for Windows and Android. It's the start of Winter in the Northern Hemisphere. )

Autumnal Equinox 2017

Updated on Saturday, August 21, 2021

Autumnal (fall) equinox 2017 rendered in Catfood Earth.

Autumn/Fall starts now in the Northern Hemisphere. Springtime for the hemispherically challenged. Rendered in Catfood Earth (Windows, Android live wallpaper).

(Previously, Previously, Previously, Previously)

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Autumnal Equinox 2017 #code #earth #equinox #autumnal Autumnal (fall) equinox 2017. Autumn begins in the Northern Hemisphere, Spring in the Southern Hemisphere. Image rendered in Catfood Earth. )

Email Alerts for new Referers in Google Analytics using Apps Script

Updated on Monday, February 13, 2023

Referral Traffic in Google Analytics

It's useful to know when you have a new website referrer. Google Analytics is plagued with spam referral and you want to filter this out of reporting as quickly as possible to stop it from skewing your data. It's also helpful to be able to respond quickly to new referral traffic - maybe leave a comment or promote the new link on social media.

The script below will send you a daily email with links to any new referrers (this is GA3, there is a GA4 version later in this post).

Start a new apps script project in Google Drive and paste in the code. At the top enter the view ID that you want to monitor and the email address that should receive reports.

Choose Advanced Google Services from the Resources menu and switch on the Google Analytics API. Then click the Google API Console link and enable the Google Analytics API there as well.

Finally pick Current project's triggers from the Edit menu and trigger the main function daily at a convenient time.

This script saves known referrers in script properties. For a site with lots of traffic this may run out of space in which case you might need to switch this out and write known referrers to a sheet instead.

For Google Analytics 4 properties use the version of the script below. The setup process is the same, but you need the Google Analytics Data API instead of the Google Analytics API.

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Email Alerts for new Referers in Google Analytics using Apps Script #code #googleanalytics #appsscript #gas #ga4 Apps script that will email you any new referral traffic from Google Analytics. Useful for responding to new links and referrer spam. GA3 and GA4 versions. )

Reading and Writing Office 365 Excel from a Console app using the Microsoft.Graph C# Client API

Updated on Sunday, September 18, 2022

Read and Write Excel from Microsoft Graph

I needed a console app that reads some inputs from an online Excel workbook, does some processing and then writes back the results to a different worksheet. Because I enjoy pain I decided to use the thinly documented new Microsoft.Graph client library. The sample code below assumes that you have a work or education Office 365 subscription.

Paste the code into a new console project and then follow the instructions at the top to add the necessary NuGet packages. You'll also need to register an application at https://portal.azure.com/. You want a Native application and you'll need the Application ID and the redirect URL (just make up some non-routable URL for this). Under Required Permissions for the app you should add read and write files delegated permissions for the Microsoft Graph API.

Hope this saves you a few hours. Comment below if you need a more detailed explanation for any of the above.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Reading and Writing Office 365 Excel from a Console app using the Microsoft.Graph C# Client API #code #microsoft #excel #graph #office365 How to read and write data to an Office 365 Excel workbook using the Microsoft.Graph client library from a C# console application. )

Summer Solstice 2017

Summer Solstice 2017

Summer starts now (or Winter for the Southern Hemisphere). Rendered in Catfood Earth.

(Previously, Previously, Previously, Previously)

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Summer Solstice 2017 #code #solstice #summer #winter #earth #northern #estival The exact moment of Summer Solstice 2017 as rendered in Catfood Earth )

CodePlex Shutdown: Shapefile, Orb, StackHash and Blogger2BlogML Migration

Updated on Wednesday, August 4, 2021

CodePlex Shutdown: Shapefile, Orb, StackHash and Blogger2BlogML Migration

Microsoft is finally pulling the plug on CodePlex. I've hosted various projects there over the years and have just finished migrating everything over to GitHub which looks like it just might have a future:

  • blogger2blogml - Migrate blogger format blogs to BlogML.
  • orb - Controller library for Ambient Orb devices.
  • shapefile - parse the ESRI Shapefile format.
  • stackhash - full client for downloading and debugging Windows Error Reporting (WER) crash dumps.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: CodePlex Shutdown: Shapefile, Orb, StackHash and Blogger2BlogML Migration #code #stackhash #codeplex #github CodePlex is gone. Shapefile, Orb, StackHash and Blogger2BlogML have been migrated to GitHib. )

Vernal Equinox 2017

Updated on Saturday, October 1, 2022

Vernal Equinox 2017

Vernal Equinox 2017 happens right now, as rendered in Catfood Earth.

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Vernal Equinox 2017 #code #earth #equinox #spring #autumn #vernal The exact moment of Vernal Equinox 2017 as rendered in Catfood Earth. )

Catfood Weather 2.30

Updated on Wednesday, February 22, 2017

Catfood Weather 2.30

Catfood Weather is a free taskbar based weather forecast app for Windows that includes a seven day forecast and severe weather alerts. It uses National Weather Service data and works for any US location (by ZIP code or latitude and longitude).

This update fixes a problem calling the National Weather Service API, so please install 2.30 if you're using a previous version of Catfood Weather. To get alerts for new releases follow the RSS Feed or sign up to the ITHCWY Newsletter.

Download Catfood Weather 2.30.

(Previously, Previously)

Add your comment...

Related Posts

You Might Also Like

(All Code Posts)

(Published to the Fediverse as: Catfood Weather 2.30 #code #weather Free taskbar based weather forecasts for Windows. Hover for current conditions, click for a seven day outlook and you get automatic severe weather alerts. US locations by ZIP or latitude/longitude only. )

Get an email if your site stops being mobile friendly (no longer available)

Updated on Tuesday, December 5, 2023

Google axed this tool today, so the script won't work any more. If you're looking for a replacement check out my Core Web Vitals script.

Get an email if your site stops being mobile friendly

Google just released an API for the mobile friendly test and so I've whipped up a script to send an alert if a web page violates their guidelines. This will run the test as often as you like and send you an email if it detects a problem. Alternatively if you're not mobile friendly it will keep emailing you until you fix any problems which might be a good motivational tool.

First start a new apps script project in drive and paste in the code below:

There are three variables you need to set, urlToMonitor is the full URL of the page to test, alertEmail is your email address (or whoever needs to be pestered) and runTestKey is the API key for the service. To get this go to the Google API Console, click Enable API, search for 'Google Search Console URL Testing Tools API' and click enable. Then click the Credentials option and generate a browser key.

Once you've configured the script choose 'Current project's triggers' from the Resources menu in apps script and set up a schedule for the mobileFriendlyMonitor() function.

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Get an email if your site stops being mobile friendly (no longer available) #code #mobile #appsscript #gas Use Google Apps Script and the Mobile Friendly Test API to constantly monitor your site for any violations. )