You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dataLayer push触发uncaught SyntaxError,新手求助错误成因

错误成因分析

你的代码存在两个明确的语法问题,这就是报错的根源:

  • 对象属性末尾多余逗号:price: 672,这一行最后多了个逗号,JavaScript中对象的最后一个属性后不能加尾随逗号,旧版JS引擎会直接抛出语法错误。
  • 不可见特殊空格字符:第一行window.dataLayer​ = ​window​.​dataLayer​ || ​​[];​里的空格是特殊Unicode空格(比如非断空格),这类字符无法被JS引擎识别,会触发语法解析错误。

修正后的代码:

<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
    'event': 'product_view',
    'ecommerce': {
        'product': {
            'sku': '',
            'name': 'product_name',
            'categories': 'category_name',
            'price': 672
        }
    }
});
</script>

内容的提问来源于stack exchange,提问作者Ac fresh 21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:47:07