如何使用JSON-LD和变量将Google跟踪代码管理器与多个架构产品评论结合使用 [英] How to use Google Tag Manager with Multiple Schema Product reviews using JSON-LD and Variables

查看:107
本文介绍了如何使用JSON-LD和变量将Google跟踪代码管理器与多个架构产品评论结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用Google跟踪代码管理器将Schema JSON-LD产品评论添加到某些页面,但遇到问题,无法找到任何资源.

So I'm using Google Tag Manager to add Schema JSON-LD Product reviews to some pages and I'm stuck and unable to find any resources on the issue.

我遇到的问题是如何告诉GTM有多个元素,应该在JSON-LD中重复使用每个元素的唯一值.

The issue that I'm having is how can I tell GTM there are multiple elements and they should be repeated in JSON-LD each with their unique values.

我用来获取值的方法是使用CSS选择器,该选择器不是唯一的,因为在某些页面上它们可以出现20次,而在其他页面上则可以出现30次.变量是通过Google GTM获取的.

The method I'm using to get the values is using CSS selectors which are not unique because on some pages they can appear 20 times and on others 30 times. The variables are obtained by Google GTM.

在我添加的所有页面上使用Google跟踪代码管理器自定义HTML

<script>
(function() {

var data = { 

  // THIS APPEARS ONCE.      
  "@context": "http://schema.org",
  "@type": "Product",
  "name": "{{name}}",
  "aggregateRating":
    {
      "@type": "AggregateRating",
      "ratingValue": "{{rating}}",
      "reviewCount": "{{ratingCount}}"
    }

    // THIS BLOCK OF CODE NEEDS TO BE REPEATED EVERYTIME VARIBLE  {{customer}} IS FOUND
    "review": [
      {
        "@type": "Review",
        "author": "{{customer}}",
        "datePublished": "{{date}}",
        "name": "{{desc}}",
        "reviewBody": "{{say}}",
        "reviewRating": {
      "@type": "Rating",
      "ratingValue": "{{customValue}}"        
    }]
  }

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

可悲的是,经过测试,我无法获得JSON-LD的审阅部分以统一的方式重复.实际上,我对JSON的审查时间不能超过一个,而且我的代码和GTM设置显然存在问题.

Sadly, after testing, I'm unable to get the review part of the JSON-LD to repeat in a uniformed manner. In fact, I can't get more than one review period into the JSON and obviously something wrong with my code and GTM setup.

然后使用带有CSS选择器的DOM元素在Google TAG管理器中设置变量,如下所示:

The variables are then setup in Google TAG manager using DOM Element with CSS selector like so:

有时页面上会显示多个评论,因此我需要在JSON中输入多个条目,而我当前的代码最多只能输出一个.

Sometimes the are multiple reviews that appear on the page so I need multiple entries in the JSON, my current code only outputs a max of one.

问题,如果页面上存在多个产品评论,我如何让GTM在JSON-LD中添加另一个评论?

推荐答案

您可以使用自定义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

让我在此博客文章示例中展示我的想法-

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跟踪代码管理器与多个架构产品评论结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆