Beeminder Forum

How to post to a Beeminder goal with a Google form

Google forms can be handy for creating an easy way to add new data to a spreadsheet. And you can also set them up to post to a Beeminder goal, too.

  • Create a new form in Google Drive and edit it.
  • Click the three vertical dots in the top right of the screen and select “Script editor.”
  • Copy the following code into the script, and replace the placeholders with your values:
function onSubmit() {
  const url = "https://www.beeminder.com/api/v1/users/[ USERNAME ]/goals/[ GOAL NAME ]/datapoints.json?auth_token=[ API TOKEN ]"
  
  const options = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({ value: 1 })
  }

  UrlFetchApp.fetch(url, options)
}
  • Save the script.
  • Click the Run or Play button in the toolbar.
  • Grant permissions when requested.
  • Check that the function worked and posted to your goal.
  • In the left sidebar select “Triggers.”
  • Click “Add trigger” in the bottom right of the screen.
  • Select “On form submit” in the type menu.
  • Save out of the dialog.
  • If requested, grant additional permissions.

You’re done! From now on, submitting your form will add a datapoint to your Beeminder goal.

Reference:

5 Likes

Thanks, this is great! :pray:

Just to clarify (because I understood it wrong): This adds a datapoint with value 1 to your goal, irrespective of your Google Forms input (= “Track whenever a response to this form is submitted.”)

To add datapoints from the content of a Google Form entry, I am using Zapier: Each form submission creates a row in a spreadsheet, each new row triggers (via Zapier) a datapoint in a beeminder goal. This works well.

Out of interest: → @narthur, do you or does anyone know if I can achieve the same by editing your function?

To specify: I submit the form with a value from 1-10 and want the beeminder datapoint to reflect the number I submitted not just count how often I submitted.

2 Likes

From the looks of it, the payload would need to be adjusted, replacing ‘1’ corresponding to value (the value of the new datapoint) to the value you wish to submit.
The payload could also contain other data the endpoint accepts, such as comment, timestamp, daystamp, and requestid.

I do not know how to obtain that value from the form.

1 Like

It seems like the function gets passed an event object, which has as fields values and namedValues, which look like what you want.

So, e.g.:

function onSubmit(event) {
   const datapointComments = event.namedValues.Comment // or the like
   // etc.
}
4 Likes

That’s awesome!! Seems like you might be able to do some interesting things with this, like having a goal track data that other people enter through a form… :thinking:

2 Likes