简体   繁体   English

Google Analytics API以可读格式输出数据

[英]Google Analytics API output data in a readable format

I'm using the Embed API in order to display Google Analytics data using server-side authentication. 我正在使用Embed API ,以便通过服务器端身份验证显示Google Analytics(分析)数据。 I've been able to display the graphs I needed but now I have a problem, I'm trying to display the following table used by Google Analytics: 我已经能够显示所需的图表,但是现在我遇到了问题,我正在尝试显示Google Analytics(分析)使用的下表:

在此处输入图片说明

I've been able to display the same data but they're not readable as the one displayed by Analytics, for example the Avg. 我已经能够显示相同的数据,但无法读取与Analytics(分析)显示的数据相同的数据,例如Avg。 Session Duration should be represented in seconds but what I display is: 165.28275862068966 会话持续时间应以秒为单位,但我显示的是:165.28275862068966

My question is, is there any way to request the correct data format to the Embed API or do I need to calculate them by myself? 我的问题是,有什么方法可以向Embed API请求正确的数据格式,还是我需要自己计算?

An example of the query I call is this: 我调用的查询示例如下:

var dataChart5 = new gapi.analytics.googleCharts.DataChart({
query: {
  'ids': 'ga:***', // My ID
  'start-date': '31daysAgo',
  'end-date': 'yesterday',
  'metrics': 'ga:users,ga:percentNewSessions,ga:sessions,ga:bounceRate,ga:avgSessionDuration,ga:pageviews,ga:pageviewsPerSession',
  'prettyPrint':'true',
},
chart: {
  'container': 'chart-5-container',
  'type': 'TABLE',
  'options': {
    'width': '100%',
    'title': 'test'
  }
}
  });

   dataChart5.execute();

Data is returned in an raw format by the Google Analytics API Google Analytics(分析)API以原始格式返回数据

Lets look at ga:avgsessionduration 让我们看看ga:avgsessionduration

The average duration of user sessions represented in total seconds. 用户会话的平均持续时间以总秒数表示。 Data type: TIME 资料类型:TIME

So the number you are seeing is 165.28275862068966 seconds. 因此,您看到的数字是165.28275862068966秒。 If you want to see it like its displayed on the website 如果您想像在网站上看到的那样观看它

00:00:00 00:00:00

You are going to have to format it yourself. 您将必须自行格式化。 The API returns its data in one format in the case of this one its total number of seconds. 如果以秒为单位,则API以一种格式返回其数据。

Here's an actual answer... 这是实际答案...

https://www.raymondcamden.com/2015/09/17/creating-a-custom-display-for-googles-analytics-embed-library https://www.raymondcamden.com/2015/09/17/creating-a-custom-display-for-googles-analytics-embed-library

Example Node code... 示例节点代码...

const moment = require( "moment" );

function formatTime(str) {
    var dur = moment.duration(parseInt(str,10), 'seconds');
    var minutes = dur.minutes();
    var seconds = dur.seconds();
    return minutes + " minutes and " + seconds + " seconds";
}

console.log( formatTime( "165.282" ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM