I'm not a real programmer, but I try. Of all the APIs I've struggled to program to, the Google Analytics API is one of the most straight forward. Being able to combine cURL and Bash to fetch reporting has not only saved me from performing the mind-numbing manual reporting tasks which drive me insane, but it has also allowed me to become much more granular with the reports I do pull. It is with great pleasure that I now share a few of the scripts that I run to pull down my Google Analytics data. First, it's important to understand a few of the basics. Steps Required for Google Analytics API Access --------------- 1. Authenticate using your account email address and password. 2. Capture and store the token returned after successful authentication. 3. Assemble the Google Analytics reporting URL and request a report. Google Analytics API Authentication - Step #1 ------------- All that is required to authenticate with the Analytics API is your email address and password. There's no need to have a special API key. The following cURL call will retrieve the token needed to request a report. curl https://www.google.com/accounts/ClientLogin -s -d Email=$USER_EMAIL -d Passwd=$USER_PASS -d accountType=GOOGLE -d source=curl-accountFeed-v1 -d service=analytics | grep "Auth=" | cut -d"=" -f2 This will leave you with a long encrypted string, which is the token you will need later on. Capture and Store the Google Analytics Token - Step #2 ----------------------- If you change the command above slightly, you can get that token stored in a variable which will make reusing the token later very simple. Simply wrap the above command like this: googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s -d Email=$USER_EMAIL -d Passwd=$USER_PASS -d accountType=GOOGLE -d source=curl-accountFeed-v1 -d service=analytics | grep "Auth=" | cut -d"=" -f2)" After running this command, your token will be stored in the variable `$googleAuth`. It can be printed to your terminal using this command if you'd like to verify that it was successful since there won't be any output. echo "$googleAuth" Requesting Your Google Analytics Reports via API - Step #3 ----------------- At this point it's important to understand what data you want and how you want the report formatted. This is of course completely up to you and should be based on what insights you hope to get from the data. If there are specific reports you would routinely view then I suggest you start there. Google Analytics reports consist of a date range, dimensions and the metrics to include. A full list of dimension and metric options can be viewed [here](http://code.google.com/apis/analytics/docs/gdata/dimsmets/dimsmets.html). The options presented on that page should look very similar to the options provided within the Google Analytics interface. I'll break the report request URL down, with one parameter per line, which should make each easier to understand. This is a typical report that I would run on my Analytics accounts. feedUri="https://www.google.com/analytics/feeds/data\ ?start-date=$START_DATE\ &end-date=$END_DATE\ &dimensions=ga:source,ga:medium\ &metrics=ga:visits,ga:newVisits,ga:pageviews,ga:bounces,ga:transactions,ga:transactionRevenue\ &sort=-ga:visits\ &max-results=10000\ &ids=ga:$PROFILE_ID\ &prettyprint=true" This same command can be written all on one line as well, like this: feedUri="https://www.google.com/analytics/feeds/data?start-date=$START_DATE&end-date=$END_DATE&dimensions=ga:source,ga:medium&metrics=ga:visits,ga:newVisits,ga:pageviews,ga:bounces,ga:transactions,ga:transactionRevenue&sort=-ga:visits&max-results=10000&ids=ga:$PROFILE_ID&prettyprint=true" I think each parameter name and value is pretty self explanatory. One thing that's worth mentioning is the data format. Make sure the dates are input in YYYY-MM-DD format. This will save the report request URL in the variable called `$feedUri`. All that is left is one more command, which will return your data. curl $feedUri -s --header "Authorization: GoogleLogin $googleAuth" This should output your data in tab-separated format by adding `>> report.txt` to the end of the command above, your report will be saved to the file called `report.txt`. The Final Google Analytics API Bash Script ----------- I've decided to just include a simple Bash script which combines each of the concepts covered above, and makes it all one command. Paste the following into a text file and name it `fetch.sh`. Replace the dummy email address and password with your own and then save the file. #!/bin/bash USER_EMAIL='[email protected]' #Insert your Google Account email address here USER_PASS='example_password' #Insert your password here if [[ "$1" = '' ]] || [[ "$2" = '' ]] || [[ "$3" = '' ]] then echo -e "Arguments required\n./fetch.sh " exit 1 fi PROFILE_ID="$1" START_DATE="$2" END_DATE="$3" googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \ -d Email=$USER_EMAIL \ -d Passwd=$USER_PASS \ -d accountType=GOOGLE \ -d source=curl-accountFeed-v1 \ -d service=analytics \ | grep "Auth=" | cut -d"=" -f2)" feedUri="https://www.google.com/analytics/feeds/data\ ?start-date=$START_DATE\ &end-date=$END_DATE\ &dimensions=ga:source,ga:medium\ &metrics=ga:visits,ga:newVisits,ga:pageviews,ga:bounces,ga:transactions,ga:transactionRevenue\ &sort=-ga:visits\ &max-results=10000\ &ids=ga:$PROFILE_ID\ &prettyprint=true" curl $feedUri -s --header "Authorization: GoogleLogin $googleAuth" You can then test it by running it like this: bash fetch.sh I do encourage you to investigate additional metrics on the page linked to above because there is a lot more insight to be gained than the one simple report I've provided. You may or may not have notices that there's a whole reporting section for AdWords! Yes, you can pull your AdWords keyword, ad copy, campaign and conversion data through the Google Analytics API. I plan to cover how to [automate Google AdWords reports](http://automateeverything.tumblr.com/post/20747660231/google-adwords-api-bash-scripts). Please let me know if you have any questions. I hope this has been helpful. Happy automation!