Permalink
Browse files

Updated to work with GA API v2.4

  • Loading branch information...
1 parent faea4b9 commit 6be9b5ff8d6f353154ac556e16eafca130ab3d3e @jsma committed Jun 20, 2012
Showing with 26 additions and 17 deletions.
  1. +3 −2 src/googleanalytics/account.py
  2. +20 −12 src/googleanalytics/connection.py
  3. +3 −3 src/googleanalytics/data.py
@@ -127,7 +127,8 @@ def get_data(self, start_date, end_date, metrics, dimensions=[], sort=[], filter
Number of results to return.
"""
- path = '/analytics/feeds/data'
+ base_url = 'https://www.googleapis.com'
+ path = '/analytics/v2.4/data'
if start_date > end_date:
raise GoogleAnalyticsClientError('Date orders are reversed')
@@ -164,7 +165,7 @@ def get_data(self, start_date, end_date, metrics, dimensions=[], sort=[], filter
data['filters'] = filter_string
data = urllib.urlencode(data)
- response = self.connection.make_request('GET', path=path, data=data)
+ response = self.connection.make_request('GET', base_url, path=path, data=data)
raw_xml = response.read()
processed_data = DataSet(raw_xml)
return processed_data
@@ -12,32 +12,38 @@
TIMEOUT = 10
class GAConnection:
- default_host = 'https://www.google.com'
- user_agent = 'python-gapi-1.0'
+ user_agent = 'python-gapi-v2'
auth_token = None
def __init__(self, google_email=None, google_password=None):
authtoken_pat = re.compile(r"Auth=(.*)")
+ base_url = 'https://www.google.com'
path = '/accounts/ClientLogin'
-
if google_email == None or google_password == None:
google_email, google_password = config.get_google_credentials()
- data = "accountType=GOOGLE&Email=%s&Passwd=%s&service=analytics&source=%s"
- data = data % (google_email, google_password, self.user_agent)
+ data = {
+ 'accountType': 'GOOGLE',
+ 'Email': google_email,
+ 'Passwd': google_password,
+ 'service': 'analytics',
+ 'source': self.user_agent
+ }
if DEBUG:
print "Authenticating with %s / %s" % (google_email, google_password)
- response = self.make_request('POST', path=path, data=data)
+ data = urllib.urlencode(data)
+ response = self.make_request('POST', base_url, path, data=data)
auth_token = authtoken_pat.search(response.read())
self.auth_token = auth_token.groups(0)[0]
def get_accounts(self, start_index=1, max_results=None):
+ base_url = 'https://www.google.com'
path = '/analytics/feeds/accounts/default'
- data = {'start-index': start_index, }
+ data = {'start-index': start_index}
if max_results:
data['max-results'] = max_results
data = urllib.urlencode(data)
- response = self.make_request('GET', path, data=data)
+ response = self.make_request('GET', base_url, path, data=data)
raw_xml = response.read()
xml_tree = ElementTree.fromstring(raw_xml)
account_list = []
@@ -72,12 +78,14 @@ def get_account(self, profile_id):
if account.profile_id == profile_id:
return account
- def make_request(self, method, path, headers=None, data=''):
+ def make_request(self, method, base_url, path, headers=None, data=''):
if headers == None:
headers = {
'User-Agent': self.user_agent,
- 'Authorization': 'GoogleLogin auth=%s' % self.auth_token
+ 'GData-Version': '2'
}
+ if self.auth_token:
+ headers['Authorization'] = 'GoogleLogin auth=%s' % self.auth_token
else:
headers = headers.copy()
@@ -98,9 +106,9 @@ def make_request(self, method, path, headers=None, data=''):
data += "&prettyprint=true"
if method == 'POST':
- request = urllib2.Request(self.default_host + path, data, headers)
+ request = urllib2.Request(base_url + path, data, headers)
elif method == 'GET':
- request = urllib2.Request(self.default_host + path, headers=headers)
+ request = urllib2.Request(base_url + path, headers=headers)
try:
response = urllib2.urlopen(request, timeout=TIMEOUT)
@@ -15,9 +15,9 @@ def __init__(self, raw_xml):
xml_tree = ElementTree.fromstring(self.raw_xml)
self.id = xml_tree.find('{http://www.w3.org/2005/Atom}id').text
self.title = xml_tree.find('{http://www.w3.org/2005/Atom}title').text
- self.totalResults = int(xml_tree.find('{http://a9.com/-/spec/opensearchrss/1.0/}totalResults').text)
- self.startIndex = int(xml_tree.find('{http://a9.com/-/spec/opensearchrss/1.0/}startIndex').text)
- self.itemsPerPage = int(xml_tree.find('{http://a9.com/-/spec/opensearchrss/1.0/}itemsPerPage').text)
+ self.totalResults = int(xml_tree.find('{http://a9.com/-/spec/opensearch/1.1/}totalResults').text)
+ self.startIndex = int(xml_tree.find('{http://a9.com/-/spec/opensearch/1.1/}startIndex').text)
+ self.itemsPerPage = int(xml_tree.find('{http://a9.com/-/spec/opensearch/1.1/}itemsPerPage').text)
endDate = xml_tree.find('{http://schemas.google.com/analytics/2009}endDate').text
self.endDate = datetime.date.fromtimestamp(time.mktime(time.strptime(endDate, '%Y-%m-%d')))

0 comments on commit 6be9b5f

Please sign in to comment.