我在通过Google Analytics(分析API(仅显示单个数据字段时遇到困难。
例如,我昨天如何显示用户数量?或过去一周的用户数量?
我假设" gapi.analytics.report.data"是正确的构造函数。我已经尝试在Google的内置组件参考中遵循"数据"代码,但前端没有显示任何内容。
对于" datachart"代码,有一个"容器"选项,该选项引用了DOM中的HTML元素,可以正确显示图表。
"数据"中缺少此"容器"选项 - 因此如何显示单个数据字段?
编辑:这是我正在使用的代码。
您可以在这里看到我的代码的粘贴:https://pastebin.com/m6u0bd8b
此处还有一个现场登台版本:http://madebychris.ca/dashboard2.html
前四个 section id
s都正确显示,但最终<p class ="report">
如果要访问原始数据编号,则可以访问响应对象:
// Set up the request
var report = new gapi.analytics.report.Data({
query: {
ids: 'ga:XXXX',
metrics: 'ga:sessions',
dimensions: 'ga:city'
}
});
// Define what to do with the response data.
report.on('success', function(response) {
console.log(response);
});
// Run the report
report.execute();
例如,指标的总数为 response.totalsForAllResults
,每个维度的行保存在 response.rows
您需要选择要使用report.on("success", function(response){
功能上的变量来完成的操作。例如:
report.on('success', function(response) {
$("body > main > div > section:nth-child(4) > h2")[0].innerText =
response.totalsForAllResults["ga:sessions"]
});
report.execute();
注意:您应该能够在嵌入式API演示上进行测试。用控制台复制和粘贴初始代码。log应该有助于显示正在发生的事情。