|
| 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 | + |
0 commit comments