binn1e / google-analytics-data-grabber

A quick and simple Python data grabber for Google Analytics API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#google analytics data grabber

A Python script which grabs data from Google Analytics API and shape it into nice and ready to use CSV files.

Script takes in parameters start date, end date, metrics, dimensions, and max results to be returned.

##install

I adapted main program hello_analytics_api_v3.py at https://developers.google.com/analytics/solutions/articles/hello-analytics-api.

To get it to work, you need to setup its dependencies first. From the same page, you need to get client_secrets.json – to store your authorization credentials — and hello_analytics_api_v3_auth.py – to store common authorization routines.

Just follow Google super clear instructions to edit these two files properly so everything can work properly.

##usage

Run without any parameters, script returns sessions by channels during past year, in the form of a CSV file called sessions by channelGrouping.csv. By default, it returns a maximum of 1000 rows.

Any file generated by the script will be named following pattern metric[,metric...] by dimension[ and dimension...].csv — that way, you don't have to scratch your head about what's inside them.

Here's a few examples to run the script so it returns whatever you need for your reporting:

python ga_api.py -d ga:city -r 5000
Returns sessions by cities. Returns a maximum of 5000 results. Includes data covering past year.

python ga_api.py -s 2014-05-01 -e 2014-11-30 -m ga:users ga:sessions -d ga:month ga:year ga:socialNetwork
Returns users and sessions by month and year and social network. Returns a maximum of 1000 results. Includes data covering period from May 1st to November 30th.

python ga_api.py -s 2014-10-01 -e yesterday -m ga:users ga:sessions -d ga:date ga:mobileDeviceModel
Returns users and sessions by date and mobile device model. Returns a maximum of 1000 results. Includes data covering period from October 1st to yesterday.

python ga_api.py -s yesterday -e today -m ga:sessions -d ga:channelGrouping
Returns sessions by channel. Returns a maximum of 1000 results. Includes data covering period from yesterday to today.

For complete Google Analytics metrics and dimensions listing see refereance at: https://developers.google.com/analytics/devguides/reporting/core/dimsmets

##help

python ga_api.py -help
usage: ga_api [-h] [-s pattern [pattern ...]] [-e pattern [pattern ...]]
              [-m pattern [pattern ...]] [-d pattern [pattern ...]]

Grab some Google Analytics data.

optional arguments:
  -h, --help            show this help message and exit
  -s pattern [pattern ...]
                        Include data from given date [Format: [0-9]{4}-[0-9]{2
                        }-[0-9]{2}|today|yesterday|[0-9]+(daysAgo)]
  -e pattern [pattern ...]
                        Include data until given date [Format: [0-9]{4}-[0-9]{
                        2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo)]
  -m pattern [pattern ...]
                        Desired Google Analytics metrics [Format: ga:metric
                        [ga:metric ...] — please see reference at https://de
                        velopers.google.com/analytics/devguides/reporting/core
                        /dimsmets
  -d pattern [pattern ...]
                        Desired Google Analytics dimensions [Format:
                        ga:dimension [ga:dimension ...] — please see
                        reference at https://developers.google.com/analytics/d
                        evguides/reporting/core/dimsmets

##notes

I tried to kill a few known pains here, please feel free to kill even more and adapt this to your needs.

About

A quick and simple Python data grabber for Google Analytics API


Languages

Language:Python 100.0%