Skip to content

Data already in the HTML

JSON-LD added by Tag Manager: in the rendered DOM, not in the HTML

Find out whether a page's JSON-LD is in the first HTTP response or injected by a tag manager after load, and move it into the server HTML so fetchers read it.

By Cerys Eaton, SiteRender team · Published

This article was drafted with AI assistance from the sources listed below and edited and published by the SiteRender team.

Is this page's JSON-LD in the first response?
  • Fetch the raw HTML and count application/ld+json blocks
  • Look for googletagmanager.com/gtm.js in the same file
  • Render the page and list the JSON-LD types again
  • A type that appears only after rendering was injected by a script
  • Fix: emit the script tag from the server and escape the less-than sign

Google Search can process the Product or Article block on your page once it renders the page. A plain HTTP fetch of the same URL finds no JSON-LD at all. The block was added by a script after load, for example by a tag manager, and a plain fetch does not run that script.

Where tag-manager JSON-LD lives

Google documents two common ways to generate structured data with JavaScript: Google Tag Manager and custom JavaScript. The Tag Manager route is a Custom HTML tag: you paste a structured data block into the tag, then publish the container.

That block is not written into your page template. In the current install snippet, the page loads the container from googletagmanager.com/gtm.js?id=GTM-… with an async script tag placed as high in the <head> as possible. As Simo Ahava's 2020 guide to Custom HTML tags describes it, the tag's HTML is stored, encoded, inside the published container JavaScript, and when the tag fires, Tag Manager parses the string through a temporary element's innerHTML and appends each resulting element as the last child of document.body.

So the <script type="application/ld+json"> exists in the DOM after the container loads and the tag fires. It is not in the bytes the server sent.

The custom JavaScript route ends the same way: a script fetches the structured data and inserts an application/ld+json element into the page at runtime.

Who reads it

Google says Google Search can process structured data that is in the DOM when it renders the page. For a search engine that renders, the injected block counts. Google's page adds one caveat for products: dynamically generated markup can make Shopping crawls less frequent and less reliable, which matters for prices and availability that change often.

A reader that does not run JavaScript gets the server HTML and nothing more. Vercel's December 2024 analysis of crawler traffic found that none of the major AI crawlers it measured rendered JavaScript, naming GPTBot, OAI-SearchBot, ChatGPT-User, ClaudeBot, PerplexityBot, Meta-ExternalAgent and Bytespider, with Gemini and AppleBot as the exceptions. Crawler behavior changes, and the 2026 log evidence is a separate question. A plain curl, a requests call or a static fetcher in your own pipeline is in the same position either way.

Check the raw response

URL=https://example.com/
curl -sL "$URL" -o raw.html
grep -o 'application/ld+json' raw.html | wc -l
grep -oE 'googletagmanager\.com/gtm\.js|GTM-[A-Z0-9]+' raw.html | sort -u

Zero JSON-LD blocks plus a container ID: the structured data, if the page has any, arrives through a script.

A nonzero count does not settle it. A page can ship an Organization block from its template and add the Product block through a tag. Compare types, not counts.

Diff raw and rendered JSON-LD

This script lists the @type values in the raw HTML, then in the rendered DOM. It waits for a new JSON-LD block to appear instead of sleeping for a fixed time, and gives up after 10 seconds.

# jsonld_diff.py: pip install requests playwright && playwright install chromium
import json, re, sys
import requests
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError

URL = sys.argv[1] if len(sys.argv) > 1 else "https://example.com/"
PATTERN = re.compile(r'<script[^>]+application/ld\+json[^>]*>(.*?)</script>', re.S | re.I)

def types(blocks):
    found = []
    for raw in blocks:
        try:
            data = json.loads(raw)
        except json.JSONDecodeError:
            found.append("(unparsable)")
            continue
        if isinstance(data, list):
            items = data
        elif isinstance(data, dict):
            items = data.get("@graph", [data])
        else:
            items = []
        for item in items:
            if isinstance(item, dict):
                found.append(str(item.get("@type")))
    return sorted(found)

raw_blocks = PATTERN.findall(requests.get(URL, timeout=30).text)

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto(URL, wait_until="load")
    try:
        page.wait_for_function(
            "n => document.querySelectorAll(\"script[type='application/ld+json']\").length > n",
            arg=len(raw_blocks),
            timeout=10_000,
        )
    except PlaywrightTimeoutError:
        pass
    rendered = page.locator("script[type='application/ld+json']").all_text_contents()
    browser.close()

raw_types, rendered_types = types(raw_blocks), types(rendered)
print("raw HTML:     ", raw_types)
print("rendered DOM: ", rendered_types)
print("script-only:  ", sorted(set(rendered_types) - set(raw_types)))
python jsonld_diff.py https://example.com/

An empty script-only line: the JSON-LD is in the first response, and any fetcher can read it with the parser from the structured data guide. Anything listed there exists only for readers that run the page's scripts. A tag that fires on a later trigger, such as a click or a scroll, will not show up in either list.

Fix it on the server

Google's page notes that with server-side rendering you can include the structured data in the rendered output. That is the fix: emit the <script type="application/ld+json"> from the page template, then delete the Custom HTML tag so the page does not carry two copies.

In a Next.js App Router app, the docs recommend rendering JSON-LD as a <script> tag in layout.js or page.js:

// app/products/[id]/page.js
export default async function Page({ params }) {
  const { id } = await params
  const product = await getProduct(id)

  const jsonLd = {
    '@context': 'https://schema.org',
    '@type': 'Product',
    name: product.name,
    description: product.description,
  }

  return (
    <section>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{
          __html: JSON.stringify(jsonLd).replace(/</g, '\\u003c'),
        }}
      />
    </section>
  )
}

getProduct is your own data function. Keep the .replace step: the Next.js docs point out that JSON.stringify does not sanitize malicious strings used in XSS injection, and they suggest replacing < with its unicode equivalent \u003c. They also say a native <script> tag is the right choice here rather than next/script, since JSON-LD is structured data, not executable code.

Building the values from the same data as the visible page has a side benefit. Google's Tag Manager advice already warns that duplicating information in a tag raises the risk of a mismatch between page content and the injected structured data; a server template reads the same record as the page.

Test it yourself

Run jsonld_diff.py on one product page and one article page of a site you run. If script-only lists a type, move that block into the template, deploy, and run the script again. The goal is an empty script-only line and the same types in both lists.

Frequently asked questions

Can Google read JSON-LD added with Google Tag Manager?

Yes. Google's documentation says Google Search can process structured data that is in the DOM when it renders the page, and it describes a Custom HTML tag in Tag Manager as one way to add it. For product markup it warns that dynamically generated markup can make Shopping crawls less frequent and less reliable.

Do AI crawlers see JSON-LD injected by JavaScript?

A crawler that does not run JavaScript only gets the HTML the server sent, so a block a script adds later is not in it. Vercel's December 2024 analysis found that none of the major AI crawlers it measured rendered JavaScript, with Gemini and AppleBot as the exceptions.

How do I check whether JSON-LD is server-rendered?

Fetch the page with curl and count the application/ld+json script blocks in the raw file. Then render the page in a headless browser and count again. A block that only shows up in the rendered DOM was added by a script after load.

How do I move JSON-LD out of Google Tag Manager?

Write the script tag with type application/ld+json in the server-rendered template of the page, then remove the Custom HTML tag from the container. In a Next.js App Router app, the docs recommend rendering it in layout.js or page.js and replacing the less-than character with its unicode escape to guard against XSS.

Sources

  1. Generate structured data with JavaScript (Google Search Central) (Documentation)
  2. The Custom HTML Tag Guide for Google Tag Manager (Simo Ahava, 2020) (Blog)
  3. Install a web container (Tag Manager Help) (Documentation)
  4. The rise of the AI crawler (Vercel blog, December 2024) (Blog)
  5. How to implement JSON-LD (Next.js docs) (Documentation)