gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:43:01 25 4
gpt4 key购买 nike

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

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

最佳答案

对于其他正在寻找此内容的人,下面是我如何在 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>"
});

希望对某人有帮助。

关于real-time - 如何通过Google Analytics API获取特定页面的活跃用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35839853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com