Stay organized with collections Save and categorize content based on your preferences.
Generate structured data with JavaScriptModern websites use JavaScript to display lots of dynamic content. There are a few things you need to look out for when using JavaScript to generate structured data on your websites, and this guide covers best practices and implementation strategies. If you're new to structured data, you can learn more about how structured data works.
There are different ways to generate structured data with JavaScript, but the most common are:
UsingProduct
markup? Be aware that dynamically-generated markup can make Shopping crawls less frequent and less reliable, which can be an issue for fast-changing content like product availability and price. If you're a merchant optimizing for all types of shopping results, make sure your server has enough computing resources to handle increased traffic from Google. Use Google Tag Manager to generate JSON-LD dynamically
Google Tag Manager is a platform that lets you manage tags on your website without editing the code. To generate structured data with Google Tag Manager, follow these steps:
Google Tag Manager (GTM) supports variables to use information on the page as part of your structured data. Use variables to extract the structured data from the page instead of duplicating the information in GTM. Duplicating the information in GTM increases the risk of having a mismatch between page content and the structured data inserted using GTM.
For example, you can dynamically create a Recipe JSON-LD block that uses the page title as the recipe name by creating the following custom variable named recipe_name
:
function() { return document.title; }
You can then use {{recipe_name}}
in your custom tag HTML.
We recommend to create variables to collect all the necessary information from the page using variables.
Here is an example for the custom HTML tag content:
<script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "Recipe", "name": "{{recipe_name}}", "image": [ "{{recipe_image}}" ], "author": { "@type": "Person", "name": "{{recipe_author}}" } } </script>Note: The previous sample assumes that you defined the variables
recipe_name
, recipe_image
and recipe_author
in GTM. Generate structured data with custom JavaScript
Another way you can generate structured data is by using JavaScript to either generate all of your structured data or add more information to the server-side rendered structured data. Either way, Google Search can understand and process structured data that's available in the DOM when it renders the page. To learn more about how Google Search processes JavaScript, check out the JavaScript basics guide.
Here is an example of JavaScript-generated structured data:
fetch('https://api.example.com/recipes/123') .then(response => response.text()) .then(structuredDataText => { const script = document.createElement('script'); script.setAttribute('type', 'application/ld+json'); script.textContent = structuredDataText; document.head.appendChild(script); });
If you are using server-side rendering, you can also include the structured data in the rendered output. Check the documentation of your framework to find out how to generate the JSON-LD for the type of structured data you're interested in.
Test your implementationTo make sure Google Search can crawl and index your structured data, test your implementation:
Success: If you did everything correctly and your structured data type is supported in the tool, you will see the message "Page is eligible for rich results".
If you are testing a structured data type that is not supported by the Rich Results test, check the rendered HTML. If the rendered HTML contains the structured data, Google Search will be able to process it.
Try again: If you see errors or warnings, it is most likely a syntax error or a missing property. Read the documentation for your type of structured data and make sure you've added all the properties. If your problem persists, make sure to also check the guide on fixing search-related JavaScript problems.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-02-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-04 UTC."],[[["This guide provides best practices for implementing structured data with JavaScript on your website to enhance search engine understanding."],["Google Tag Manager and custom JavaScript code are the primary methods for dynamically generating structured data."],["When using JavaScript, ensure your server has enough resources to handle potential increased traffic from Google, especially with `Product` markup."],["Validate your structured data implementation using the Rich Results Test to identify and resolve any errors or warnings."],["Refer to the comprehensive structured data documentation and JavaScript troubleshooting guide for detailed information and support."]]],["JavaScript dynamically generates website content and structured data through methods like Google Tag Manager (GTM) or custom JavaScript. GTM allows adding structured data via Custom HTML tags and using variables to avoid data duplication. Custom JavaScript can generate or augment structured data, which becomes available in the DOM. Server-side rendering can also include structured data. Implementation should be validated with the Rich Results Test, ensuring the data is crawlable and indexable by Google Search.\n"]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4