使用 gtag.js 获取客户端 ID [英] Getting client ID with gtag.js

查看:23
本文介绍了使用 gtag.js 获取客户端 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google 几个月前发布了 gtag.js 作为新的使用 Google Analytics 进行跟踪的方式,据我所知最终取代了 analytics.js.gtag.js 是设置新 Google Analytics(分析)帐户时的默认设置,因此代码片段如下:

Google released gtag.js a couple of months ago as the new way of tracking with Google Analytics, eventually replacing analytics.js as far as I understood. gtag.js is the default when setting up a new Google Analytics account, so the code snippet went from this:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-12345678-1', 'auto');
  ga('send', 'pageview');
</script>

为此:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-123456789-1');
</script>

我需要做的是使用 gtag.js 获取客户端 ID.使用旧脚本,我可以执行以下操作.

What I need to do, is to get the client ID with gtag.js. With the old script, I could do as follows.

ga(function(tracker) {
  let clientId = tracker.get('clientId');
});

从表面上看,gtag.js 通过 Google Tag Manager 加载了相同的 analytics.js 脚本,因为 ga 变量确实可用.但是,有一些区别,因为在调用回调时 tracker 参数是 undefined因为 gtag.js 不使用跟踪器,所以这种方法 clear 行不通.

By the looks of it, gtag.js loads the same analytics.js script through Google Tag Manager, because the ga variable is indeed available. However, there is some difference because the tracker parameter is undefined when the callback is invoked, because gtag.js does not use trackers, so this approach clear won't work.

我浏览了 gtag.js 的文档,但我无法找到有关如何获取客户端 ID 的任何信息.Analytics.js 的文档 声明不要直接访问 cookie 获取客户端 ID,这是有道理的.但是有什么办法可以通过 gtag.js 通过 JavaScript API 获取它,还是我现在必须求助于读取 cookie?

I looked through the documentation for gtag.js, but I was not able to find any information on how to obtain the client ID. The documentation for analytics.js states not to access the cookie directly to obtain the client ID, which makes sense. But is there any way to get it through the JavaScript API with gtag.js, or do I have to resort to reading the cookie for now?

推荐答案

Whlie tracker id 属性不可用,至少目前它仍然是一个合适的 ga 跟踪器.所以下面的方法可以获得一个clientId:

Whlie tracker id property is unavailable it's still a proper ga tracker at least at the time being. so the following would work to get a clientId:

ga.getAll().forEach( (tracker) => {
  var id = tracker.get('clientId'); console.log(id)
})

使用 ga.getAll() 访问 ga 跟踪器后,您可以设置customTask",将 clientId 分配给您选择的自定义维度.查看 Simo 的指南 此处

After accessing the ga tracker with ga.getAll() you can set up the 'customTask' that would assign clientId to custom dimension of your choice. Look at Simo's guide here

这篇关于使用 gtag.js 获取客户端 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆