How to create a bunch of Google Alerts, in 3 minutes?
(Source/Credits: https://dev.to/mailmeteor/how-to-create-a-bunch-of-google-alerts-in-3-minutes-54n)
Sometimes you want to create a lot of Google Alerts. Here's how in less than 3 minutes.
title: How to create a bunch of Google Alerts, in 3 minutes? published: true description: Sometimes you want to create a lot of Google Alerts. Here's how in less than 3 minutes. tags: #js #google #alerts #automatization
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:
js
// 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"]
Then, head to Google Alerts so we will learn how it works behind the scene:
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:
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:
We are almost done 🙌 If you paste that into the console, you will have something like:
```js // 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" } );
```
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
```js // 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) ```
I recommend adding it as a snippet on your Google Chrome (learn how to do it right here).
Comments section
thomasc
•May 1, 2024
Trying this in 2023 and getting 404 errors when attempting to run the fetch() code. Anyone have ideas?
bergeny
•May 1, 2024
There's a bug in the script
everytime I run it it shows the following error on this line :
.then(() => { setTimeout(function() {addAlert(i+1)}, (Math.min(i || 2, 30) * 1000)) })
Uncaught SyntaxError: Unexpected token '.'
bergeny
•May 1, 2024
After a trial I found the error is the ";" just before the ".then" line
You just need to remove it
Regards
kremzeek
•May 1, 2024
I got as far as being able to create a Google alert. However, instead of creating separate alerts for every search term assigned to var 'keywords', it creates one alert comprised of each search term separated by commas, i.e. one concatenated search string. Can anyone assist? TIA.
kremzeek
•May 1, 2024
I figured out the issue. I was using davideladio's fix but accidentally declared the param variable with the keywords variable instead of the keyword constant. Deleting the 's' fixed the issue!
paullchaffey
•May 1, 2024
Hello, anyone have a script to add google alerts in bulk?
davideladio
•May 1, 2024
check it out
datamgmtdoc
•May 1, 2024
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!
datamgmtdoc
•May 1, 2024
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