Generate structured data with JavaScript

Modern 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:

Use Google Tag Manager to generate JSON-LD dynamically

Google Tag Manageris a platform that allows you to manage tags on your website without editing the code. To generate structured data with Google Tag Manager, follow these steps:

  1. Set up and install Google Tag Manageron your site.
  2. Add a newCustom HTMLtag to the container.
  3. Pastethe desired structured datablock into the tag content.
  4. Install the container as shown in theInstall Google Tag Managersection of your container's admin menu.
  5. To add the tag to your website, publish your container in the Google Tag Manager interface.
  6. Test your implementation.

Using variables in Google Tag Manager

Google Tag Manager (GTM) supportsvariables 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 via GTM.

For example, you can dynamically create aRecipeJSON-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>

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:

  1. Find the type of structured datayou are interested in.
  2. Edit your website's HTML to include a JavaScript snippet like the following example (refer to the documentation from your CMS or hosting provider, or ask your developers).
    fetch('https://api.example /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);
    });
  3. Test your implementation with the Rich Results Test.

Using server-side rendering

If you are usingserver-side rendering, you can also include the desired structured data in the rendered output. Check the documentation of your framework to find out how to generate the JSON-LD forthe type of structured data you are interested in.

Test your implementation

To make sure Google Search can crawl and index your structured data, test your implementation:

  1. Open theRich Results Test.
  2. Enter the URL that you want to test.
  3. ClickTest URL.

    Success:If you did everything correctly and yourstructured 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 thedocumentation for your type of structured dataand make sure you've added all the properties. If your problem persists, make sure to also check the guide on fi xing search-related JavaScript problems.