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

3

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

我想要获取特定页面(path或者url)的实时活跃用户,而不是整个网站的活跃用户。有什么方法可以实现吗?我查看了API Explorer,但没有成功。


你尝试过为rt:pagePath添加过滤器吗? - Linda Lawton - DaImTo
谢谢@DaImTo,请将您的答案放在“答案”中以接受它。 - Alireza
很高兴你已经解决了它 :) - Linda Lawton - DaImTo
2个回答

3
如果有其他人需要这个功能,以下是我在javascript中实现的示例。
我从这个示例开始:https://ga-dev-tools.appspot.com/embed-api/third-party-visualizations/ 该示例使用嵌入式api的扩展来获取活跃用户:active-users.js
我不知道在哪里可以获取active-users.js文件,所以我只是使用Chrome中的开发者工具并以这种方式获取它。将其本地保存并在我的页面上链接到它。如果有人知道官方位置,请评论。谢谢。
如果您查看该文件的源代码,我修改了pollActiveUsers函数以添加filters参数,如下所示:
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>"
            });

希望这能帮到一些人。

1

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

实时文档也非常有限,但您可以查看核心报告API的filters文档,其工作方式相同。

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

我认为你应该查看维度rt:pagePath,这可能是你正在寻找的内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接