عWork with me

Structured data with JavaScript and Tag Manager

When it is safe to generate JSON-LD in the browser or with Google Tag Manager, the risks for products, and how to test and keep it in sync with the page.

Ahmed Ali · 4 min read
Structured data with JavaScript and Tag Manager
Server-rendered JSON-LDin the first HTMLsame data as the pageeasiest to keep in syncScript or Tag Managerin the DOM after renderingrisk of mismatchcare for product datavs
Prefer the server. Use scripts and Tag Manager with care, and keep one source of truth.

In short: Google can process structured data that is added by JavaScript or Google Tag Manager, as long as it is in the rendered DOM. It is slower to be seen than markup in the server HTML, easier to get out of sync with the page and, for products, can make crawls of price and availability less reliable. Prefer the server, and use script injection with care. This article is part of the JavaScript SEO guide.

What Google says

Google’s guide to generating structured data with JavaScript describes two routes: Google Tag Manager and custom JavaScript. A script can generate all of the markup or add to what the server rendered, and Google Search can process structured data that is present in the DOM after the page renders. Server-side rendering is another route that includes it in the output.

The same page raises two cautions:

  • Mismatch risk. Typing values into Tag Manager that already exist on the page duplicates information and “increases the risk of having a mismatch between page content and the structured data”. Pull values from the page with variables instead.
  • Product markup. Dynamically generated markup can make Shopping crawls less frequent and less reliable, which matters for fast-changing details such as price and availability.

Which route to choose

Route In the first HTML Sync with the page Best for Watch for
Server-rendered JSON-LD Yes Easiest, same data source Articles, organizations, products, anything you control Needs a developer
Custom JavaScript No, in the DOM after rendering Depends on your code Apps where the server cannot output it Delay, failures, mismatch
Tag Manager custom HTML No, in the DOM after rendering Easy to drift Fast fixes on sites you cannot change Duplicated values, container changes, product data

When you can edit templates, put JSON-LD on the server. Use Tag Manager when you truly cannot, and treat it as a stopgap for important markup.

Rules that keep it safe

  1. Mark up what users can see. Google’s general structured data guidelines require markup to reflect the page content. Do not add types or properties for content that is not there.
  2. Pull values from the page. In Tag Manager, read the title, price, image and date from the page with variables. In custom code, read them from the same data the page uses.
  3. Use one source of truth. If the server also outputs markup, do not inject a second copy. Duplicate or conflicting blocks are confusing.
  4. Keep identifiers stable. Use consistent @id values so entities connect across pages. The structured data entry explains how entities connect.
  5. Output after the data is ready. If the script runs before the API returns, it may output empty values. Build the block from the data that has arrived.
  6. Do not block the script. If robots.txt blocks the JavaScript or the tag file, Google cannot render it.
  7. Version your container. Review changes to Tag Manager containers like code changes, with a test after publishing.

Product pages deserve extra care

Price, availability and ratings change often. If Product markup is generated only in the browser, crawls of that data can be less frequent and reliable, and it may lag behind the page. For a store, output Product markup on the server and make sure it matches the visible price and stock. Google’s Product structured data guide lists the properties.

How to test it

  1. Rich Results Test, URL input. Google recommends testing a URL rather than pasted code, because of JavaScript limits such as CORS. Check that the detected items are the ones you expect: Rich Results Test.
  2. Check the rendered HTML. For types the Rich Results Test does not cover, look in the rendered HTML from URL Inspection for the JSON-LD block. How to check what Google really sees shows where to find it.
  3. Validate the syntax. The Schema Markup Validator checks any type.
  4. Compare with the page. Confirm every value in the markup matches what a visitor sees.
  5. Watch Search Console. Enhancement reports show errors and warnings for types Google supports.
  6. Re-test after releases and container publishes.

Common problems

  • Markup appears in the browser but not in the rendered HTML, because the script fails or is blocked.
  • Two blocks of the same type, one from the server and one from a tag.
  • Prices in markup that differ from the page.
  • Tag Manager fires after consent and so misses crawlers. Make sure markup does not depend on user consent for crawlers to see it, when it describes content that is public. This is a design decision to review with whoever owns privacy.
  • Types copied from a template that do not match the page.

This falls under my JavaScript rendering SEO service work, so get in touch if you want a hand.

FAQ

Can Google read JSON-LD added by JavaScript or Tag Manager?

Yes. Google's documentation says structured data present in the DOM after the page renders can be processed. The risk is timing and reliability, not impossibility.

Is it better to output JSON-LD on the server?

Usually yes. Server-rendered JSON-LD is in the initial HTML, does not depend on a script running and is easier to keep in sync with the visible content. Use browser or Tag Manager injection when the server cannot do it.

How do I test JavaScript-generated markup?

Use the Rich Results Test with the URL input, which Google recommends over pasted code, and check that the rendered HTML contains the markup. Validate other types with the Schema Markup Validator.

Sources

Keep reading

Related glossary terms

Want this applied to your site?

Tell me about your site and I’ll suggest the next step.

Work with me Services
Book a free audit call

Work with me

Tell me about your site and goals. I usually reply within 24 hours.

Your message comes straight to me. I usually reply within 24 hours.

Esc

↑↓ move · Enter open · Esc close