How to Use JavaScript to Automate SEO (With Scripts)

Programming and automation are more and more fashionable matters within the SEO business, and rightfully so.

Leveraging new methods to extract, rework, and analyze information at scale with minimal human enter will be extremely helpful.

Although velocity is essential, one of many primary advantages of utilizing automation is that it takes the load off our shoulders from repetitive duties and leaves us extra time to use our brains.

Read on to be taught a few of the advantages of utilizing JavaScript to automate SEO duties, the primary avenues you may take to begin utilizing it, and some concepts to hopefully spark your curiosity.

Why Learn Automation With JavaScript?

A number of improbable automation initiatives in the neighborhood come from SEO professionals coding in Python together with Hamlet Batista, Ruth Everett, Charly Wargnier, Justin Briggs, Britney Muller, Koray Tuğberk GÜBÜR, and lots of extra.

However, Python is just one of many many instruments you should utilize for automation. There are a number of programming languages that may be helpful for SEO resembling R, SQL, and JavaScript.

Advertisement

Continue Reading Below

Outside of the automation capabilities you’ll be taught within the next section, there are clear advantages from studying JavaScript for SEO. Here are just some:

1. To Advance Your Knowledge to Audit JavaScript on Websites

Whether or not you cope with internet apps constructed with fashionable frameworks (e.g., Angular, Vue), the possibilities are that your web site is utilizing a JavaScript library like React, jQuery, or Bootstrap.

(And maybe even some customized JavaScript code for a particular function.)

Learning to automate duties with JavaScript will assist you to construct a extra strong basis to dissect how JavaScript or its implementation could also be affecting your web site’s natural efficiency.

2. To Understand and Use New Exciting Technologies Based on JavaScript

The internet improvement business strikes at an extremely quick tempo. Hence, new transformative applied sciences emerge continuously, and JavaScript is on the heart of it.

By studying JavaScript, you’ll give you the option to higher perceive applied sciences like service workers, which can straight have an effect on SEO and be leveraged to its profit.

Advertisement

Continue Reading Below

Additionally, JavaScript engines like Google’s V8 are getting higher yearly. JavaScript’s future solely seems to be brighter.

3. To Use Tools Like Google Tag Manager That Rely on JavaScript to Work

If you’re employed in SEO, you might be aware of Tag Management Systems like Google Tag Manager or Tealium. These companies use JavaScript to insert code (or tags) into web sites.

By studying JavaScript, you may be higher geared up to perceive what these tags are doing and probably create, handle and debug them in your web site.

4. To Build or Enhance Your Own Websites with JavaScript

One of the nice issues about learning to code in JavaScript is that it’s going to assist you to to construct web sites as aspect initiatives or testing grounds for SEO experiments.

There isn’t any higher manner to perceive one thing than by getting your arms soiled, particularly if what you need to take a look at depends on JavaScript.

Paths to Leveraging JavaScript for SEO Automation

JavaScript was initially developed as a browser-only language however has now developed to be in all places, even on {hardware} like microcontrollers and wearables.

For the needs of SEO automation, there are two primary environments the place you may automate SEO duties with JavaScript:

  • A browser (front-end).
  • Directly on a pc/laptop computer (back-end).

SEO Automation with Your Browser

One of the primary benefits that separate JavaScript from different scripting languages is that browsers can execute JavaScript. This means the one factor you want to get began with JavaScript automation is a browser.

Automation Using the Browser’s Console

The simplest way to get began is utilizing JavaScript straight in your browser’s console.

There are some straightforward and enjoyable automations you are able to do. For instance, you may make any web site editable by typing “doc.physique.content materialEditready = true” in your console.

This could possibly be helpful for mocking up new content material or headings on the web page to present to your purchasers or different stakeholders in your organization with out the necessity for picture modifying software program.

Advertisement

Continue Reading Below

But let’s step it up a bit extra.

The Lesser-Known Bookmarklets

Since a browser’s console can run JavaScript, you may create customized features that carry out particular actions like extracting data from a web page.

However, creating features on the spot could be a bit tedious and time-consuming. Therefore, Bookmarklets are a less complicated manner to save your personal customized features with out the necessity for browser plugins.

Bookmarklets are small code snippets saved as browser bookmarks that run features straight from the browser tab you might be on.

An example of a Bookmarklet.

For instance, Dominic Woodman created a bookmarklet here that enables customers to extract crawl stats information from the previous Google Search Console person interface and obtain it to a CSV.

Advertisement

Continue Reading Below

It would possibly sound a bit daunting, however you may learn the way to create your personal Bookmarklets following the steps in this great resource on GitHub.

Snippets, a User-Friendly Version of Bookmarklets

If you utilize Chrome, there’s a fair easier answer utilizing Snippets. With this, you may create and save the identical sort of features in a way more user-friendly manner.

For instance, I’ve created a small Snippet that checks what number of “crawlable” hyperlinks are on-page and obtain the record to a CSV file. You can download the code from GitHub here.

Chrome snippet link counter SEO.

While these are normally small duties which can be “good to have” you’d in all probability need to do extra heavy-lifting duties that may assist along with your SEO workload in a extra vital manner.

Advertisement

Continue Reading Below

Therefore, it’s significantly better to use JavaScript straight in your laptop computer (or a server within the Cloud) utilizing Node.js.

SEO Automation within the Back-End with Node.js

Node.js is software program that permits you to run JavaScript code in your laptop computer with out the necessity for a browser.

There are some variations between operating JavaScript in your browser and JavaScript in your laptop computer (or a server within the Cloud) however we’ll skip these for now as that is only a quick intro to the subject.

Nodejs homepage screenshot.

In order to run scripts with Node.js, you have to have it put in in your laptop computer. I’ve written a brief weblog submit the place I am going step-by-step on how you can install Node in addition to a number of further setup ideas to make it simpler to get you began.

Advertisement

Continue Reading Below

Although your creativeness is the restrict when it comes to automation, I’ve narrowed it down to a number of areas that I see SEO professionals come again to when utilizing Node.js.

I’ll embrace scripts which can be prepared to run so you will get began very quickly.

Extracting Data From APIs

Collecting data from completely different sources to present insights and advocate actions is among the commonest jobs in SEO.

Node.js makes this extremely easy with completely different choices, however my most well-liked go-to module is Axios.

// Create an index.js file inside a folder & paste the code under

// Import axios module
const axios = require('axios');

// Custom perform to extract information from PageSpeed API
const getApiData = async (url) => {
  const endpoint="https://www.googleapis.com/pagespeedonline/v5/runPagespeed";
  const key = 'YOUR-GOOGLE-API-KEY' // Edit with your personal key;
  const apiResponse = await axios(`${endpoint}?url=${url}&key=${key}`); // Create HTTP name
  console.log(apiResponse.information); // Log information
  return apiResponse.information;
};

// Call your customized perform
getApiData('https://www.searchenginejournal.com/');

To begin interacting with APIs, you want a module that’s ready to deal with HTTP requests (HTTP consumer) and an endpoint (a URL to extract data).

In some circumstances, you may also want an API key, however this isn’t all the time essential.

For a style of how Node.js interacts with APIs, take a look at this script I printed that makes use of Google’s PageSpeed API to extract Core Web Vitals information and different lab metrics in bulk.

Advertisement

Continue Reading Below

Scraping Websites

Whether you desire to to monitor your personal web site, keep watch over your opponents or just extract data from platforms that don’t provide an API, scraping is an extremely useful gizmo for SEO.

Since JavaScript interacts effectively with the DOM, there are lots of benefits to utilizing Node.js for scraping.

The commonest module I’ve used for scraping is Cheerio, which has a really related syntax to jQuery, together with an HTTP consumer like Axios.

// Import modules
const cheerio = require('cheerio');
const axios = require('axios');

// Custom perform to extract title
const getTitle = async (url) => {
  const response = await axios(url); // Make request to desired URL
  const $ = cheerio.load(response.information); // Load it with cheerio.js
  const title = $('title').textual content(); // Extract title
  console.log(title); // Log title
  return title;
};

// Call customized perform
getTitle('https://www.searchenginejournal.com/');

If you want the rendered model of a web site, fashionable modules like Puppeteer or Playwright can launch a headless occasion of an precise browser like Chrome or Firefox and carry out actions or extract data from the DOM.

Chris Johnson’s Layout Shift Generator is a good instance of how to use Puppeteer for SEO. You can discover more info here or download the script here.

There are additionally different choices like JSDOM that emulate what a browser does with out the necessity for a browser. You can mess around with a JSDOM-based script utilizing this Node.js SEO Scraper constructed by Nacho Mascort.

Advertisement

Continue Reading Below

Processing CSV and JSON Files

Most of the time, the information extracted from APIs comes as JSON objects, and JavaScript is ideal for coping with these.

However, as SEOs, we usually cope with information in spreadsheets.

Node.js can simply deal with each codecs utilizing built-in modules just like the File System module or extra simplified variations like csvtojson or json2csv.

Whether you need to learn information from a CSV and rework it into JSON for processing, otherwise you’ve already manipulated the information and also you want to output to CSV, Node.js has your again.

// Import Modules
const csv = require('csvtojson');
const { parse } = require('json2csv');
const { writeFileSync } = require('fs');

// Custom perform to learn URLs and convert it to JSON
const readCsvExportJson = async () => {
  const json = await csv().fromFile('yourfile.csv');
  console.log(json); // Log conversion JSON

  const transformed = parse(json);
  console.log(transformed); // Log conversion to CSV
  writeFileSync('new-csv.csv', transformed);
};

readCsvExportJson();

Create Cloud Functions to Run Serverless Tasks

This is a extra superior case, however it’s extremely useful for technical SEO.

Cloud computing suppliers like Amazon AWS, Google Cloud Platform, or Azure make it extremely easy (and low cost) to arrange “cases of servers” that run customized scripts created for particular functions with out the necessity for you to configure that server.

A helpful instance can be to schedule a perform that extracts information from the Google Search Console API robotically on the finish of day-after-day and shops the information right into a BigQuery database.

Cloud function exmple to extract search console data and load it to big query.

There are a number of transferring elements on this case, however the potentialities are really countless.

Advertisement

Continue Reading Below

Just to present you an instance of how to create a cloud perform, take a look at this episode of Agency Automators the place Dave Sottimano creates his personal Google Trends API utilizing Google Cloud Functions.

A Potential Third Avenue, Apps Script

For me, it made extra sense to begin with the less-opinionated method.

But Apps Script might provide a much less intimidating manner to be taught to code as a result of you should utilize it in apps like Google Sheets that are the bread and butter of technical SEO.

Appscript homepage screenshot.

There are actually helpful initiatives that may give you a way of what you are able to do with Apps Script.

Advertisement

Continue Reading Below

For instance, Hannah Butler’s Search Console explorer or Noah Lerner’s Google My Business Postmatic for native SEO.

If you have an interest in studying Apps Script for SEO, I’d advocate Dave Sottimano’s Introduction to Google Apps Scripts. He additionally gave this awesome presentation at Tech SEO Boost, which explains some ways to use Apps Script for SEO.

Final Thoughts

JavaScript is among the most popular programming languages in the world, and it’s right here to keep.

The open-source group is extremely lively and continuously bringing new developments in several verticals – from internet improvement to machine studying – making it an ideal language to be taught as an SEO skilled.

What you’ve examine on this article is simply the tip of the iceberg.

Automating duties is a step towards abandoning uninteresting and repetitive on a regular basis duties, turning into extra environment friendly, and discovering new and higher methods to convey worth to our purchasers.

Hopefully this text reduces in a small capability the marginally unhealthy popularity that JavaScript has within the SEO group and instills a little bit of curiosity to begin coding.

Advertisement

Continue Reading Below

More Resources:


Image Credit

Featured picture created by writer, May 2021

Recommended For You

Leave a Reply