Search code examples
javagoogle-analyticsgoogle-analytics-api

How to sort month (ie. September 2015, to January 2016) using Get query of Core Reporting API for analytics?


I am getting google analytics data using core reporting API using below code.

Get apiQuery = analytics.data().ga().get("ga:[profileId]", "2015-09-01", "2016-01-27", "ga:users");
apiQuery.setDimensions("ga:month");
apiQuery.execute();

I am getting corresponding GaData results as below with 01 (January 2016) as the first returned month when it should be 09 (September 2015). What is the easiest way to get a result with already sorted months 09 > 10 > 11 > 12 > 01 using the Get query method?

First Profile Id: 86105992
Data Table: ga:month(STRING)                ga:users(INTEGER)               
Total Results: 01                              Total Results: 187                             
Total Results: 09                              Total Results: 74                              
Total Results: 10                              Total Results: 37                              
Total Results: 11                              Total Results: 283                             
Total Results: 12                              Total Results: 228  

Solution

  • I don't think you can do a proper numeric sort by month, but a workaround would be to select ga:nthMonth as first dimension; this will give you a numeric index (first month in the selected timeframe is "0", second ist "1" etc.) to sort by, so you would get the proper sequence.