如何通过 Google 分析 API 获取特定页面的活跃用户?

How to get active users of a specific page by Google analytics API?

我使用 rt:activeUsers 指标来获取网站上的活跃用户以获取实时数据。

我想实时获取特定页面(pathurl)的活跃用户,而不是整个网站。有没有办法实现它?我经历了 API Explorer 但没有成功。

虽然实时 API 非常有限,但它允许您使用过滤器。过滤器是一种 where 子句。

实时文档也非常有限,但您可以在核心报告上查看 filters 的文档 API 它的工作原理相同

filters=ga:browser%3D~%5EFirefox

我认为您应该查看维度 rt:pagePath,它可能是您要查找的内容。

下面是我如何在 javascript.

中完成此操作的示例,供其他人查找此内容

我从样本开始:https://ga-dev-tools.appspot.com/embed-api/third-party-visualizations/

该示例使用嵌入 api 的扩展来获取活跃用户:active-users.js

我不知道从哪里获得 active-users.js 文件,所以我只是使用 Chrome 中的开发人员工具并以这种方式获取它。将其保存在本地并在我的页面中链接到它。如果有人知道获取该文件的官方位置,请在下面评论。谢谢

如果您查看该文件的源代码,我修改了 pollActiveUsers 函数以添加一个过滤器参数,如下所示:

pollActiveUsers_: function () {
                var t = this.get(),
                i = 1e3 * (t.pollingInterval || 5);
                if (isNaN(i) || i < 5e3)
                    throw new Error("Frequency must be 5 seconds or more.");
                this.polling_ = !0,
                gapi.client.analytics.data.realtime.get({
                    ids: t.ids,
                    metrics: "rt:activeUsers",
                    filters: t.filters
                }).then(function (t) {
                    var e = t.result,
                    s = e.totalResults ? +e.rows[0][0] : 0,
                    n = this.activeUsers;
                    this.emit("success", {
                        activeUsers: this.activeUsers
                    }),
                    s != n && (this.activeUsers = s, this.onChange_(s - n)),
                    1 == this.polling_ && (this.timeout_ = setTimeout(this.pollActiveUsers_.bind(this), i))
                }
                    .bind(this))
            },

现在在我的页面中javascript我可以这样称呼它:

var activeUsers = new gapi.analytics.ext.ActiveUsers({
                container: 'active-users-container',
                pollingInterval: 5,
                filters: "rt:pagePath==/somepath/somepage/",
                ids: "ga:<yourviewid>"
            });

希望对某人有所帮助。