简体   繁体   中英

Using Google Tag Manager to Dynamically Generate Schema/JSON-LD Tags, but keep getting the following error: ' unparsable structured data'

Good day, I really need someone's help, please. I am trying to dynamically generate schema for the news article section. When I test it using the rich results tool, everything looks fine, but google console returns the following error: 'unparsable structured data'. I presume I have to wrap the code up in JS, but I am not very strong in this, will appreciate your help, please. Thanks very much for looking into this for me.

The screenshot of the jsonld script jsonld脚本截图

In short: you're most likely good. The error you're seeing is just due to the fact that Google testing tool fetches source and doesn't render DOM. Google Bot will render it properly, so no issues there.

You certainly don't need a JS wrapper, though you can use it. I used it with schema when I didn't want to inflate the size of it with variables. Here is a quick example:

 var script = document.createElement('script'); script.type = "application/ld+json"; script.innerHTML = JSON.stringify(data); document.getElementsByTagName('head')[0].appendChild(script);

Where Data is your JS object representing the schema json.

Your error is most likely due to Google Fetching the code rather than rendering the page and letting JS populate the DOM.

Deployment of Schema with GTM, though convenient, leads to issues in Future. It should be considered as a temporary solution contributing to the technical debt.

I just tested your code and it looks fine.

Here is the code for those who want it:

 <script type="application/ld+json"> { "@context": "https://schema.org", "@type": "NewsArticle", "mainEntityofPage": { "@type": "WebPage", "id": "{{Page URL}}" }, "headline": "{{UA-CJS-Schema-Heading}}", "description": "{{UA-CJS-Schema-Meta-Description}}", "image": "{{UA-CJS-BlogImageURL}}", "author": { "@type": "Organization", "name": "Association for Project Management" }, "publisher": { "@type": "Organization", "name": "Association for Project Management", "logo": { "@type": "ImageObject", "url": "https://www.apm.org.uk/" } }, "datePublished": "{{UA-CJS-Schema-Date Published-Edited}}", "dateModified": "{{UA-CJS-Schema-DatePublished-Edited}}" } </script>

I will add some debugging steps, but you don't really need them since you used a different service and it confirmed that the schema is there. But just in case.

Debugging steps:

  • Check that your trigger for the tag is something that happens rather sooner, like window load or history change.
  • Make sure that your tag fires, using preview mode in GTM.
  • Make sure that you can see your properly added to the DOM when you expect it.
  • Copy it, go to https://search.google.com/structured-data/testing-tool/u/0/
  • Choose the Code Snippet option and paste your snippet
  • Assess the errors and warnings
  • Fix the script in GTM accordingly
  • Enjoy errorless schema
  • Ask the SEO folks when you can remove this unrelated to analytics code from GTM.

Both the Rich Results Test tool and the Search Console Test render before testing, so there could still be an issue in how the json-ld is being added to the page.

The tools normally pinpoint to the place where a syntax error like this happens.

One possible issue is if the dynamic content (eg UA-CJS-Schema-Meta-Description) contains double quotes or a closing script tag. I'm not sure how GTM escapes this content but if it does not, they can cause invalid json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM