感谢您的关注,我不是技术人员,所以请耐心等待。
我使用JavaScript在我的JSON-LD代码中应用动态变量以用于SEO(结构化数据)。我遇到的问题是,每当我尝试预览或提交标签时,都会收到此错误:
验证容器
容器有以下错误:
JavaScript编译器
错误产品 - Apple架构
第36行出错,第9行:解析错误。 '}'预计
这是我的代码:
<script>
(function(){
var data = {
"@context": "http://schema.org/",
"@type": "Product",
"name": {{SCH Product - Device_Name}} {{SCH Device Memory}},
"image": {{SCH Product - Device_Img}},
"description": {{SCHEMA - Page Description}},
"brand": {
"@type": "Thing",
"name": "Apple"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.4",
"ratingCount": "89"
},
"offers": {
"@type": "AggregateOffer",
"lowPrice": "100",
"highPrice": "420",
"priceCurrency": "USD",
"itemCondition": "http://schema.org/UsedCondition",
"availability": "http://schema.org/InStock",
"seller": {
"@type": "Organization",
"name": "Ciclii"
}
}
}
var script = document.creatingElement('script');
script.type = "apllication/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTag('head')[0].appendChild(script);
})(document)
</script>
我用这篇文章作为来源:
https://moz.com/blog/using-google-tag-manager-to-dynamically-generate-schema-org-json-ld-tags
请分享您的想法或任何建议,非常感谢! - 提前谢谢你。
答案 0 :(得分:1)
这一行的问题
"name": {{SCH Product - Device_Name}} {{SCH Device Memory}},
正确的方法是:
"name": "{{SCH Product - Device_Name}} {{SCH Device Memory}}",
你忘了一个;
工作代码是:
<script>
(function(){
var data = {
"@context": "http://schema.org/",
"@type": "Product",
"name": "{{SCH Product - Device_Name}} {{SCH Device Memory}}",
"image": {{SCH Product - Device_Img}},
"description": {{SCHEMA - Page Description}},
"brand": {
"@type": "Thing",
"name": "Apple"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.4",
"ratingCount": "89"
},
"offers": {
"@type": "AggregateOffer",
"lowPrice": "100",
"highPrice": "420",
"priceCurrency": "USD",
"itemCondition": "http://schema.org/UsedCondition",
"availability": "http://schema.org/InStock",
"seller": {
"@type": "Organization",
"name": "Ciclii"
}
}
};
var script = document.creatingElement('script');
script.type = "apllication/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTag('head')[0].appendChild(script);
})(document)
</script>
为什么会这样?
如果您只使用不带引号{{SCH Product - Device_Name}
的varriable,GTM会将其编译为"name": google_tag_manager["GTM-XXXXXX"].macro('gtmX') google_tag_manager["GTM-XXXXXX"].macro('gtmX')
之类的内容。而这个结构是无效的JS
但如果您将变量放在引号内,它将执行此变量并将呈现为"name": "string1 string2"