如何使用 DOM 元素变量在 GTM 中添加 AggregateRatingin?
How to add AggregateRatingin in GTM using DOM element Variables?
我用作自定义 HTML 标签的产品架构代码,用于在 GTM 中使用 DOM 变量实现产品架构..
<script>
var jsonData = {
"@context": "http://schema.org",
"@type": "Product",
"name": {{productName}},
"image": {{productImg}},
"url": {{Page URL}},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{avgRating}},
"reviewCount": {{ratingsCount}},
}
}
var script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(jsonData);
$("head").append(script);
</script>
如何在 GTM 中为 AggregateRating 变量(avgRating、ratingsCount)配置 DOM 元素变量。
这是标记
<div class="woocommerce-product-rating">
<div class="star-rating">
<span style="width:100%">
<strong class="rating">5.00</strong> out of <span>5</span> based on <span class="rating">1</span> customer rating </span>
</div>
<a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<span class="count">1</span> customer review)</a> </div>
您需要在 GTM 中创建两个变量
1) 转到 Variables->User-Defined Variables->New->Custom Javascript
2) 创建名称为 ProductAvgRating
和 JS 代码的变量:
function () {
try {
return parseFloat(document.querySelector('.woocommerce-product-rating strong.rating').innerText);
}
catch(e) {return 0;}
}
2) 创建名称为 ProductRatingsCount
和 JS 代码的变量:
function () {
try {
return parseInt(document.querySelector('.woocommerce-product-rating span.rating').innerText);
}
catch(e) {return 0;}
}
然后像这样更改您的 html 标签:
<script>
var jsonData = {
"@context": "http://schema.org",
"@type": "Product",
"name": {{productName}},
"image": {{productImg}},
"url": {{Page URL}},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{ProductAvgRating}},
"reviewCount": {{ProductRatingsCount}},
}
}
var script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(jsonData);
$("head").append(script);
</script>
P.S。你没有问 productName
和 productImg
变量,我猜你已经知道了
我用作自定义 HTML 标签的产品架构代码,用于在 GTM 中使用 DOM 变量实现产品架构..
<script>
var jsonData = {
"@context": "http://schema.org",
"@type": "Product",
"name": {{productName}},
"image": {{productImg}},
"url": {{Page URL}},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{avgRating}},
"reviewCount": {{ratingsCount}},
}
}
var script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(jsonData);
$("head").append(script);
</script>
如何在 GTM 中为 AggregateRating 变量(avgRating、ratingsCount)配置 DOM 元素变量。
这是标记
<div class="woocommerce-product-rating">
<div class="star-rating">
<span style="width:100%">
<strong class="rating">5.00</strong> out of <span>5</span> based on <span class="rating">1</span> customer rating </span>
</div>
<a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<span class="count">1</span> customer review)</a> </div>
您需要在 GTM 中创建两个变量
1) 转到 Variables->User-Defined Variables->New->Custom Javascript
2) 创建名称为 ProductAvgRating
和 JS 代码的变量:
function () {
try {
return parseFloat(document.querySelector('.woocommerce-product-rating strong.rating').innerText);
}
catch(e) {return 0;}
}
2) 创建名称为 ProductRatingsCount
和 JS 代码的变量:
function () {
try {
return parseInt(document.querySelector('.woocommerce-product-rating span.rating').innerText);
}
catch(e) {return 0;}
}
然后像这样更改您的 html 标签:
<script>
var jsonData = {
"@context": "http://schema.org",
"@type": "Product",
"name": {{productName}},
"image": {{productImg}},
"url": {{Page URL}},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{ProductAvgRating}},
"reviewCount": {{ProductRatingsCount}},
}
}
var script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(jsonData);
$("head").append(script);
</script>
P.S。你没有问 productName
和 productImg
变量,我猜你已经知道了