Google published a new developer page for generating structured data with JavaScript. The guide provides guidance on how to do it properly so that it wins in Google search.
The new guide states that there are two ways to generate structured data using JavaScript.
Google’s Martin Splitt announced Google’s new JavaScript structured data guidelines on LinkedIn
The developer page discusses three ways to generate structured data with JavaScript
- Google Tag Manager
- Custom JavaScript
- Server Side Rendering
JavaScript plays an increasing role in how sites are developed. Guidance on how to dynamically generate structured data using JavaScript is going to be immensely helpful.
Google Tag Manager (GTM)
Google lists six steps for generating structured data dynamically with GTM:
- “Set up and install Google Tag Manager on your site.
- Add a new Custom HTML tag to the container.
- Paste the desired structured data block into the tag content.
- Install the container as shown in the Install Google Tag Manager section of your container’s admin menu.
- To add the tag to your website, publish your container in the Google Tag Manager interface.
- Test your implementation.”
Custom JavaScript
Google lists three steps for developing JavaScript structured data with custom JavaScript
- Find the type of structured data you are interested in.
- Edit your website’s HTML to include a JavaScript snippet like the example below (refer to the documentation from your CMS or hosting provider, or ask your developers).
- Test With the Rich Results Test
The Google documentation only says to test it with the Rich Results Test probably because it does two things at once:
- Tests for eligibility for rich results
- Checks if the structured data validates
Google also has the Structured Data Testing Tool that tests if the structured data is valid but doesn’t give feedback about rich results.
The Rich Results testing tool does double duty by showing warnings and errors in addition to showing if the structured data is eligible for rich results.
There are two ways to test structured data in Google’s Rich Results Test. The first way is by entering a URL so that Google crawls the page and extracts the structured data and tests it.
The second way is to input the code directly.
Google recommends using the URL input and advises to not use the code input.
This is Google’s recommendation for testing JavaScript dynamically generated structured data:
“We recommend that you use the URL input instead of the code input because there are JavaScript limitations when using the code input (for example, CORS restrictions).”
Read Google’s new documentation here:
Generate structured data with JavaScript