Access google analytics data with Django

2.4k Views Asked by At

I'm trying to build a super simple dashboard to show to users their Google Analytics data well formatted.

I'm using oAuth2Client and Django 1.10.4 and Python 3.5.

I've followed the example within the documentation and now I have a very simple app, the landing page will ask you to login, you click on a link to authorise, the Google page loads and asks you if you want to share your GA data and if you accept you are redirect to a page you can see only if you are logged in. All good so far.

However I can't manage to actually get users data, what's the best way to get for example the list of properties in a user's account or even better the number of page views a property had in the last week?

This is my code so far:

/pools/models.py

from django import http
from oauth2client.contrib.django_util import decorators
from django.views.generic import ListView
# from polls.models import GoogleAnalytic
from django.http import HttpResponse
# Google Analytics
from apiclient.discovery import build

# def index(request):
#     return http.HttpResponse("Hello and Welcome! </br> </br> Click <a href='/profile_enabled'> here</a> to login")

@decorators.oauth_required
def get_profile_required(request):
    resp, content = request.oauth.http.request(
        'https://www.googleapis.com/plus/v1/people/me')
    return http.HttpResponse(content)

@decorators.oauth_enabled
def get_profile_optional(request):
    if request.oauth.has_credentials():
        # this could be passed into a view
        # request.oauth.http is also initialized
        return http.HttpResponse('You are signed in.</br> </br>'+'User email: {}'.format(
            request.oauth.credentials.id_token['email']) + "</br></br>Click <a href='/ga'> here </a> to view your metrics")
    else:
        return http.HttpResponse(
            'Hello And Welcome!</br></br>'
            'You need to sign in to view your data. </br></br>' +
            'Here is an OAuth Authorize link:<a href="{}">Authorize</a>'
            .format(request.oauth.get_authorize_redirect()))

########## MY CODE! ###############

@decorators.oauth_required
def google_analytics(object):
    return HttpResponse('These are your results for last week:')

urls.py

from django.conf import urls
from polls import views
import oauth2client.contrib.django_util.site as django_util_site


urlpatterns = [
    urls.url(r'^$', views.get_profile_optional),
    urls.url(r'^profile_required$', views.get_profile_required),
    # urls.url(r'^profile_enabled$', views.get_profile_optional),
    urls.url(r'^oauth2/', urls.include(django_util_site.urls)),
    urls.url(r'^ga/$', views.google_analytics)
]

settings.py

  [...]


    GOOGLE_OAUTH2_CLIENT_ID = 'XXX.apps.googleusercontent.com'

    GOOGLE_OAUTH2_CLIENT_SECRET = 'XXX'

    GOOGLE_OAUTH2_SCOPES = ('email','https://www.googleapis.com/auth/analytics')

So my problem is I don't really understand where Django saves the token to access the data of that particular user, I know it works because it prints out the email address correctly etc, but I can't figure out what I should add to def google_analytics(object): to actually get specific Google API methods.

If anyone has experience on these kind of things I would really appreciate some help! Thanks!

1

There are 1 best solutions below

0
On

If you want to fetch Google Analytics configuration details e.g. Accounts, Web properties, Profiles, Filters, Goals, etc you can do that using Google Analytics Management API V3

If you want to fetch data of certain dimension and metrics from a Google Analytics view (aka profile), you can do that using either Core Reporting API V3 or Analytics Reporting API V4.

I think you will find the python api examples in their respective Guides.