Google 跟踪代码管理器获取用户 ID 和点击时间

Google Tag Manager get user ID and time onClick


我在网站上有带 ID 的按钮。
目前我们使用跟踪代码管理器和 Analytics 跟踪按钮点击。
但我想知道,有没有办法保存有关用户ID和单击按钮时间的信息?
期待任何信息。
谢谢!

我猜是用户ID,你说的是你网站的内部ID。首先,您需要找到一种方法如何在您的系统中接收用户 ID。它可以有多种方式。

例如,假设您的网站隐藏了用户 ID 为 id="user-id" 的输入,并且您想跟踪按钮 "Track this button"。基本 html 代码,如果您的页面是:

<input type="hidden" id="user-id" value="unqieuserid123"/>
<button id="button-id">Track this button</button>
  1. 您需要创建用户定义变量(变量->用户定义变量->新建)。选择 "Javascript" 并输入名称 "UserId" 和此代码:

    function () {
        return document.getElementById("user-id") != null ? 
                   document.getElementById("user-id").value : ""
    }
    

    最后应该是这样的:

  2. 创建用于接收用户时间的变量(它将写入用户本地时间),名称为 "UserTime",代码为 javascript:

        function() {
            // Get local time as ISO string with offset at the end
            var now = new Date();
            var tzo = -now.getTimezoneOffset();
            var dif = tzo >= 0 ? '+' : '-';
            var pad = function(num) {
                var norm = Math.abs(Math.floor(num));
                return (norm < 10 ? '0' : '') + norm;
            };
            return now.getFullYear() 
                + '-' + pad(now.getMonth()+1)
                + '-' + pad(now.getDate())
                + 'T' + pad(now.getHours())
                + ':' + pad(now.getMinutes()) 
                + ':' + pad(now.getSeconds())
                + '.' + pad(now.getMilliseconds())
                + dif + pad(tzo / 60) 
                + ':' + pad(tzo % 60);
        }
    

    最后应该是这样的:

  3. 启用内置变量 Click Element 如果尚未启用。

  4. 为您的按钮创建新触发器。在我的例子中是这样的:

  5. 创建新标签用于跟踪和向 GA 发送数据。在这里,您可以使用在步骤 #1 和 #2 中创建的变量。喜欢 {{UserId}}{{UserTime}}。在我的例子中是这样的:

  6. 在 Google 分析结束时,您将收到带有此标签的事件:

    'user id: unqieuserid123; user time: 2017-05-31T12:20:19.06+04:00'

我通过以下方式解决了这个问题:
- 在 Google 跟踪用户 ID 和日期和时间的跟踪代码管理器 (GTM) 中创建了变量。
我已经有了跟踪网站上按钮点击的标签,所以我在标签类别中添加了这些变量("Label" 和 "Value") Click event
每次用户单击按钮时,这些变量都会跟踪有关 ID 和日期和时间的信息。