简体   繁体   English

使用 Google Tag Manager 动态生成 Schema/JSON-LD 标签,但不断收到以下错误:“无法解析的结构化数据”

[英]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.我想我必须用 JS 包装代码,但我在这方面不是很擅长,请感谢您的帮助。 Thanks very much for looking into this for me.非常感谢您为我调查此事。

The screenshot of the jsonld script jsonld脚本截图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 测试工具获取源代码而不呈现 DOM。 Google Bot will render it properly, so no issues there. Google Bot 将正确呈现它,因此没有问题。

You certainly don't need a JS wrapper, though you can use it.您当然不需要 JS 包装器,尽管您可以使用它。 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.其中 Data 是表示架构 json 的 JS 对象。

Your error is most likely due to Google Fetching the code rather than rendering the page and letting JS populate the DOM.您的错误很可能是由于 Google 获取代码而不是呈现页面并让 JS 填充 DOM。

Deployment of Schema with GTM, though convenient, leads to issues in Future.使用 GTM 部署模式虽然方便,但会导致 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.使用 GTM 中的预览模式确保您的代码触发。
  • Make sure that you can see your properly added to the DOM when you expect it.确保您可以看到您在预期时正确添加到 DOM 中。
  • Copy it, go to https://search.google.com/structured-data/testing-tool/u/0/复制它,转到https://search.google.com/structured-data/testing-tool/u/0/
  • Choose the Code Snippet option and paste your snippet选择 Code Snippet 选项并粘贴您的代码段
  • Assess the errors and warnings评估错误和警告
  • Fix the script in GTM accordingly相应地修复 GTM 中的脚本
  • Enjoy errorless schema享受无误的架构
  • Ask the SEO folks when you can remove this unrelated to analytics code from GTM.询问 SEO 人员何时可以从 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. Rich Results Test 工具和 Search Console Test 都会在测试之前呈现,因此如何将 json-ld 添加到页面中仍然可能存在问题。

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.一个可能的问题是动态内容(例如 UA-CJS-Schema-Meta-Description)是否包含双引号或结束脚本标签。 I'm not sure how GTM escapes this content but if it does not, they can cause invalid json.我不确定 GTM 如何转义此内容,但如果不转义,它们可能会导致无效的 json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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