My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
#
# Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sample Google Analytics Data Export API Data Feed application.

This sample demonstrates how to make requests and retrieve the important
information from the Google Analytics Data Export API Data Feed. This
sample requires a Google Analytics username and password and uses the
Client Login authorization routine.

Class DataFeedDemo: Prints all the important Data Feed informantion.
"""

__author__ = 'api.nickm@google.com (Nick Mihailovski)'


import gdata.analytics.client
import gdata.sample_util

def main():
"""Main function for the sample."""

demo = DataFeedDemo()
demo.PrintFeedDetails()
demo.PrintDataSources()
demo.PrintFeedAggregates()
demo.PrintSegmentInfo()
demo.PrintOneEntry()
demo.PrintFeedTable()


class DataFeedDemo(object):
"""Gets data from the Data Feed.

Attributes:
data_feed: Google Analytics AccountList returned form the API.
"""

def __init__(self):
"""Inits DataFeedDemo."""

SOURCE_APP_NAME = 'Google-dataFeedDemoPython-v2'
my_client = gdata.analytics.client.AnalyticsClient(source=SOURCE_APP_NAME)

try:
gdata.sample_util.authorize_client(
my_client,
service=my_client.auth_service,
source=SOURCE_APP_NAME,
scopes=['https://www.google.com/analytics/feeds/'])
except gdata.client.BadAuthentication:
exit('Invalid user credentials given.')
except gdata.client.Error:
exit('Login Error')

table_id = gdata.sample_util.get_param(
name='table_id',
prompt='Please enter your Google Analytics Table id (format ga:xxxx)')

# DataFeedQuery simplifies constructing API queries and uri encodes params.
data_query = gdata.analytics.client.DataFeedQuery({
'ids': table_id,
'start-date': '2008-10-01',
'end-date': '2008-10-30',
'dimensions': 'ga:source,ga:medium',
'metrics': 'ga:visits',
'sort': '-ga:visits',
'filters': 'ga:medium==referral',
'max-results': '50'})

self.feed = my_client.GetDataFeed(data_query)

def PrintFeedDetails(self):
"""Prints important Analytics related data found at the top of the feed."""

print '\n-------- Feed Data --------'
print 'Feed Title = ' + self.feed.title.text
print 'Feed Id = ' + self.feed.id.text
print 'Total Results Found = ' + self.feed.total_results.text
print 'Start Index = ' + self.feed.start_index.text
print 'Results Returned = ' + self.feed.items_per_page.text
print 'Start Date = ' + self.feed.start_date.text
print 'End Date = ' + self.feed.end_date.text
print 'Has Sampled Data = ' + str(self.feed.HasSampledData())

def PrintDataSources(self):
"""Prints data found in the data source elements.

This data has information about the Google Analytics account the referenced
table ID belongs to. Note there is currently exactly one data source in
the data feed.
"""

data_source = self.feed.data_source[0]

print '\n-------- Data Source Data --------'
print 'Table ID = ' + data_source.table_id.text
print 'Table Name = ' + data_source.table_name.text
print 'Web Property Id = ' + data_source.GetProperty('ga:webPropertyId').value
print 'Profile Id = ' + data_source.GetProperty('ga:profileId').value
print 'Account Name = ' + data_source.GetProperty('ga:accountName').value

def PrintFeedAggregates(self):
"""Prints data found in the aggregates elements.

This contains the sum of all the metrics defined in the query across.
This sum spans all the rows matched in the feed.total_results property
and not just the rows returned by the response.
"""

aggregates = self.feed.aggregates

print '\n-------- Metric Aggregates --------'
for met in aggregates.metric:
print ''
print 'Metric Name = ' + met.name
print 'Metric Value = ' + met.value
print 'Metric Type = ' + met.type
print 'Metric CI = ' + met.confidence_interval

def PrintSegmentInfo(self):
"""Prints segment information if the query has advanced segments
defined."""

print '-------- Advanced Segments Information --------'
if self.feed.segment:
if segment.name:
print 'Segment Name = ' + str(segment.name)
if segment.id:
print 'Segment Id = ' + str(segment.id)
print 'Segment Definition = ' + segment.definition.text
else:
print 'No segments defined'

def PrintOneEntry(self):
"""Prints all the important Google Analytics data found in an entry"""

print '\n-------- One Entry --------'
if len(self.feed.entry) == 0:
print 'No entries found'
return

entry = self.feed.entry[0]
print 'ID = ' + entry.id.text

for dim in entry.dimension:
print 'Dimension Name = ' + dim.name
print 'Dimension Value = ' + dim.value

for met in entry.metric:
print 'Metric Name = ' + met.name
print 'Metric Value = ' + met.value
print 'Metric Type = ' + met.type
print 'Metric CI = ' + met.confidence_interval

def PrintFeedTable(self):
"""Prints all the entries as a table."""

print '\n-------- All Entries In a Table --------'
for entry in self.feed.entry:
for dim in entry.dimension:
print ('Dimension Name = %s \t Dimension Value = %s'
% (dim.name, dim.value))
for met in entry.metric:
print ('Metric Name = %s \t Metric Value = %s'
% (met.name, met.value))
print '---'


if __name__ == '__main__':
main()

Change log

79f7b0921e30 by Ali Afshar <afs...@google.com> on Dec 18, 2011   Diff
Fixed typo in analytics sample.
Go to: 

Older revisions

daa8898c9254 by Joe Gregorio <j...@bitworking.org> on Jan 21, 2011   Diff
Fixes  bug 462 
73f3fbb5ea88 by Vic Fryzel <vicfry...@google.com> on Nov 16, 2010   Diff
Committing
http://codereview.appspot.com/2734041/
on behalf of api.nickm at google dot
com
5bd5d560b263 by v...@google.com on Jun 30, 2010   Diff
Fixing file permissions for samples.
See http://code.google.com/p/gdata-
python-client/issues/detail?id=135
All revisions of this file

File info

Size: 6246 bytes, 184 lines
Powered by Google Project Hosting