There's a couple of things I want to mention before we start. Obviously, if you have zero coding/scripting experience, this tutorial is not for you. You're expected to have some coding knowledge as I will not be explaining every single line of code. If you're new to Python and would like to start learning I can recommend this excellent beginners class at Codecademy
This tutorial was developed for Python 2.7 and coded in PyCharm I expect this to work with other versions and you're free to use any editor you choose but there's no guarantees. Just sayin' ????
To get started using the Google Analytics Reporting API you'll first need to create a project in the Google API console. You can use this setup tool ??which guides you through the process. You'll need to enable the correct API (reporting API V4) and create your credentials.
On the credentials page ??click create credentials and select Service account key. Create a JSON key and download it to your computer. You've now successfully created a service account which can be used to authenticate with the Reporting API.
Lastly, log in to Google Analytics, select the account you plan to use for this tutorial and in the admin section open up User Management on account level. Now, open facebook city marketing passau your JSON key file you just downloaded in your favourite text editor and copy the client_email. Give this email address read permissions and you're all set up.
First we need to install some Python libraries: the Google API Python client to connect with the API, numpy to do some data tinkering and matplotlib to create a nice looking graph.
You can either use pip or setuptools to install the Google API Python client. For detailed installation instructions visit this page over at Google. numpy and matplotlib are both part of the SciPy stack.
The easiest way to install the SciPy stack is by downloading one of the scientific Python distributions. Visit to download and install? one of these distributions.?
Next we need to authorise ourselves and create a service object we can use to request Google Analytics data:
credentials = ServiceAccountCredentials.from_json_keyfile_name('keys/', '')
If you've ever worked with previous versions of the Reporting API you'll notice that with V4 creating a report works quite differently. We now need to create a ReportRequest object, whereas before we needed to create a query string.
Subscribe to our newsletter and??receive a monthly update??with new posts and industry news
The following code parses the response and is pulled straight from the Reporting API documentation. Go have a look if you want to learn more about the request and response format.
Now all that's left is to render the response as a nice graph with matplotlib. And if you're feeling inspired and want to try something different, is chock full of examples.
#reverse the order of the data to create a nicer looking graph val.reverse() cities.reverse() #create a horizontal bar chart (np.arange(len(cities)), val, align='center', alpha=0.4) plt.yticks(np.arange(len(cities)), cities) #add some context plt.xlabel('Visits') plt.title('Top 10 cities last 30 days') #render the damn thing! ()
MultiMinds is a leading Belgian digital analytics agency. With our expert team we help both local and international brands with their digital analytics strategy.
Google HQ in NY on the agenda this morning. Same typical Google "happiness" atmosphere as the offices in London & M'? 2d ago
Subscribe to our newsletter if you want to be kept up to speed on the latest news and upcoming workshops.
No comments:
Post a Comment