【问题标题】:How to check if GA4 or Universal Analytics is installed?如何检查是否安装了 GA4 或 Universal Analytics?
【发布时间】:2023-02-10 20:55:41
【问题描述】:

我正在尝试在使用 Javascript 的网站上检测它们是否安装了新的 GA4 或旧的 Universal Analytics。 对于 UA 检测,我可以使用 typeof(ga) !== "undefined",但不确定如何为 GA4 执行此操作。

根据资料here:好像也在用?我们可以在客户端使用的任何其他区别?

【问题讨论】:

    标签: javascript analytics


    【解决方案1】:

    为什么, 我们过去常常为 GA4 这样做:

    if(typeof gtag === 'function' ){
            gtag('event', 'myeventname',{
                'event_category': 'myeventcategory'
            });
    }
    

    请注意,这仅在您直接在页面中安装了 GA4 跟踪代码时才有效。如果您使用谷歌标签管理器,这将不起作用。

    【讨论】:

      【解决方案2】:

      要在网站上检测 Google Analytics 4 (GA4),您可以检查是否存在 gtag 函数,因为 GA4 使用 gtag.js 库。这是一个例子:

      if (typeof gtag === "function") {
        console.log("Google Analytics 4 is installed on this website.");
      } else {
        console.log("Google Analytics 4 is not installed on this website.");
      }
      

      请注意,如果 GA4 和 Universal Analytics (UA) 安装在同一网站上,您将需要使用 gtag 函数将数据发送到 GA4,并使用 ga 函数将数据发送到 UA。在这种情况下,您还需要通过指定正确的跟踪 ID 来确保将数据发送到正确的媒体资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 2014-04-19
        • 2011-10-06
        • 2017-11-14
        相关资源
        最近更新 更多