GTM JavaScript 编译器错误

GTM JavaScript Compiler Error

感谢您的关注,我不是技术人员,所以请耐心等待。

我使用 JavaScript 在我的 JSON-LD 代码中应用动态变量以用于 SEO(结构化数据)目的。我遇到的问题是,每次我尝试预览或提交标签时,都会收到此错误:

Validate Container

The container has the following errors:

JavaScript Compiler

Error Product - Apple Schema
Error at line 36, character 9: Parse error. '}' expected

这是我的代码:

    <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

请分享您的想法或任何建议,我们将不胜感激! - 提前谢谢你。

这一行有问题

"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},GTM 会将其编译成类似的东西 "name": google_tag_manager["GTM-XXXXXX"].macro('gtmX') google_tag_manager["GTM-XXXXXX"].macro('gtmX')。而这个构造是无效的 JS

但是如果你将变量放在引号内,它将执行这个变量并呈现为 "name": "string1 string2"