Google has published a new developer page to generate structured data with JavaScript. The guide offers guidance on how it can be done with precision. The new guide states that there are two ways of generating structured data by using JavaScript.
This developer page discusses three ways for generating structured data with JavaScript
- Google Tag Manager
- Custom JavaScript
- Server Side Rendering
JavaScript plays a major role in the way sites are developed. Guidance on how to generate structured data using JavaScript can be really helpful.
Google Tag Manager(GTM)
Google lists six steps to generate structured data dynamically with GTM:
- ‘’Set up and install Google Tag Manager on your site.
- Add the new Custom HTML tag to the container.
- Paste the preferred structured data block within the tag content.
- Install the container as displayed on the Install Google Tag Manager section of the admin menu of your container.
- For adding the tag to your site, publish your container in the Google Tag Manager interface.
- Test the implementation.
Custom JavaScript
Google lists three steps for developing JavaScript structured data with custom JavaScript.
- Look for the type of structured data that you are interested in.
- Edit the HTML of your website for including a JavaScript snippet like the example below(follow the documentation of your hosting service provider or CMS, or share your query with your developers).
- Test With the Rich Results Test
The Google documentation only says that you should test it with Rich Results Test probably since it does two things simultaneously:
- Testing the eligibility of rich results
- Verify if the structured data validates
Google also has a Structured Data Testing Tool which tests if the structured data is valid. However, it doesn’t feedback about rich results.
The Rich Results testing tool shows warnings and errors and also displays whether the structured data is suitable for rich results. There are two ways of testing structured data in Google’s Rich Results Test. The first way is by inserting a URL so that Google crawls the page and extracts structured data and performs data. The second way is to input the code directly.
Google suggests using the URL input and advises not to utilize the code input. Here 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).”