Google Analytics and Python

I am new to Python and I am trying to write an extension for an application that imports GA information and parses it into MySQL. A rather small amount of information is presented on this topic. Google Docs only have examples in JS and Java ...

... I have come to the point that my user can authenticate to GA using SubAuth. This code is here:

import gdata.service
import gdata.analytics  
from django import http
from django import shortcuts
from django.shortcuts import render_to_response

def authorize(request):
    next = 'http://localhost:8000/authconfirm'
    scope = 'https://www.google.com/analytics/feeds'
    secure = False  # set secure=True to request secure AuthSub tokens
    session = False
    auth_sub_url = gdata.service.GenerateAuthSubRequestUrl(next, scope, secure=secure, session=session)
    return http.HttpResponseRedirect(auth_sub_url)

So, a step further goes according to the data. I found this library: (beware, the user interface is offensive) http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.analytics.html However, it was difficult for me to navigate. It seems that I should be gdata.analytics.AnalyticsDataEntry.getDataEntry (), but I'm not sure if he asks me to pass it.

. , Google, .

!

: , . ( ).... , : "str" "_BecomeChildElement" " , ? , . ?

def auth_confirm(request):
    gdata_service = gdata.service.GDataService('iSample_acctSample_v1.0')
    feedUri='https://www.google.com/analytics/feeds/accounts/default?max-results=50'
    # request feed
    feed = gdata.analytics.AnalyticsDataFeed(feedUri)
    print str(feed)
+5
3

. , Google Analytics, gdata.

+3

GA , 2009 python, python-googleanalytics Clint Ecker et al. .

, : http://github.com/clintecker/python-googleanalytics.

.

: -, API, :

[Credentials]
google_account_email = youraccount@gmail.com
google_account_password = yourpassword

'.pythongoogleanalytics' .

:

from googleanalytics import Connection
import datetime
connection = Connection()     # pass in id & pw as strings **if** not in config file
account = connection.get_account(<*your GA profile ID goes here*>)
start_date = datetime.date(2009, 12, 01)
end_data = datetime.date(2009, 12, 13)
# account object does the work, specify what data you want w/ 
# 'metrics' & 'dimensions'; see 'USAGE.md' file for examples
account.get_data(start_date=start_date, end_date=end_date, metrics=['visits'])

get_account python list ( , "account" ), .

+2

3 . client_secrets.json, analytics.dat google_auth.py.

Query.py :

class Query(object):
    def __init__(self, startdate, enddate, filter, metrics):
        self.startdate = startdate.strftime('%Y-%m-%d')
        self.enddate = enddate.strftime('%Y-%m-%d')
        self.filter = "ga:medium=" + filter  
        self.metrics = metrics

models.py: #

import google_auth
service = googleauth.initialize_service()
def total_visit(self):
    object = AnalyticsData.objects.get(utm_source=self.utm_source)
    trial = Query(object.date.startdate, object.date.enddate, object.utm_source, ga:sessions")
    result = service.data().ga().get(ids = 'ga:<your-profile-id>', start_date =   trial.startdate, end_date = trial.enddate, filters= trial.filter, metrics = trial.metrics).execute()
    total_visit = result.get('rows')
    <yr save command, ColumnName.object.create(data=total_visit) goes here>
0

All Articles