Beeminder Forum

Gmail Labeler script

Hi folks!

Beeminder’s got to switch its Gmail integration to look at labels, instead of arbitrary searches. (Lots of details available here: https://blog.beeminder.com/gmailzerolabels/)

Nearly all searches can be turned into labels through filters at Gmail. Go to Gmail, type the search in the box, but instead of hitting enter, click the little down arrow. Click “Create Filter”, and now you’ve got a label automatically assigned when email comes in!

However, not all searches work–for instance, older_than. Some folks may find it slick to filter on “in:inbox older_than:1d”. Gmail won’t create a filter for that, but there’s a pretty good workaround.

Google has a thing called Google Apps Script. It’s Javascripty, and runs on their servers, and has all the authentication rigamarole worked out. They can be set to run automatically, or manually–it’s really quite slick.

I had committed to writing a Google Apps Script if anyone needed it for the Gmail transition here at the end of 2019, and someone did need it. Before writing one, though, I did a search, and found a great, well-documented example! It uses a config file, and runs every 10 minutes.

Here’s my config. You can use it and its instructions to set something up for yourself.

//Adam's sample configuration for "Gmail Filter by Search Query", https://gist.github.com/isiahmeadows/63716b78c58b116c8eb7
//v1

//This example will add the already-created label "Stagnant" to any threads in my inbox that are older than 1 day,
//and adds the already-created label "Stale" to any threads that are older than 3 days.  It does this every ten minutes!
//Then, Beeminder can pick up those labels.

//As of 2020, Beeminder's Gmail integration looks at labels and can't do arbitrary searches.  Most searches can be
//turned into a filter in Gmail that applies a label, but a few can't, like "older_than:".  This example
//uses an open source script to have Google search your email every ten minutes and apply labels.

//To set this up yourself, go to https://gist.github.com/isiahmeadows/63716b78c58b116c8eb7 and follow the directions.
//You can base your configuration off this one.

//As of December 30th, 2019, there's a small error in the source's Company label example.
//I've corrected it and hopefully it will be updated soon.

//If you get Invalid argument: label, you probably need to create the label first!
//You can do this in Gmail or via GmailApp.createLabel("labelname");

__setup({
    queries: [
      ["in:inbox older_than:1d ",  function (thread) {
            thread.addLabel(GmailApp.getUserLabelByName("Stagnant"));
        }],
      ["in:inbox older_than:3d", function (thread) {
            thread.addLabel(GmailApp.getUserLabelByName("Stale"));
        }],
    ],
      
    notify: {
      // The email the script runs under--change it to your email.
        email: "CHANGETOYOUREMAIL@gmail.com",
        subject: "Filter Summary",
        body: "%c emails processed in the past 7 days.",
    },
});

Let me know what you think!

4 Likes

Dynamic things like Categories need a little more work to label, because the label will stick around even after the category changes, or they’re not in the inbox anymore. We can add a step that removes the label from anything that no longer matches.

//Adam's sample configuration for "Gmail Filter by Search Query" https://gist.github.com/isiahmeadows/63716b78c58b116c8eb7

//Primary Category

//This example will add a label "primarycat" to any threads that are in my inbox and in the Primary category.
//It removes the label from anything that gets removed from the Primary category or from the inbox.
//It does this every ten minutes!  Then, Beeminder can pick up those labels.

//As of 2020, Beeminder's Gmail integration looks at labels and can't do arbitrary searches.  Most searches can be
//turned into a filter in Gmail that applies a label, but a few can't, like "older_than:".  This example
//uses an open source script to have Google search your email every ten minutes and apply labels.

//To set this up yourself, go to https://gist.github.com/isiahmeadows/63716b78c58b116c8eb7 and follow the directions.
//You can base your configuration off this one.

//As of December 30th, 2019, there's a small error in the source's Company label example.
//I've corrected it and hopefully it will be updated soon.

//If you get Invalid argument: label, you probably need to create the label first!
//You can do this in Gmail or via GmailApp.createLabel("labelname");

//Labeling based on dynamic features

//There are some dynamic email features, like Priority Inbox and Categories, that don't match
//labels, but we can map them to labels.

//In general, we build a query that maps to this dynamic feature.  We add the label to anything
//that matches this query, and then remove the label from anything that no longer matches the
//query.

const category = "primary";
const categoryLabel = "primarycat";

const labelQuery = "in:inbox category:" + category;
  
__setup({
    queries: [
      //Add the label to anything matching the query
      ["in:inbox category:" + category, function (thread) {
            thread.addLabel(GmailApp.getUserLabelByName(categoryLabel));
        }],
      //Remove the label to no longer matching the query
      ["-(" + labelQuery + ") label:" + categoryLabel,  function (thread) {
            thread.removeLabel(GmailApp.getUserLabelByName(categoryLabel));
        }],      
    ],
      
    notify: {
      // The email the script runs under--change it to your email.
        email: "CHANGETOYOUREMAIL@gmail.com",
        subject: "Filter Summary",
        body: "%c emails processed in the past 7 days.",
    },
});
1 Like