从'simple'dataLayer创建GTM变量没有问题,例如:
"transactionEntity": "ORDER",
"transactionId": "193552702",
但无法理解如何从对象中获取值。基本上我需要从这个dataLayer中获取值:
var dataLayer = [({
"productReviews": [{
"name": "Donna S. - Stockton, CA",
"datePublished": "2016-01-01",
"ratingValue": "5",
"reviewBody": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
}, {
"name": "Sharon N. - Belfast, UK",
"datePublished": "2016-02-01",
"ratingValue": "5",
"reviewBody": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam."
}, {
"name": "Elizabeth M. - Richmond, VA",
"datePublished": "2016-03-01",
"ratingValue": "5",
"reviewBody": "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos."
}],
})]
将name,datePublished,ratingValue,reviewBody值传递给以下GTM变量:
<script type="application/ld+json">
{
"review": [{
"@type": "Review",
"author": {
"@type": "Person",
"name": ""
},
"datePublished": "",
"reviewRating": {
"@type": "Rating",
"ratingValue": "",
"bestRating": "5",
"worstRating": "1"
},
"reviewBody": ""
}, {
"@type": "Review",
"author": {
"@type": "Person",
"name": ""
},
"datePublished": "",
"reviewRating": {
"@type": "Rating",
"ratingValue": "",
"bestRating": "5",
"worstRating": "1"
},
"reviewBody": ""
}, {
"@type": "Review",
"author": {
"@type": "Person",
"name": ""
},
"datePublished": "",
"reviewRating": {
"@type": "Rating",
"ratingValue": "",
"bestRating": "5",
"worstRating": "1"
},
"reviewBody": ""
}]
}
</script>
更新:根据vinoaj的回答,我做了以下事情:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"name": "{{dataLayerProductName}}",
"sku": "{{dataLayerProductId}}",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "{{dataLayerRatingValue}}",
"reviewCount": "{{dataLayerReviewCount}}"
},
"review": [
var productReviews = {{dataLayerProductReviews}};
for (i = 0, l = productReviews.length; i < l; i++){
var r = productReviews[i];
{
"@type": "Review",
"author": {
"@type": "Person",
"name": "r.name"
},
"datePublished": "r.datePublished",
"reviewRating": {
"@type": "Rating",
"ratingValue": "r.ratingValue",
"bestRating": "5",
"worstRating": "1"
},
"reviewBody": "r.reviewBody"
},
}]
}
</script>
但是这给出了错误“Syntax error: value, object or array expected。”
P.S。我知道更新问题不是正确的方法,但是没有办法以可读的方式在评论中插入代码。
答案 0 :(得分:0)
要处理传递给数据层的对象,请执行以下操作。
第1步
为密钥productReviews
创建数据层变量。
将变量命名为DLV - productReviews
或类似的简单但具有描述性的内容。
第2步
创建一个自定义HTML标记,用于对象执行某些操作。在下面的例子中,我只是输出每个评论的值。在您的情况下,您可以修改以下内容以生成JSON + LD对象。
<script>
var productReviews = {{DLV - productReviews}};
for (i = 0, l = productReviews.length; i < l; i++){
var r = productReviews[i];
console.log("GTM says: ", r.name, r.datePublished, r.ratingValue, r.reviewBody);
}
</script>
或尝试根据您的示例构建JSON-LD对象:
<script>
var reviewArr = [];
var productReviews = {{DLV - productReviews}};
for (i = 0, l = productReviews.length; i < l; i++){
var r = productReviews[i];
reviewArr.push({
"@type": "Review",
"author": {
"@type": "Person",
"name": r.name
},
"datePublished": r.datePublished,
"reviewRating": {
"@type": "Rating",
"ratingValue": r.ratingValue,
"bestRating": "5",
"worstRating": "1"
},
"reviewBody": r.reviewBody
})
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"name": "{{dataLayerProductName}}",
"sku": "{{dataLayerProductId}}",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "{{dataLayerRatingValue}}",
"reviewCount": "{{dataLayerReviewCount}}"
},
"review": reviewArr
}
</script>