Search code examples
javascriptgoogle-analyticsanalyticsgoogle-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. 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:

enter image description here

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();

Solution

  • 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.