Javascript 在GTM中使用JS foreach在JSON ld模式上循环
我正在使用GoogleTagManager中的JSONLD为餐馆页面设置一个模式。菜单部分会定期更新,因此我们希望使用动态变量(使用数据层变量或javascript变量)在模式中循环该部分Javascript 在GTM中使用JS foreach在JSON ld模式上循环,javascript,google-tag-manager,json-ld,Javascript,Google Tag Manager,Json Ld,我正在使用GoogleTagManager中的JSONLD为餐馆页面设置一个模式。菜单部分会定期更新,因此我们希望使用动态变量(使用数据层变量或javascript变量)在模式中循环该部分 <script type="application/ld+json"> (function(){ var data = { "@context":"http://schema.org", "@
<script type="application/ld+json">
(function(){
var data = {
"@context":"http://schema.org",
"@type":"Restaurant",
"url":"https://www.example.com/",
"name":"Example",
"hasMenu":{
"@type":"Menu",
"name":"Menu",
"description":{{Menu Description}},
"hasMenuSection":[{ //loop through from here
"@type":"MenuSection",
"name":{{Menu Section Name}},
"description":{{Menu Section Description}},
"image":{{Menu Section Image}},
}]
}
}}
var script = document.createElement('script');
script.type = 'application/ld+json';
script.innerHTML = JSON.stringify( data );
document.getElementsByTagName('head')[0].appendChild(script);
})(document);
</script>
我在看这个问题
但答案并没有完全贯彻到整个实施过程中。
这里还有一个选项可以查看json数据的循环,但这是在twig中,而不是GTM中的实现。可能类似于这样:
“HasMenuAction”:
[…document.querySelectorAll(“#菜单节名称”)]
.map(节=>{
“@type”:“menusecoction”,
“名称”:section.querySelectorAll(“.section name”).textContent,
“description”:section.querySelectorAll(“.section description”).textContent,
“image”:section.querySelectorAll('.section-image').src,
})
我认为您为脚本设置的类型将停止它作为Javascript执行。
function () {return document.querySelectorAll('#menu-section-name');}