Skip to content

Commit 45803e9

Browse files
author
vf@google.com
committed
1 parent fdc9fbe commit 45803e9

62 files changed

Lines changed: 8698 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2009 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Sample Google Analytics Data Export API Account Feed application.
18+
19+
This sample demonstrates how to retrieve the important data from the Google
20+
Analytics Data Export API Account feed using the Python Client library. This
21+
requires a Google Analytics username and password and uses the Client Login
22+
authorization routine.
23+
24+
Class AccountFeedDemo: Prints all the import Account Feed data.
25+
"""
26+
27+
__author__ = 'api.nickm@google.com (Nick Mihailovski)'
28+
29+
30+
import gdata.analytics.client
31+
import gdata.sample_util
32+
33+
def main():
34+
"""Main fucntion for the sample."""
35+
36+
demo = AccountFeedDemo()
37+
demo.PrintFeedDetails()
38+
demo.PrintAdvancedSegments()
39+
demo.PrintCustomVarForOneEntry()
40+
demo.PrintGoalsForOneEntry()
41+
demo.PrintAccountEntries()
42+
43+
44+
class AccountFeedDemo(object):
45+
"""Prints the Google Analytics account feed
46+
47+
Attributes:
48+
account_feed: Google Analytics AccountList returned form the API.
49+
"""
50+
51+
def __init__(self):
52+
"""Inits AccountFeedDemo."""
53+
54+
SOURCE_APP_NAME = 'Google-accountFeedDemoPython-v1'
55+
my_client = gdata.analytics.client.AnalyticsClient(source=SOURCE_APP_NAME)
56+
57+
try:
58+
gdata.sample_util.authorize_client(
59+
my_client,
60+
service=my_client.auth_service,
61+
source=SOURCE_APP_NAME,
62+
scopes=['https://www.google.com/analytics/feeds/'])
63+
except gdata.client.BadAuthentication:
64+
exit('Invalid user credentials given.')
65+
except gdata.client.Error:
66+
exit('Login Error')
67+
68+
account_query = gdata.analytics.client.AccountFeedQuery()
69+
self.feed = my_client.GetAccountFeed(account_query)
70+
71+
def PrintFeedDetails(self):
72+
"""Prints important Analytics related data found at the top of the feed."""
73+
74+
print '-------- Important Feed Data --------'
75+
print 'Feed Title = ' + self.feed.title.text
76+
print 'Feed Id = ' + self.feed.id.text
77+
print 'Total Results Found = ' + self.feed.total_results.text
78+
print 'Start Index = ' + self.feed.start_index.text
79+
print 'Results Returned = ' + self.feed.items_per_page.text
80+
81+
def PrintAdvancedSegments(self):
82+
"""Prints the advanced segments for this user."""
83+
84+
print '-------- Advances Segments --------'
85+
if not self.feed.segment:
86+
print 'No advanced segments found'
87+
else:
88+
for segment in self.feed.segment:
89+
print 'Segment Name = ' + segment.name
90+
print 'Segment Id = ' + segment.id
91+
print 'Segment Definition = ' + segment.definition.text
92+
93+
def PrintCustomVarForOneEntry(self):
94+
"""Prints custom variable information for the first profile that has
95+
custom variable configured."""
96+
97+
print '-------- Custom Variables --------'
98+
if not self.feed.entry:
99+
print 'No entries found'
100+
else:
101+
for entry in self.feed.entry:
102+
if entry.custom_variable:
103+
for custom_variable in entry.custom_variable:
104+
print 'Custom Variable Index = ' + custom_variable.index
105+
print 'Custom Variable Name = ' + custom_variable.name
106+
print 'Custom Variable Scope = ' + custom_variable.scope
107+
return
108+
print 'No custom variables defined for this user'
109+
110+
def PrintGoalsForOneEntry(self):
111+
"""Prints All the goal information for one profile."""
112+
113+
print '-------- Goal Configuration --------'
114+
if not self.feed.entry:
115+
print 'No entries found'
116+
else:
117+
for entry in self.feed.entry:
118+
if entry.goal:
119+
for goal in entry.goal:
120+
print 'Goal Number = ' + goal.number
121+
print 'Goal Name = ' + goal.name
122+
print 'Goal Value = ' + goal.value
123+
print 'Goal Active = ' + goal.active
124+
125+
if goal.destination:
126+
self.PrintDestinationGoal(goal.destination)
127+
elif goal.engagement:
128+
self.PrintEngagementGoal(goal.engagement)
129+
return
130+
131+
def PrintDestinationGoal(self, destination):
132+
"""Prints the important information for destination goals including all
133+
the configured steps if they exist.
134+
135+
Args:
136+
destination: gdata.data.Destination The destination goal configuration.
137+
"""
138+
139+
print '----- Destination Goal -----'
140+
print 'Expression = ' + destination.expression
141+
print 'Match Type = ' + destination.match_type
142+
print 'Step 1 Required = ' + destination.step1_required
143+
print 'Case Sensitive = ' + destination.case_sensitive
144+
145+
# Print goal steps.
146+
if destination.step:
147+
print '----- Destination Goal Steps -----'
148+
for step in destination.step:
149+
print 'Step Number = ' + step.number
150+
print 'Step Name = ' + step.name
151+
print 'Step Path = ' + step.path
152+
153+
def PrintEngagementGoal(self, engagement):
154+
"""Prints the important information for engagement goals.
155+
156+
Args:
157+
engagement: gdata.data.Engagement The engagement goal configuration.
158+
"""
159+
160+
print '----- Engagement Goal -----'
161+
print 'Goal Type = ' + engagement.type
162+
print 'Goal Engagement = ' + engagement.comparison
163+
print 'Goal Threshold = ' + engagement.threshold_value
164+
165+
def PrintAccountEntries(self):
166+
"""Prints important Analytics data found in each account entry"""
167+
168+
print '-------- First 1000 Profiles in Account Feed --------'
169+
if not self.feed.entry:
170+
print 'No entries found'
171+
else:
172+
for entry in self.feed.entry:
173+
print 'Web Property ID = ' + entry.GetProperty('ga:webPropertyId').value
174+
print 'Account Name = ' + entry.GetProperty('ga:accountName').value
175+
print 'Account Id = ' + entry.GetProperty('ga:accountId').value
176+
print 'Profile Name = ' + entry.title.text
177+
print 'Profile ID = ' + entry.GetProperty('ga:profileId').value
178+
print 'Table ID = ' + entry.table_id.text
179+
print 'Currency = ' + entry.GetProperty('ga:currency').value
180+
print 'TimeZone = ' + entry.GetProperty('ga:timezone').value
181+
if entry.custom_variable:
182+
print 'This profile has custom variables'
183+
if entry.goal:
184+
print 'This profile has goals'
185+
186+
187+
if __name__ == '__main__':
188+
main()
189+
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2009 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Sample Google Analytics Data Export API Data Feed application.
18+
19+
This sample demonstrates how to make requests and retrieve the important
20+
information from the Google Analytics Data Export API Data Feed. This
21+
sample requires a Google Analytics username and password and uses the
22+
Client Login authorization routine.
23+
24+
Class DataFeedDemo: Prints all the important Data Feed informantion.
25+
"""
26+
27+
__author__ = 'api.nickm@google.com (Nick Mihailovski)'
28+
29+
30+
import gdata.analytics.client
31+
import gdata.sample_util
32+
33+
def main():
34+
"""Main function for the sample."""
35+
36+
demo = DataFeedDemo()
37+
demo.PrintFeedDetails()
38+
demo.PrintDataSources()
39+
demo.PrintFeedAggregates()
40+
demo.PrintSegmentInfo()
41+
demo.PrintOneEntry()
42+
demo.PrintFeedTable()
43+
44+
45+
class DataFeedDemo(object):
46+
"""Gets data from the Data Feed.
47+
48+
Attributes:
49+
data_feed: Google Analytics AccountList returned form the API.
50+
"""
51+
52+
def __init__(self):
53+
"""Inits DataFeedDemo."""
54+
55+
SOURCE_APP_NAME = 'Google-dataFeedDemoPython-v1'
56+
my_client = gdata.analytics.client.AnalyticsClient(source=SOURCE_APP_NAME)
57+
58+
try:
59+
gdata.sample_util.authorize_client(
60+
my_client,
61+
service=my_client.auth_service,
62+
source=SOURCE_APP_NAME,
63+
scopes=['https://www.google.com/analytics/feeds/'])
64+
except gdata.client.BadAuthentication:
65+
exit('Invalid user credentials given.')
66+
except gdata.client.Error:
67+
exit('Login Error')
68+
69+
table_id = gdata.sample_util.get_param(
70+
name='table_id',
71+
prompt='Please enter your Google Analytics Table id (format ga:xxxx)')
72+
73+
# DataFeedQuery simplifies constructing API queries and uri encodes params.
74+
data_query = gdata.analytics.client.DataFeedQuery({
75+
'ids': table_id,
76+
'start-date': '2008-10-01',
77+
'end-date': '2008-10-30',
78+
'dimensions': 'ga:source,ga:medium',
79+
'metrics': 'ga:visits',
80+
'sort': '-ga:visits',
81+
'filters': 'ga:medium==referral',
82+
'max-results': '50'})
83+
84+
self.feed = my_client.GetDataFeed(data_query)
85+
86+
def PrintFeedDetails(self):
87+
"""Prints important Analytics related data found at the top of the feed."""
88+
89+
print '\n-------- Feed Data --------'
90+
print 'Feed Title = ' + self.feed.title.text
91+
print 'Feed Id = ' + self.feed.id.text
92+
print 'Total Results Found = ' + self.feed.total_results.text
93+
print 'Start Index = ' + self.feed.start_index.text
94+
print 'Results Returned = ' + self.feed.items_per_page.text
95+
print 'Start Date = ' + self.feed.start_date.text
96+
print 'End Date = ' + self.feed.end_date.text
97+
98+
def PrintDataSources(self):
99+
"""Prints data found in the data source elements.
100+
101+
This data has information about the Google Analytics account the referenced
102+
table ID belongs to. Note there is currently exactly one data source in
103+
the data feed.
104+
"""
105+
106+
data_source = self.feed.data_source[0]
107+
108+
print '\n-------- Data Source Data --------'
109+
print 'Table ID = ' + data_source.table_id.text
110+
print 'Table Name = ' + data_source.table_name.text
111+
print 'Web Property Id = ' + data_source.GetProperty('ga:webPropertyId').value
112+
print 'Profile Id = ' + data_source.GetProperty('ga:profileId').value
113+
print 'Account Name = ' + data_source.GetProperty('ga:accountName').value
114+
115+
def PrintFeedAggregates(self):
116+
"""Prints data found in the aggregates elements.
117+
118+
This contains the sum of all the metrics defined in the query across.
119+
This sum spans all the rows matched in the feed.total_results property
120+
and not just the rows returned by the response.
121+
"""
122+
123+
aggregates = self.feed.aggregates
124+
125+
print '\n-------- Metric Aggregates --------'
126+
for met in aggregates.metric:
127+
print ''
128+
print 'Metric Name = ' + met.name
129+
print 'Metric Value = ' + met.value
130+
print 'Metric Type = ' + met.type
131+
print 'Metric CI = ' + met.confidence_interval
132+
133+
def PrintSegmentInfo(self):
134+
"""Prints segment information if the query has advanced segments
135+
defined."""
136+
137+
if self.feed.segment:
138+
print '-------- Advanced Segments Information --------'
139+
for segment in self.feed.segment:
140+
print 'Segment Name = ' + segment.name
141+
print 'Segment Id = ' + segment.id
142+
print 'Segment Definition = ' + segment.definition.value
143+
144+
def PrintOneEntry(self):
145+
"""Prints all the important Google Analytics data found in an entry"""
146+
147+
print '\n-------- One Entry --------'
148+
if len(self.feed.entry) == 0:
149+
print 'No entries found'
150+
return
151+
152+
entry = self.feed.entry[0]
153+
print 'ID = ' + entry.id.text
154+
155+
for dim in entry.dimension:
156+
print 'Dimension Name = ' + dim.name
157+
print 'Dimension Value = ' + dim.value
158+
159+
for met in entry.metric:
160+
print 'Metric Name = ' + met.name
161+
print 'Metric Value = ' + met.value
162+
print 'Metric Type = ' + met.type
163+
print 'Metric CI = ' + met.confidence_interval
164+
165+
def PrintFeedTable(self):
166+
"""Prints all the entries as a table."""
167+
168+
print '\n-------- All Entries In a Table --------'
169+
for entry in self.feed.entry:
170+
for dim in entry.dimension:
171+
print ('Dimension Name = %s \t Dimension Value = %s'
172+
% (dim.name, dim.value))
173+
for met in entry.metric:
174+
print ('Metric Name = %s \t Metric Value = %s'
175+
% (met.name, met.value))
176+
print '---'
177+
178+
179+
if __name__ == '__main__':
180+
main()
181+

0 commit comments

Comments
 (0)