推荐答案您可以使用自定义JavaScript变量存储值的数组,然后将这些数组的元素设置为JSON-LD中的值.You can use custom JavaScript variables to store arrays of values and then set elements of these arrays as values in JSON-LD.例如,要获取H1标签的文本,您可以使用以下函数创建自定义JavaScript变量:For example, to get the text of H1 tag you can create custom JavaScript variable with this function:function () {return document.querySelector('h1').innerText;}假设您将命名此变量h1.然后,您可以将此变量用作JSON-LD中的{{h1}}.Let's say you will name this variable h1. Then you can use this variable as {{h1}} in JSON-LD. JSON-LD代码可以存储在自定义HTML标记中,并填充上面提到的变量.JSON-LD code can be stored in custom HTML tag and filled with variables mentioned above.如果页面上有一个以上的H1,则可以使用此变量:If you have more then one H1 on the page you can use this variable:function () {return document.querySelectorAll('h1');}它将返回一个元素数组,您可以遍历该数组以获取innerText或只看每个元素,如:It will return an array of elements and you can loop through this array to get innerText or just look at each element like:{{h1}}[0].innerText让我在此博客文章示例中展示我的想法- https://netpeak.net/blog/modify-your-ppc-strategy-to-suit-voice-search/.您可以从页面中获取所有H2标头(或评论或其他内容),如下所示:Let me show my idea on this blog post example - https://netpeak.net/blog/modify-your-ppc-strategy-to-suit-voice-search/. You can get all H2 headers (or reviews or anything) from the page like this:var allh2 = document.querySelectorAll('h2');就GTM而言,它可以是自定义JavaScript变量,也可以只用JSON-LD构建一个自定义HTML标签.接下来,创建一个空数组来存储这些标头的内部文本,并将所有值作为对象以JSON-LD表示形式推送:In terms of GTM it could be custom JavaScript variable or you can just build one custom HTML tag with JSON-LD. Next you create empty array to store inner text of these headers and push all values as object in JSON-LD notation:var reviews = [];for(i = 0; i < allh2.length; i ++){ reviews.push({'review':allh2[i].innerText});}现在,您有了reviews变量,该变量可用作JSON-LD标记中"reviews"键的值.Now you have reviews variable that can be used as a value in JSON-LD markup for "reviews" key. 这篇关于如何使用JSON-LD和变量将Google跟踪代码管理器与多个架构产品评论结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 11:26