Reputation: 7139
I'm using the Embed API in order to display Google Analytics data using server-side authentication. 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:
I've been able to display the same data but they're not readable as the one displayed by Analytics, for example the Avg. Session Duration should be represented in seconds but what I display is: 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?
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();
Upvotes: 1
Views: 1587
Reputation: 1
Here's an actual answer...
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" ) );
Upvotes: 0
Reputation: 117126
Data is returned in an raw format by the Google Analytics API
Lets look at ga:avgsessionduration
The average duration of user sessions represented in total seconds. Data type: TIME
So the number you are seeing is 165.28275862068966 seconds. If you want to see it like its displayed on the website
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.
Upvotes: 1