【问题标题】:How to send google structured data via dom element?如何通过 dom 元素发送谷歌结构化数据?
【发布时间】:2019-04-16 07:07:02
【问题描述】:

我正在使用 dom 元素通过 javscript 设置 google 结构化数据

喜欢这个

    function setStructuredDataGeneral() {
        structuredData = '{"@context": "https://schema.org/", "@type": "Product", "name": "Ofertas", "image": "<?php echo $wLanding->getFSlideImage(); ?>", "offers": { "@type": "AggregateOffer", "lowPrice": "'+Math.min(...offerPrices)+'", "highPrice": "'+Math.max(...offerPrices)+'", "priceCurrency": "EUR"}}';

        var elementScriptStructuredData = document.getElementById("structuredDataGeneral");
        var text = document.createTextNode(structuredData);
        elementScriptStructuredData.appendChild(text); 
    }

然后我有一个 sctipt 标记去哪里

<script type="application/ld+json" id="structuredDataGeneral">
</script> 

当我检查它工作的网站时,生成预期的结果,问题是当我尝试通过谷歌结构化数据工具 (https://search.google.com/structured-data/testing-tool) 验证 URL 时,标签为空。

是因为这个工具没有加载javascript还是我做错了?

【问题讨论】:

  • 这可能与json/ld应该是从服务器返回的html的一部分,而不是动态添加的事实有关。 (除了goolge没有人跑你页面的js,连google都做的不太好。)
  • @felixmosh 所以你的意思是我也通过 javascript 生成标签,然后使用 document.write 或类似的东西将它添加到 html 中?
  • 不,如果您使用的是一些服务器端语言,例如 PHP、NodeJs 或类似语言,请在服务器返回的 html 上执行此操作。

标签: javascript dom json-ld google-rich-snippets


【解决方案1】:

结构化数据测试工具对 JavaScript 的支持非常有限。使用富结果测试工具或 Google Search Console 中的 URL Inspector 进行实时测试以呈现 JavaScript。

【讨论】:

  • 如果我使用检查器,我可以在 javascript 上看到我生成的结构化数据,我可以手动将我的代码放入谷歌搜索工具,我怎么知道谷歌是否会在测试时看到结构化数据工具不能正常工作?
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 2020-02-17
相关资源
最近更新 更多