DEV Community

loading...

How to create a bunch of Google Alerts, in 3 minutes?

Corentin
Co-founder and VP Engineering at Mailmeteor.com 🚀
・3 min read

Google Alerts is a very handy and powerful service to stay informed of what's happening in a particular field.

I'm a huge fan of this service and I create dozens of new alerts every week. Recently, I wanted to add 100+ Google Alerts to being informed about APIs that we're working with at Bearer.

Unfortunately, Google Alerts doesn't provide an API. And I didn't felt ok to share my Google credentials (email + password) to the available libraries.

What do we need?

First, a list of keywords that we want to add alerts on:

// Here's my list of keywords to add Google Alerts on;
// Change it with whatever you want to be informed of.
const keywords = ["GitHub API", "Google Alerts API", "Dev.to API"]
Enter fullscreen mode Exit fullscreen mode

Then, head to Google Alerts so we will learn how it works behind the scene:

Google Alerts homepage

I'm using Google Chrome, but that should work just fine with Safari or Firefox.

Create a Google Alert with JS

On Google Alerts homepage, open the developer tools Alt+Command+J (on Mac) or Ctrl+Shit+J (on Windows), then open the Network tab. You should see something like:

Network request opened on Google Alerts homepage

Now create a sample alert using dev.to as the keyword. The network tab will show a request to the /create endpoint. Use Copy as fetch to see what's inside that request:

Request to the /create endpoint

We are almost done 🙌 If you paste that into the console, you will have something like:

// Code has been prettified
fetch(
  "https://www.google.com/alerts/create?x=ABJHsmWAbcU-xxxxxxxxxxxxxxxxxxxxx&hl=us",
  {
    "credentials": "include",
    "headers": {
    "accept": "*/*",
    "accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "pragma": "no-cache",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-client-data": "xxxxxxxxxxxxxxxxxxxxx"
    },
    "referrer": "https://www.google.com/alerts?hl=us",
    "referrerPolicy": "no-referrer-when-downgrade",
    // The dev.to keyword is passed ==================== right here ∨∨∨
    "body": "params=%5Bnull%2C%5Bnull%2Cnull%2Cnull%2C%5Bnull%2C%22dev.to%22%2C%22com%22%2C%5Bnull%2C%22en%22%2C%22US%22%5D%2Cnull%2Cnull%2Cnull%2C0%2C1%5D%2Cnull%2C3%2C%5B%5Bnull%2C1%2C%22corentin%40bearer.sh%22%2C%5Bnull%2Cnull%2C10%5D%2C2%2C%22en-US%22%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%220%22%2Cnull%2Cnull%2C%22AB2xxxxxxxxxxx%22%5D%5D%5D%5D",
    "method": "POST",
    "mode": "cors"
  }
);

Enter fullscreen mode Exit fullscreen mode

As you might see, the dev.to keyword is passed into the body. Changing it to something else, will let us automatically add a new Google Alert 🥳

A script that creates Google Alerts in bulk

// Replace with your keywords list
var keywords = ["GitHub API", "Google Alerts API", "Dev.to API"]

function addAlert(i) {
  // Retrieve the keyword to work with
  const keyword = encodeURIComponent(keywords[i])

  // Stop the script if there's no keyword
  if (!keywords[i] || !keyword) { return; }

  console.log(`Adding ${keyword}`)

  // 1. Replace the line below with your own fetch (see Copy as fetch above)
  // 2. Replace `dev.to` with `${keyword}`
  fetch(/*...*/)

  // Exponentially delay the next request,
  // to avoid rate limit on Google.
  .then(() => { setTimeout(function() {addAlert(i+1)}, (Math.min(i || 2, 30) * 1000)) })
}

addAlert(0)
Enter fullscreen mode Exit fullscreen mode

I recommend adding it as a snippet on your Google Chrome (learn how to do it right here).

Discussion (6)

Collapse
datamgmtdoc profile image
Joshua Girard • Edited

I get an error with unexpected token '.'
debugger:///VM90496

Attached is the screenshot...dev-to-uploads.s3.amazonaws.com/i/...
I assume you don't need a const and var for keywords? I also got an error saying keywords had been defined already. Probably user error but missing a step here...

Thanks!

Collapse
datamgmtdoc profile image
Joshua Girard • Edited

Total NEWB - just needed to remove the ';' from after the fetch which ended the statement...that said I got a notification that it added my keywords however when I refresh I only see it added ${keyword} to my alerts....?? Is this right: 22${keyword}%22

Collapse
davideladio profile image
davideladio

Hi there, im getting the same. the Javascript code works but it adds an alerts with the actual text '${keyword}' as the term to be looked up in the alert. I can't make the code work to extract the ${keyword} value and use it to set the alert :(

Anyone worked this out? Thanks so much!!!

Thread Thread
davideladio profile image
davideladio

Fixed!!! The issue is that having the 'params' inside an string like this:

"params=hexcode${keyword}hexcode" makes the interpreter to not extract the value for ${keyword}

Instead of that in your code define 2 new var like this:
var left = "params=hexcode%22"

This var has 'all the text on the left of the actual keyword you want to add'

var right = "%22hexcode"

This var has 'all the text on the left of the actual keyword you want to add'

Now in addAlert(i) build a new params var containing the term you want to add in your google alert like this:

function addAlert(i) {
// Retrieve the keyword to work with
const keyword = encodeURIComponent(keywords[i])

//build the params var with the term to be added
var params = left + keyword + right

And all that's left is to use it (without any '$') in your fetch getting something like this:

"referrer": "google.es/alerts",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": params,
"method": "POST",
"mode": "cors",
"credentials": "include"

I hope this will help anyone!!!

Collapse
paullchaffey profile image
paull chaffey

Hello, anyone have a script to add google alerts in bulk?

Collapse
davideladio profile image
davideladio

check it out