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

如何在Nuxt应用所有页面的body标签内添加Google Tag Manager的noscript标签

方案1:针对Nuxt 3

两种实现方式任选其一即可:

  1. 直接在app.vue模板顶部插入代码
<template>
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-4BXKY65" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

该方式插入的noscript会自动渲染在所有页面body标签的最开头位置,符合GTM的接入要求。

  1. 在nuxt.config.ts中全局配置注入
export default defineNuxtConfig({
  app: {
    head: {
      body: [
        {
          tag: 'noscript',
          innerHTML: '<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-4BXKY65" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
          position: 'start'
        }
      ]
    }
  }
})

方案2:针对Nuxt 2

在nuxt.config.js中添加如下配置:

export default {
  head: {
    noscript: [
      {
        innerHTML: '<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-4BXKY65" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
        body: true
      }
    ],
    // 禁用HTML转义,避免iframe标签被解析为纯文本
    __dangerouslyDisableSanitizers: ['noscript']
  }
}

也可以直接在全局默认布局layouts/default.vue的模板最顶部插入noscript代码,所有使用该布局的页面都会自动加载。


原有方案报错原因

你之前将HTML格式的noscript代码放在.js文件中引入,浏览器会将HTML内容当作JavaScript语法解析,自然会抛出语法错误,这是使用方式错误导致的问题。

内容的提问来源于stack exchange,提问作者hendy0817

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 02:24:00