发布于: -/最后更新: -/7 分钟/

GTM 与 GA4 的正确食用方法

摘要

搭建网站数据分析体系需要使用Google Tag Manager(GTM)和Google Analytics 4(GA4)。首先,创建GTM容器并安装到网站中,集中管理所有追踪脚本。然后,在GA4中创建数据流,获取Measurement ID,并在GTM中添加GA4 Configuration标签。配置事件追踪,包括按钮点击、表单提交等用户行为,并将这些行为发送到GA4进行分析。同时,配置广告转化,包括Meta Pixel和Google Ads,确保广告平台能准确接收到来自网站的行为数据。最后,实现Cookie同意管理,使用vanilla-cookieconsent库创建Cookie同意弹窗,并与GTM的Consent Mode联动,确保所有标签都遵循用户的Cookie选择。通过这些步骤,构建一个数据结构清晰、事件追踪规范、广告转化准确、隐私合规的网站数据分析体系。

在搭建网站数据分析体系时,Google Tag Manager(GTM)与 Google Analytics 4(GA4)几乎是必备组合。它们能帮助你更灵活地管理追踪代码、监控用户行为,并为后续的广告投放(如 Meta Pixel、Google Ads)提供可靠的数据基础。

这篇文章将带你从 GTM 容器创建 → GA4 数据流配置 → 事件追踪 → 广告转化 → Cookie 同意管理 一步步走完整流程,让你的网站数据收集既规范又可扩展。

创建 GTM 容器

首先,在 Google Tag Manager 中创建一个新的容器,并将其安装到你的网站中。完成后,你就可以通过 GTM 来集中管理所有追踪脚本,而无需频繁修改代码。

根据指示安装GTM脚本
点击查看大图
根据指示安装GTM脚本

配置 GA4 数据流

在 GA4 中创建一个新的数据流,并获取 Measurement ID(形如 G-XXXXXXX)。
接着,在 GTM 中添加 GA4 Configuration 标签,并启用:

  • Conversion Linker(转化链接器)
    勾选 Enable linking on all page URLs
    这一步能确保 gclid 等广告参数在跳转过程中被正确记录。

可通过设置全局变量来插入measurement id
点击查看大图
可通过设置全局变量来插入measurement id

设置事件追踪(如按钮点击、表单提交)

你可以在 GTM 中通过触发器(Triggers)来监听用户行为,例如:

  • 链接点击

  • 表单提交

  • 页面滚动

  • 自定义事件

然后将这些行为发送到 GA4,作为事件(Events)进行分析。

自定义事件
点击查看大图
自定义事件

配置广告转化(Meta Pixel / Google Ads)

如果你需要广告投放,那么你还需要在 GTM 中配置:

Meta Pixel(Facebook Pixel)

  1. 首先注册一个Business Suite账号

  2. 在Event Manager里新建一个dataset,获取id

  3. 回到GTM新建一个Tag,去社区模板里寻找一个叫做Facebook Pixel的tag,作者是facebookarchive,填入id,触发选择initialzation,这个也就是相当于装了pixel的js脚本Google Ads 转化

    社区Pixel插件
    点击查看大图
    社区Pixel插件
  4. 设置 PageView、ViewContent、AddToCart 等事件

    配置事件
    点击查看大图
    配置事件
  1. 前往GoalsSummary,然后Create Conver Actions

  2. 选择Conversion on a website,输入你的网址选择Google Tag

  3. 选择好转化类型后,然后一直下一步找到use Google Tag Manager,主要的目的是为了找到

    1. Conversion ID

    2. Conversion Label

  4. 回到GTM,新建一个Google Ads Conversion Tracking标签,填入相关信息即可追踪转化事件

配置Conversion ID
点击查看大图
配置Conversion ID

这样,你的广告平台就能准确接收到来自网站的行为数据。

为了满足隐私法规要求,你需要在网站上加入 Cookie 同意弹窗,并与 GTM 的 Consent Mode(同意模式)联动。

你可以使用 vanilla-cookieconsent 库来快速实现:

TypeScript
import * as CookieConsent from "vanilla-cookieconsent";

// 默认信息都存在cc_cookie里
export function CookieBanner({
  language = "en",
  privacyPolicyUrl = "/privacy-policy",
}: {
  language?: string;
  privacyPolicyUrl?: string;
}) {
  useEffect(() => {
    CookieConsent.setLanguage(language);
    CookieConsent.run({
      onFirstConsent: initializeConsent,
      onChange: updateConsent,
      language: getLanguageConfig({ language, privacyPolicyUrl }),
      categories: {
        necessary: {
          readOnly: true,
        },
        functionality: {},
        analytics: {},
        marketing: {},
      },
    });
  }, [language, privacyPolicyUrl]);
    
  return null;
}

const getLanguageConfig = ({
  language,
  privacyPolicyUrl,
}: {
  language: string;
  privacyPolicyUrl: string;
}) => {
  return {
    default: language,
    translations: {
      en: {
        consentModal: {
          title: "We Value Your Privacy",
          description: `We use cookies to enhance your browsing experience, personalize ads or content, and analyze our traffic to improve user experience on our website. By clicking "Accept All", you consent to our use of cookies. For more info, see our <a href="${privacyPolicyUrl}">Privacy Policy</a>.`,
          acceptAllBtn: "Accept All",
          acceptNecessaryBtn: "Reject All",
          showPreferencesBtn: "Customize Settings",
        },
        preferencesModal: {
          title: "Privacy Preference Center",
          acceptAllBtn: "Accept All Cookies",
          acceptNecessaryBtn: "Reject All",
          savePreferencesBtn: "Save Preferences",
          closeIconLabel: "Close modal",
          serviceCounterLabel: "Service|Services",
          sections: [
            {
              title: "Cookie Usage",
              description:
                "When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is used to make the site work as you expect it to. The information does not directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change your default settings.",
            },
            {
              title:
                'Strictly Necessary Cookies <span class="pm__badge">Always Active</span>',
              description:
                "These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences or filling in forms.",
              linkedCategory: "necessary",
            },
            {
              title: "Analytics Cookies",
              description:
                "These cookies allow us to track analytics by counting visits and traffic sources so we can measure and improve the performance of our website. They may be set by us or by third party providers to help us to know which pages are the most and least popular and see how visitors move around the website.",
              linkedCategory: "analytics",
            },
            {
              title: "Functionality Cookies",
              description:
                "These cookies enable the website to provide enhanced functionality and personalization based on your interaction with the website. They may be set by us or by third party providers whose services we have added to our pages.",
              linkedCategory: "functionality",
            },
            {
              title: "Advertisement Cookies",
              description:
                "These cookies may be set through our site by our advertising partners. They may be used by those partners to build a profile of your interests and show you relevant advertisements on other websites.",
              linkedCategory: "marketing",
            },
          ],
        },
      },
      zh: {
        consentModal: {
          title: "我们重视您的隐私",
          description: `我们使用 cookie 来增强您的浏览体验,提供个性化广告或内容,并分析我们的流量以改善网站用户体验。点击“接受所有”即表示您同意我们使用 cookie。有关更多信息,请参阅我们的<a href="${privacyPolicyUrl}">隐私政策</a>。`,
          acceptAllBtn: "接受所有",
          acceptNecessaryBtn: "拒绝所有",
          showPreferencesBtn: "自定义设置",
        },
        preferencesModal: {
          title: "隐私偏好中心",
          acceptAllBtn: "接受所有 cookie",
          acceptNecessaryBtn: "拒绝所有",
          savePreferencesBtn: "保存偏好",
          closeIconLabel: "关闭模态窗口",
          serviceCounterLabel: "服务",
          sections: [
            {
              title: "Cookie 使用",
              description:
                "当您访问任何网站时,它可能会在您的浏览器中存储或检索信息,主要以 cookie 的形式。这些信息可能是关于您、您的偏好或您的设备,并用于使网站按您的期望工作。这些信息并不直接识别您,但它可以为您提供更个性化的 Web 体验。因为我们尊重您的隐私权,您可以选择不允许某些类型的 cookie。单击不同的类别标题以了解更多信息并更改您的默认设置。",
            },
            {
              title:
                '严格必要的 Cookie <span class="pm__badge">始终活动</span>',
              description:
                "这些 cookie 对于网站的功能是必不可少的,不能在我们的系统中关闭。它们通常只是对您的操作做出的响应,这些操作相当于请求服务,例如设置您的隐私偏好或填写表单。",
              linkedCategory: "necessary",
            },
            {
              title: "分析 Cookie",
              description:
                "这些 cookie 允许我们通过计算访问量和流量来源来跟踪分析,以便我们可以衡量和改善我们网站的性能。它们可能是由我们设置的,也可能是由第三方提供商设置的,以帮助我们了解哪些页面最受欢迎,哪些页面最不受欢迎,并查看访问者在网站上的移动情况。",
              linkedCategory: "analytics",
            },
            {
              title: "功能性 Cookie",
              description:
                "这些 cookie 使网站能够根据您与网站的交互提供增强功能和个性化。它们可能是由我们设置的,也可能是由我们添加到我们页面的第三方提供商设置的。",
              linkedCategory: "functionality",
            },
            {
              title: "广告 Cookie",
              description:
                "这些 cookie 可能会通过我们的网站由我们的广告合作伙伴设置。它们可能会被这些合作伙伴用来建立您的兴趣概要,并在其他网站上向您显示相关广告。",
              linkedCategory: "marketing",
            },
          ],
        },
      },
    },
  };
};

同时,你需要在 GTM 中启用 Consent Overview(同意概览),并确保所有标签都遵循用户的 Cookie 选择。

总结

通过以上步骤,你就能构建一个:

  • 数据结构清晰

  • 事件追踪规范

  • 广告转化准确

  • 隐私合规

的网站数据分析体系。

正文结束