Use the Google Analytics API with Django

Google Analytics is a powerful tool that gives you useful insight about the traffic that arrives in your website. But together with Django, you will be able to retrieve data of your Analytics account and display it to the user, on demand.

In this post you’ll learn how to put together Django, the oauth2 protocol and the Google Analytics API.

Let’s Start! 🙂

First of all, you might want to check this post if you’re not familiar with the Google Analytics API with Python.

Moreover, it assumes that you are familiar with Python and pip, and that you know Django and have a working application. If you navigate into the Python and Django sections of this website you’ll learn more about those 😉

Create a Google Application

I assume you have a working Django application, and that you want to extract data from your Google Analytics account using an existing Project. If you didn’t create a Project in the Google Developers Console, check this post and create one. Then, come back 😉

But if you have a live Django project, then you need to create a Web application instead. You just need to select Web application as the Application type when you are creating a Client Id in the Credentials section:

Google Web Application

 

In Authorized Javascript origins insert your website domain, and in authorized redirect uris, insert your domain plus /oauth2/oauth2callback. This endpoint will be used by django to catch the oatuh2 response.

After that, you should have the Oauth credentials in a json format.

From where should I get the JSON Credential file?

Install oauth2 and Google Analytics

Next, we need to install the oaut2client python package. Activate your virtual environment (if you have one) and type:

This will install oauth2client and the following packages: httplib2 (an http library), pyasn1 and pyasn1-modules (to use the ASN.1 protocol), and rsa to use the RSA cryptosystem).

No virtual environment?

Don’t forget to add these packages into the requirements file of your virtual environment.

And also, we will need the API Python Client library:

Create a Django App

Next, we will create a Django app to manage the Authorization process. Depending on where you want to create the app’s folder, you will run a slightly different command (this one will create the folder in the current directory):

Take the client_secrets.json file and save it inside this app, and include this app into your settings.py file, in the INSTALLED_APPS.

Now that we have the app created, we are going to create two different models that will store the Flow and Credential objects.

Edit the models.py inside the oaut2_authentication app and write:

Next, we’ll edit the urls of your app. First, open the main urls.py and add the following urlpattern:

And in the oauth2_authentication/urls.py write:

This way, /oauth2 will start the authorization process, and /oauth2/oaut2callback will wait for the authorization response.

But we need to write our views.py first:

Note that you have to choose which REDIRECT_URI to use, depending on if you are developing your Django app locally or your app is live on a server.

Inside the FLOW object, we have used the readonly scope, but you could also choose:

https://www.googleapis.com/auth/analytics for writing permissions, and https://www.googleapis.com/auth/analytics.manage.users to view and manage permission of users in the Analytics accounts (more info).

And finally, write something in the oauth2_authentication/main.html template, like:

Now you just need to start the server and visit /oauth2 to start the authentication process. If everything worked correctly, you should see the list of ids of your Google Analytics account.

Note that once you have the Google Analytics service object you can retrieve all the data you want from your accounts. To see more examples, you can check this post: Google Analytics API with Python.

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)