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
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:
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.
You can obtain it from the Google Developers Console. Go to your project and then select APIs & Oauth credentials –> Oauth and click on Download JSON.
Next, we need to install the oaut2client python package. Activate your virtual environment (if you have one) and type:
$ pip install oauth2client
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).
Learn how to create one here, for Python 2.7 and for Python 3. It’s really useful, once you start using them you won’t stop!
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:
$ pip install python-gflags $ pip install -U google-api-python-client
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):
$ python manage.py startapp oauth2_authentication
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:
from django.db import models from django.contrib.auth.models import User from oauth2client.django_orm import FlowField, CredentialsField class FlowModel(models.Model): id = models.ForeignKey(User, primary_key=True) flow = FlowField() class CredentialsModel(models.Model): id = models.ForeignKey(User, primary_key=True) credential = CredentialsField()
Next, we’ll edit the urls of your app. First, open the main urls.py and add the following urlpattern:
url(r'^oauth2/', include('oauth2_authentication.urls', namespace="oauth2"))
And in the oauth2_authentication/urls.py write:
from django.conf.urls import patterns, url from . import views urlpatterns = patterns( '', url(r'^$', views.index, name='index'), url(r'oauth2callback', views.auth_return, name='return'), )
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:
import os import httplib2 from oauth2client import xsrfutil from oauth2client.client import flow_from_clientsecrets from oauth2client.django_orm import Storage from apiclient.discovery import build from django.contrib.auth.decorators import login_required from django.http import HttpResponseBadRequest from django.http import HttpResponseRedirect from django.shortcuts import render from django.conf import settings from django.contrib.auth import get_user_model from django.core.urlresolvers import reverse from django.contrib.sites.models import get_current_site from .models import CredentialsModel, FlowModel CLIENT_SECRETS = os.path.join( os.path.dirname(__file__), 'client_secrets.json') def get_accounts_ids(service): accounts = service.management().accounts().list().execute() ids = [] if accounts.get('items'): for account in accounts['items']: ids.append(account['id']) return ids @login_required def index(request): # use the first REDIRECT_URI if you are developing your app # locally, and the second in production # REDIRECT_URI = 'http://localhost:8000/oauth2/oauth2callback' REDIRECT_URI = "https://%s%s" % ( get_current_site(request).domain, reverse("oauth2:return")) FLOW = flow_from_clientsecrets( CLIENT_SECRETS, scope='https://www.googleapis.com/auth/analytics.readonly', redirect_uri=REDIRECT_URI ) user = request.user storage = Storage(CredentialsModel, 'id', user, 'credential') credential = storage.get() if credential is None or credential.invalid is True: FLOW.params['state'] = xsrfutil.generate_token( settings.SECRET_KEY, user) authorize_url = FLOW.step1_get_authorize_url() f = FlowModel(id=user, flow=FLOW) f.save() return HttpResponseRedirect(authorize_url) else: http = httplib2.Http() http = credential.authorize(http) service = build('analytics', 'v3', http=http) ids = get_account_ids(service) return render( request, 'oauth2_authentication/main.html', {'ids':ids}) @login_required def auth_return(request): user = request.user if not xsrfutil.validate_token( settings.SECRET_KEY, request.REQUEST['state'], user): return HttpResponseBadRequest() FLOW = FlowModel.objects.get(id=user).flow credential = FLOW.step2_exchange(request.REQUEST) storage = Storage(CredentialsModel, 'id', user, 'credential') storage.put(credential) return HttpResponseRedirect("/oauth2")
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:
Your credentials are up to date! :-) {% if ids %} The List of Ids of your Google Analytics accounts are: {{ids}} {% endif %}
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.
The post Use the Google Analytics API with Django appeared first on Marina Mele's site.
In this post you will learn to create a Python script that allows users to enter its Google Analytics account and get information from there.
In order to do that, we will create a Project in the Google Developers Console and authorize it to use the Analytics API.
Next, we will use the Oauth 2.0 protocol to allow users to connect to their Analytics account through our Project.
And finally, we will retrieve the number of sessions of our view, segmented by traffic source.
Let’s start!
Go to the Google Developers Console and login with your account.
Click on Create Project and write your Project name and choose (if you want) your project ID.
Next, on your new project menu, go to APIs & auth –> Credentials. Here, in the Oauth section, click on Create new Client ID.
In this case, as we are creating a script that will run on our computer, we will choose Installed application as the application type, and Other as the installed application type.
Finally, click on Create Client ID.
You will see, next to the OAuth section, the credentials for your project, which contain your Client ID, the Client Secret, and the redirect URIS. Click on Download JSON to download them, and save the file as client_secrets.json.
From here, go to APIs & auth –> Consent screen and personalize the message that your users will see when requesting access to their accounts.
Next, we need to activate the Goolge Analytics API in your Project. Go to APIs & auth –> APIs and look for the Analytics API. You just need to activate it by clicking at the OFF button on the right.
Ok! now that we have our Project created we can move on to our Python script!
In order to use the Analytics API with Python, we will use the Google API Python Client library. You can install it in your working environment using pip (how? learn to install Python, virtualenv and virtualenvwrapper to work with virtual environments).
$ pip install python-gflags $ pip install -U google-api-python-client
We also install the python-gflags library, which we will use latter in the code.
Next create the file analytics_service_object.py in your working directory (where client_secrets.json is located). This file will create an authorized Analytics Service object, used to interact with the user’s analytics accounts.
import httplib2 from apiclient.discovery import build from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage from oauth2client import tools import argparse CLIENT_SECRETS = 'client_secrets.json' # The Flow object to be used if we need to authenticate. FLOW = flow_from_clientsecrets( CLIENT_SECRETS, scope='https://www.googleapis.com/auth/analytics.readonly', message='%s is missing' % CLIENT_SECRETS ) # A file to store the access token TOKEN_FILE_NAME = 'credentials.dat' def prepare_credentials(): parser = argparse.ArgumentParser(parents=[tools.argparser]) flags = parser.parse_args() # Retrieve existing credendials storage = Storage(TOKEN_FILE_NAME) credentials = storage.get() # If no credentials exist, we create new ones if credentials is None or credentials.invalid: credentials = tools.run_flow(FLOW, storage, flags) return credentials def initialize_service(): # Creates an http object and authorize it using # the function prepare_creadentials() http = httplib2.Http() credentials = prepare_credentials() http = credentials.authorize(http) # Build the Analytics Service Object with the authorized http object return build('analytics', 'v3', http=http) if __name__ == '__main__': service = initialize_service()
In the previous script:
Now, when you type
$ python analytics_service_object.py
you will see, in a browser window, the consent screen you customized before. This means that your Project is asking your permission to access your Analytics account through the API. After clicking yes, your new credentials will be stored in TOKEN_FILE_NAME so that you won’t have to enter them again (except when the access_token expires).
Once we have an authorized Analytics service object, we can use it to retrieve all the data in the user’s analytics accounts.
For example, to get a list of all the existing accounts of the user, just type:
accounts = service.management().accounts().list().execute()
This will give you a dictionary containing the following keys:
As we will see, this is a common structure when getting data from analytics, even when we ask for properties or views instead of accounts (the returned object has the same keys).
Moreover, the items value is a list of accounts, each of which is in turn a dictionary with keys:
Therefore, you can get a list of your users accounts with:
def get_accounts_ids(service): accounts = service.management().accounts().list().execute() ids = [] if accounts.get('items'): for account in accounts['items']: ids.append(account['id']) return ids
You can also see the account ids in the Google Analytics web. You have to go to the Admin tab, and open the top-left drop down menu. There, your different accounts will be displayed, with their id on the right.
But as you may know, each Account can have multiple Properties, each of which has a different tracking code. To obtain a list of the Properties inside the Account with an id of account_id, you can use:
webproperties = service.management().webproperties().list( accountId=account_id).execute()
where webproperties is a dictionary with the same keys as accounts, but in which
Again, each web property is a dictionary that contains the keys:
and many more (you can print the webproperties object to see its keys).
You’ll see that the web property id is the tracking code of this property, which you can also obtain in the Google Analytics Admin tab.
But there is another level! Inside each Property there can be multiple views! You can obtain a list of views (or profiles) of each web property with:
profiles = service.management().profiles().list( accountId=firstAccountId, webPropertyId=firstWebpropertyId).execute()
The profiles dictionary contains the same keys as accounts and webproperties, but with
and each profile has:
Now that we know how to get information about our accounts, properties and views, let’s obtain the number of sessions of a view during a period of time.
Create the file get_sessions.py and write:
from analytics_service_object import initialize_service def get_sessions(service, profile_id, start_date, end_date): ids = "ga:" + profile_id metrics = "ga:sessions" data = service.data().ga().get( ids=ids, start_date=start_date, end_date=end_date, metrics=metrics ).execute() return data["totalsForAllResults"][metrics] if __name__ == '__main__': service = initialize_service() profile_id = "your_profile_id" print get_sessions(service, profile_id, "2014-09-01", "2014-09-30")
Note: you have to add your view id in “your_profile_id”, and then, run this script with:
$ python get_sessions.py
Check all the functionalities of the service.data().ga().get() method, and retrieve all the data you want form your view!
Obtaining the number of sessions for each traffic source (i.e. organic, referral, social, direct, email and other) is a little bit trickier. You have to work with filters in order to segment your data.
Here’s a little script that does this, thanks to Michael for the update
from analytics_service_object import initialize_service def get_source_group(service, profile_id, start_date, end_date): ids = "ga:" + profile_id metrics = "ga:sessions" dimensions = "ga:channelGrouping" data = service.data().ga().get( ids=ids, start_date=start_date, end_date=end_date, metrics=metrics, dimensions=dimensions).execute() return dict( data["rows"] + [["total", data["totalsForAllResults"][metrics]]]) if __name__ == '__main__': service = initialize_service() profile_id = "your_profile_id" start_date = "2014-09-01" end_date = "2014-09-30" data = get_source_group(service, profile_id, start_date, end_date) for key, value in data.iteritems(): print key, value
Again, add your view’s id in “your_profile_id”, and change the start_date and end_date to match the time interval you want.
After running this script, you’ll see the desired information in your terminal.
Another solution to get the number of sessions by traffic source, less optimized but instructive, is to use filter instead of dimensions:
from analytics_service_object import initialize_service not_source_filters = { "social": "ga:hasSocialSourceReferral==No", "organic": "ga:medium!=organic", "direct": "ga:source!=(direct),ga:medium!=(none);ga:medium!=(not set)", "email": "ga:medium!=email", "referral": "ga:medium!=referral,ga:hasSocialSourceReferral!=No" } source_filters = { "social": "ga:hasSocialSourceReferral==Yes", "organic": "ga:medium==organic", "direct": "ga:source==(direct);ga:medium==(none),ga:medium==(not set)", "email": "ga:medium==email", "referral": "ga:medium==referral;ga:hasSocialSourceReferral==No", "other": "%s;%s;%s;%s;%s" % ( not_source_filters["social"], not_source_filters["organic"], not_source_filters["direct"], not_source_filters["email"], not_source_filters["referral"]) } def get_source_sessions(service, profile_id, start_date, end_date, source): ids = "ga:" + profile_id metrics = "ga:sessions" filters = source_filters[source] data = service.data().ga().get( ids=ids, start_date=start_date, end_date=end_date, metrics=metrics, filters=filters).execute() return data["totalsForAllResults"][metrics] if __name__ == '__main__': service = initialize_service() profile_id = "your_profile_id" start_date = "2014-09-01" end_date = "2014-09-30" for source in ["social", "organic", "direct", "email", "referral", "other"]: print source, get_source_sessions( service, profile_id, start_date, end_date, source)
Again, add your view’s id in “your_profile_id”, and change the start_date and end_date to match the time interval you want.
After running this script, you’ll see the desired information in your terminal.
Some information you may find useful when working with filters:
That’s all for today!
Please, +1 if was useful and share it with your friends! Thaaanks!
The post Use the Google Analytics API with Python appeared first on Marina Mele's site.