DEV Community

Cover image for Building a Personalized Workout Tracker with Google Apps Script
Damilare Abogunrin
Damilare Abogunrin

Posted on

Building a Personalized Workout Tracker with Google Apps Script

In this article, I’ll walk you through how I built a Personalized Workout Tracker using Google Apps Script. This tool allows users to log their workout activities, store the data in Google Sheets, and receive weekly progress reports via email. The inspiration came from my desire to automate fitness tracking while keeping the process simple and accessible for anyone with a Google account.

If you’ve read my previous article on building an automated workflow with Airtable, Zapier, and ChatGPT, you’ll notice a similar step-by-step approach here. Let’s dive into it!

Step 1: Setting the Foundation with Google Forms

The first step was to create a Google Form that users can fill out to log their workouts. Here's what I included in the form:

  • Name: A short text field to identify the user.
  • Email Address: Essential for sending weekly progress reports.
  • Date of Workout: To track when the workout was performed.
  • Type of Workout: A dropdown menu with options like Cardio, Strength, Yoga, etc.
  • Duration: A number field to record the time spent on the workout (in minutes).

Image description

Once the form was ready, I linked it to a Google Sheet where all responses would be stored. You can do this by clicking the "Responses" tab in the form and selecting the green Sheets icon.

Image description

Step 2: Exploring the Linked Google Sheet

The linked Google Sheet served as the backend for my workout tracker. Each submission from the form was automatically logged as a new row. I renamed the sheet to Workout Data for clarity.

The columns in the sheet looked like this:

  • Timestamp
  • Name
  • Email Address
  • Date of Workout
  • Type of Workout
  • Duration (Minutes)

Add a screenshot of the Google Sheet with sample data here.

Step 3: Writing the Google Apps Script

Here’s where the magic happens. I wrote a script to process the workout data, summarize it, and email a weekly progress report to each user.

To access the Apps Script editor, I opened the Google Sheet and navigated to Extensions > Apps Script. Here’s the script I used:

function sendWeeklyProgressReports() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Workout Data");
  const data = sheet.getDataRange().getValues();

  const headers = data[0];
  const records = data.slice(1);

  const emailIndex = headers.indexOf("Email Address");
  const nameIndex = headers.indexOf("Name");
  const workoutTypeIndex = headers.indexOf("Type of Workout");
  const durationIndex = headers.indexOf("Duration (Minutes)");
  const dateIndex = headers.indexOf("Date of Workout");

  const userMap = {};

  records.forEach(record => {
    const email = record[emailIndex];
    const name = record[nameIndex];
    const workoutType = record[workoutTypeIndex];
    const duration = record[durationIndex];
    const date = record[dateIndex];

    if (!userMap[email]) {
      userMap[email] = { name, workouts: [] };
    }

    userMap[email].workouts.push({ workoutType, duration, date });
  });

  for (const [email, userData] of Object.entries(userMap)) {
    const name = userData.name;
    const workouts = userData.workouts;

    let emailBody = `Hi ${name},\n\nHere's your workout summary for the week:\n\n`;
    let totalDuration = 0;

    workouts.forEach(workout => {
      emailBody += `- ${workout.date}: ${workout.workoutType}, ${workout.duration} minutes\n`;
      totalDuration += workout.duration;
    });

    emailBody += `\nTotal workout duration: ${totalDuration} minutes\n\nKeep up the great work!\n\nBest regards,\nYour Workout Tracker`;

    GmailApp.sendEmail(email, "Your Weekly Workout Summary", emailBody);
  }
}

Enter fullscreen mode Exit fullscreen mode

This script collects the data from the Google Sheet, groups it by user, and sends an email summary for the week.


Step 4: Automating the Process with Triggers

To make the script run automatically every week, I set up a time-based trigger.

Here’s how:

  1. Open the Apps Script editor.

  2. Click the clock icon for Triggers.

  3. Set the function sendWeeklyProgressReports to run weekly (e.g., every Sunday at 9:00 AM).

Add a screenshot showing the trigger setup here.

Step 5: Adding Optional Features

To make the tracker more visually engaging, I added a dashboard in the Google Sheet. This dashboard included charts and pivot tables to show:

  • Workout types distribution.
  • Weekly workout duration trends.

You can create these visualizations using Google Sheets' built-in chart tools.

Add a screenshot of the dashboard here.

Conclusion

This personalized workout tracker demonstrates how you can use Google Apps Script to simplify repetitive tasks and provide meaningful insights to users. The flexibility of Apps Script allowed me to create a solution that is both functional and easy to scale.

If you’re looking to build something similar or want to adapt this idea for other use cases, I encourage you to explore the possibilities of Google Apps Script. Let me know how it works for you!

Add a closing screenshot of the email summary or a visualization here.

If this inspires you to build something of your own, check out my article on automating workflows for more ideas!

What would you build next? Let me know in the comments!

Top comments (0)