Skip to content

Commit 7dc9e91

Browse files
author
j.s@google.com
committed
Adding Google Analytics API wrapper written by Sal Uryasev.
1 parent 1e4ac44 commit 7dc9e91

4 files changed

Lines changed: 559 additions & 2 deletions

File tree

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- Google Webmaster Tools Data API
4141
- Blogger Data API
4242
- Google Health API
43+
- Google Book Search API
44+
- Google Analytics API
4345
- core Google data API functionality
4446
The core Google data code provides sufficient functionality to use this
4547
library with any Google data API (even if a module hasn't been written for
@@ -53,7 +55,7 @@
5355
packages=['atom', 'gdata', 'gdata.calendar', 'gdata.base',
5456
'gdata.spreadsheet', 'gdata.apps', 'gdata.apps.emailsettings',
5557
'gdata.apps.migration', 'gdata.apps.groups',
56-
'gdata.docs', 'gdata.codesearch',
58+
'gdata.docs', 'gdata.codesearch', 'gdata.books',
5759
'gdata.photos', 'gdata.exif', 'gdata.geo', 'gdata.media',
5860
'gdata.contacts', 'gdata.youtube', 'gdata.webmastertools',
5961
'gdata.blogger', 'gdata.alt', 'gdata.oauth', 'gdata.tlslite',

src/gdata/analytics/__init__.py

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#!/usr/bin/python
2+
#
3+
# Original Copyright (C) 2006 Google Inc.
4+
# Refactored in 2009 to work for Google Analytics by Sal Uryasev at Juice Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# Note that this module will not function without specifically adding
19+
# 'analytics': [ #Google Analytics
20+
# 'https://www.google.com/analytics/feeds/'],
21+
# to CLIENT_LOGIN_SCOPES in the gdata/service.py file
22+
23+
"""Contains extensions to Atom objects used with Google Analytics."""
24+
25+
__author__ = 'api.suryasev (Sal Uryasev)'
26+
27+
import atom
28+
import gdata
29+
30+
GAN_NAMESPACE = 'http://schemas.google.com/analytics/2009'
31+
32+
class TableId(gdata.GDataEntry):
33+
"""tableId element."""
34+
_tag = 'tableId'
35+
_namespace = GAN_NAMESPACE
36+
37+
class Property(gdata.GDataEntry):
38+
_tag = 'property'
39+
_namespace = GAN_NAMESPACE
40+
_children = gdata.GDataEntry._children.copy()
41+
_attributes = gdata.GDataEntry._attributes.copy()
42+
43+
_attributes['name'] = 'name'
44+
_attributes['value'] = 'value'
45+
46+
def __init__(self, name=None, value=None, *args, **kwargs):
47+
self.name = name
48+
self.value = value
49+
super(Property, self).__init__(*args, **kwargs)
50+
51+
def __str__(self):
52+
return self.value
53+
54+
def __repr__(self):
55+
return self.value
56+
57+
class AccountListEntry(gdata.GDataEntry):
58+
"""The Google Documents version of an Atom Entry"""
59+
60+
_tag = 'entry'
61+
_namespace = atom.ATOM_NAMESPACE
62+
_children = gdata.GDataEntry._children.copy()
63+
_attributes = gdata.GDataEntry._attributes.copy()
64+
_children['{%s}tableId' % GAN_NAMESPACE] = ('tableId',
65+
[TableId])
66+
_children['{%s}property' % GAN_NAMESPACE] = ('property',
67+
[Property])
68+
69+
def __init__(self, tableId=None, property=None,
70+
*args, **kwargs):
71+
self.tableId = tableId
72+
self.property = property
73+
super(AccountListEntry, self).__init__(*args, **kwargs)
74+
75+
76+
def AccountListEntryFromString(xml_string):
77+
"""Converts an XML string into an AccountListEntry object.
78+
79+
Args:
80+
xml_string: string The XML describing a Document List feed entry.
81+
82+
Returns:
83+
A AccountListEntry object corresponding to the given XML.
84+
"""
85+
return atom.CreateClassFromXMLString(AccountListEntry, xml_string)
86+
87+
88+
class AccountListFeed(gdata.GDataFeed):
89+
"""A feed containing a list of Google Documents Items"""
90+
91+
_tag = 'feed'
92+
_namespace = atom.ATOM_NAMESPACE
93+
_children = gdata.GDataFeed._children.copy()
94+
_attributes = gdata.GDataFeed._attributes.copy()
95+
_children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
96+
[AccountListEntry])
97+
98+
99+
def AccountListFeedFromString(xml_string):
100+
"""Converts an XML string into an AccountListFeed object.
101+
102+
Args:
103+
xml_string: string The XML describing an AccountList feed.
104+
105+
Returns:
106+
An AccountListFeed object corresponding to the given XML.
107+
All properties are also linked to with a direct reference
108+
from each entry object for convenience. (e.g. entry.AccountName)
109+
"""
110+
feed = atom.CreateClassFromXMLString(AccountListFeed, xml_string)
111+
for entry in feed.entry:
112+
for pro in entry.property:
113+
entry.__dict__[pro.name.replace('ga:','')] = pro
114+
for td in entry.tableId:
115+
td.__dict__['value'] = td.text
116+
return feed
117+
118+
class Dimension(gdata.GDataEntry):
119+
_tag = 'dimension'
120+
_namespace = GAN_NAMESPACE
121+
_children = gdata.GDataEntry._children.copy()
122+
_attributes = gdata.GDataEntry._attributes.copy()
123+
124+
_attributes['name'] = 'name'
125+
_attributes['value'] = 'value'
126+
_attributes['type'] = 'type'
127+
_attributes['confidenceInterval'] = 'confidence_interval'
128+
129+
def __init__(self, name=None, value=None, type=None,
130+
confidence_interval = None, *args, **kwargs):
131+
self.name = name
132+
self.value = value
133+
self.type = type
134+
self.confidence_interval = confidence_interval
135+
super(Dimension, self).__init__(*args, **kwargs)
136+
137+
def __str__(self):
138+
return self.value
139+
140+
def __repr__(self):
141+
return self.value
142+
143+
class Metric(gdata.GDataEntry):
144+
_tag = 'metric'
145+
_namespace = GAN_NAMESPACE
146+
_children = gdata.GDataEntry._children.copy()
147+
_attributes = gdata.GDataEntry._attributes.copy()
148+
149+
_attributes['name'] = 'name'
150+
_attributes['value'] = 'value'
151+
_attributes['type'] = 'type'
152+
_attributes['confidenceInterval'] = 'confidence_interval'
153+
154+
def __init__(self, name=None, value=None, type=None,
155+
confidence_interval = None, *args, **kwargs):
156+
self.name = name
157+
self.value = value
158+
self.type = type
159+
self.confidence_interval = confidence_interval
160+
super(Metric, self).__init__(*args, **kwargs)
161+
162+
def __str__(self):
163+
return self.value
164+
165+
def __repr__(self):
166+
return self.value
167+
168+
class AnalyticsDataEntry(gdata.GDataEntry):
169+
"""The Google Analytics version of an Atom Entry"""
170+
171+
_tag = 'entry'
172+
_namespace = atom.ATOM_NAMESPACE
173+
_children = gdata.GDataEntry._children.copy()
174+
_attributes = gdata.GDataEntry._attributes.copy()
175+
176+
_children['{%s}dimension' % GAN_NAMESPACE] = ('dimension',
177+
[Dimension])
178+
179+
_children['{%s}metric' % GAN_NAMESPACE] = ('metric',
180+
[Metric])
181+
182+
def __init__(self, dimension=None, metric=None, *args, **kwargs):
183+
self.dimension = dimension
184+
self.metric = metric
185+
186+
super(AnalyticsDataEntry, self).__init__(*args, **kwargs)
187+
188+
class AnalyticsDataFeed(gdata.GDataFeed):
189+
"""A feed containing a list of Google Analytics Data Feed"""
190+
191+
_tag = 'feed'
192+
_namespace = atom.ATOM_NAMESPACE
193+
_children = gdata.GDataFeed._children.copy()
194+
_attributes = gdata.GDataFeed._attributes.copy()
195+
_children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
196+
[AnalyticsDataEntry])
197+
198+
199+
"""
200+
Data Feed
201+
"""
202+
203+
def AnalyticsDataFeedFromString(xml_string):
204+
"""Converts an XML string into an AccountListFeed object.
205+
206+
Args:
207+
xml_string: string The XML describing an AccountList feed.
208+
209+
Returns:
210+
An AccountListFeed object corresponding to the given XML.
211+
Each metric and dimension is also referenced directly from
212+
the entry for easier access. (e.g. entry.keyword.value)
213+
"""
214+
feed = atom.CreateClassFromXMLString(AnalyticsDataFeed, xml_string)
215+
if feed.entry:
216+
for entry in feed.entry:
217+
for met in entry.metric:
218+
entry.__dict__[met.name.replace('ga:','')] = met
219+
for dim in entry.dimension:
220+
entry.__dict__[dim.name.replace('ga:','')] = dim
221+
222+
return feed

0 commit comments

Comments
 (0)