grouping session counts for 2 dimensions

29 views
Skip to first unread message

Alistair Burrowes

unread,
Sep 22, 2015, 1:06:50 AM9/22/15
to Google Analytics Reporting API
Hi,

I currently have something like:

metric - ga:sessions

dimensions - ga:customCountry

which gives me:

Australia, 100
Brazil, 200

etc.

I want to modify this to now include totals broken down by deviceCategory as well:

Australia, MobileTotal=50, DesktopTotal=50, Total=100
Brazil, MobileTotal=150, DesktopTotal=50, Total=200

This is similar to the SQL grouping feature.

I can set dimensions to ga:CustomCountry,ga:deviceCategory however this gives me:

Australia, Mobile, 50
Australia, Desktop, 50

etc.

Is there a way to group these session totals into the one row? 

I kind of have it working in a custom report via the analytics web interface by adding multiple segements, however adding segments via the API just seems to filter the results and that is it.

Matt

unread,
Sep 25, 2015, 12:35:57 AM9/25/15
to Google Analytics Reporting API
You likely need to do the formatting on the client side.
If I were you I would read the results into a data structure and then loop through the data structure to construct the table.

Assume: 

rows = [['Austrialia', 'Mobile', '50'],

             ['Austrialia', 'Desktop', '50'],

             ['Brazil', 'Mobile', '23']]


report = {}

for country, category, value in rows:

  if country not in report:

    report[country] = {}

  report[country][category] = value

  report[country]['total'] = report[country].get('total', 0) + int(value)


for country in report:

  # You will probably want to order and format this better.

  print country + "," + ",".join([category+"="+str(report[country][category]) for category in report[country]])


-Matt
Reply all
Reply to author
Forward
0 new messages