Skip to content
  • Texas | Arizona | Virginia | Idaho | Illinois
  • (888) 705-0930
  • info@therawragency.com
Facebook-f Twitter Instagram Linkedin-in
rawr logo short
  • Home
  • About
  • Solutions
    colorized icons 04
    B2B Marketing
    colorized icons 05
    B2C Marketing
    colorized icons 06
    Online Visibility Management
  • Industries
    colorized icons 07
    Healthcare
    colorized icons 08
    Manufacturing
    colorized icons 10
    Home Services
    colorized icons 09
    Professional Services (B2B)
    colorized icons 11
    Retail
  • Services
    colorized icons 01
    Brand Strategy
    • Graphic Design
    • Corporate Brand Identity
    • Sales Enablement
    • Editorial Calendar Management
    colorized icons 02
    Website Design
    • WordPress Website Development
    • Conversion Rate Optimization
    • eCommerce Development
    • Content Strategy & Copywriting
    colorized icons 03
    Digital Marketing
    • Digital Marketing Strategy
    • Search Engine Optimization (SEO)
    • Pay-Per-Click (PPC) Management
    • Content Marketing & Digital PR
    • Account Based Marketing (B2B)
    • Marketing Automation
    • Social Media Management
  • Blog
  • Contact
Let's Talk

Coding for SEO: Using JavaScript to track COVID-19

  • April 1, 2020
  • General
  • Agency
Facebook
Twitter
LinkedIn
Email

Alongside tanking search rankings, you may encounter a Google Search Console warning about First Input Delay. After profiling page performance using Dev Tools (performance tab, CPU chart), you might find JavaScript is consuming time on the CPU main thread. That JavaScript probably contains widely supported, but older-style, XML HTTP Request (XHR) for a network resource.

Modern JavaScript doesn’t have to lock up the CPU main thread the way XHR does. New patterns also make it easy to request and manage JSON data. Let’s see how it can be done with a simple script that fetches COVID-19 statistics to display in your browser.

Copy the source code above and paste it into a text editor. Save it as a local file on your desktop (or somewhere you’ll remember). Make sure to give it the .html extension so that it opens easily using your web browser. Double-click the new file, or navigate to it using your browser’s [File]→[Open File] menu item.

Provided you’re connected to the Internet, you should see statistics appear on the page. Our script fetches JSON data from a source managed by several journalists and unpacks it to update your page DOM. Refresh the page or visit it periodically to track the latest statistics.

Assigning functions as values

When considering this code, skip over the opening HTML to look at what’s happening inside the script tag. The first line of JavaScript is a statement that assigns a complex value — a function — to the name “getCovidStats.”

const getCovidStats = async() => {

The assignment value is everything between the first equal sign after “getCovidStats” and the semicolon on line 28: async() => { ...};

The semicolon here terminates the value assignment.

Since the code declares an async() function, when we reference getCovidStats, we’re going to need to do so in the form for calling functions for it to work. We need to use parentheses as you see them on line 29: getCovidStats();

Notice how we named getCovidStats() after what the function does? It’s a good idea to name functions after their primary action — labeling what our functions do — as a general naming convention. This makes it easier for future programmers to follow our code. It also makes it easier for you to follow your own code if you return to it months or years down the road.

A note about constants

The const keyword is shorthand for ‘constant,’ which is a general programming notion for a value that should not change. In this case, getCovidStats is a constant.

JavaScript constants cannot be reassigned or redeclared without errors. JavaScript provides error messages as guard rails for us to avoid buggy code. The condition forces us to use our constants properly.

When you’re writing constant values you “hard-code” values to the variable names as if chiseling them in stone. We often write functions as constant variables because we don’t want our functions to behave unpredictably.

The async() function

Don’t sweat the => arrow — the body of our async() function is bounded by a set of curly braces: {}, opening on line 9 and closing on line 28. Notice the nested try {}, catch {}, and finally {} methods which are also defined using curly braces as bounds. The code inside curly braces is what will run when these functions or methods are called.

They are called here sequentially.

Await

Our async() function introduces a special new keyword used in the try block: await. When we use async() { await { code } } it implies a JavaScript ‘Promise’ that the await code will eventually return a value at some future point. Using Promise won’t block processing other tasks, because we can count on it returning a value.

Fetch API

We’re using fetch() which returns a response object for the URL we designate as a required argument. Network methods such as fetch() are perfect to use with await Promises, because network connectivity is totally unpredictable. We don’t want our program to stop waiting for an immediate response. We want to move on and count on the fact the Promise will return down the line.

A note about browser support

The Fetch API and async() functions are modern JavaScript patterns that aren’t always supported by old browsers, particularly non-Edge IE browsers. Heed the warning about writing code for your target market.

If you need to support old browsers then look into a deployment strategy that offers advanced features with polyfills or can gracefully decline to your lowest denominator browser.

Our covid-19 data source

The source we use is kept up by several journalists working for major news organizations. It offers us JSON responses to simple GET requests. That is ideal for our purposes.

There are scattered data sources supporting International sites that produce amazing visualizations of the outbreak. Looking at what these use for data and trying to find an open API can lead to PDF (WHO situation reports) or CSV (John Hopkins), but for simplicity here we want JSON.

More development tips for SEOs

  • Our SEO for Developers Column
  • Coding for SEO 101: Understanding source code, compressed code and compiled code
  • SameSite requirements for cookies: What SEOs and developers need to know
  • Connect to remote hosts with secure shell

The post Coding for SEO: Using JavaScript to track COVID-19 appeared first on Search Engine Land.


Source: IAB

Facebook
Twitter
LinkedIn
Email

Recent Blog Posts

9 Hidden Revenue Blockers Every CRO Needs to Eliminate

February 3, 2025

What Is B2B Appointment Setting?

November 29, 2024

Leveraging LinkedIn for Targeted Growth

November 12, 2024

Mastering B2B Prospecting: Key Strategies for Sales Success

November 4, 2024

Maximize Marketing ROI: How Cost per Lead Can Fuel Your Business Growth

June 13, 2024
View More
rawr logo short
Facebook-f Twitter Instagram Linkedin-in
Get In Touch
  • Texas | Arizona | Virginia | Idaho | Illinois
  • (888) 705-0930
  • info@therawragency.com
Send An Email

"*" indicates required fields

Copyright 2025 | The RAWR Agency, LLC. |

Sitemap | Privacy Policy

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkNo