|
| 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.PrintOneEntry() |
| 41 | + demo.PrintFeedTable() |
| 42 | + |
| 43 | + |
| 44 | +class DataFeedDemo(object): |
| 45 | + """Gets data from the Data Feed. |
| 46 | +
|
| 47 | + Attributes: |
| 48 | + data_feed: Google Analytics AccountList returned form the API. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self): |
| 52 | + """Inits DataFeedDemo.""" |
| 53 | + |
| 54 | + SOURCE_APP_NAME = 'Google-dataFeedDemoPython-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 | + table_id = gdata.sample_util.get_param( |
| 69 | + name='table_id', |
| 70 | + prompt='Please enter your Google Analytics Table id (format ga:xxxx)') |
| 71 | + |
| 72 | + # DataFeedQuery simplifies constructing API queries and uri encodes params. |
| 73 | + data_query = gdata.analytics.client.DataFeedQuery({ |
| 74 | + 'ids': table_id, |
| 75 | + 'start-date': '2008-10-01', |
| 76 | + 'end-date': '2008-10-30', |
| 77 | + 'dimensions': 'ga:source,ga:medium', |
| 78 | + 'metrics': 'ga:visits', |
| 79 | + 'sort': '-ga:visits', |
| 80 | + 'filters': 'ga:medium==referral', |
| 81 | + 'max-results': '50'}) |
| 82 | + |
| 83 | + self.feed = my_client.GetDataFeed(data_query) |
| 84 | + |
| 85 | + def PrintFeedDetails(self): |
| 86 | + """Prints important Analytics related data found at the top of the feed.""" |
| 87 | + |
| 88 | + print '\n-------- Feed Data --------' |
| 89 | + print 'Feed Title = ' + self.feed.title.text |
| 90 | + print 'Feed Id = ' + self.feed.id.text |
| 91 | + print 'Total Results Found = ' + self.feed.total_results.text |
| 92 | + print 'Start Index = ' + self.feed.start_index.text |
| 93 | + print 'Results Returned = ' + self.feed.items_per_page.text |
| 94 | + print 'Start Date = ' + self.feed.start_date.text |
| 95 | + print 'End Date = ' + self.feed.end_date.text |
| 96 | + |
| 97 | + def PrintDataSources(self): |
| 98 | + """Prints data found in the data source elements. |
| 99 | +
|
| 100 | + This data has information about the Google Analytics account the referenced |
| 101 | + table ID belongs to. Note there is currently exactly one data source in |
| 102 | + the data feed. |
| 103 | + """ |
| 104 | + |
| 105 | + data_source = self.feed.data_source[0] |
| 106 | + |
| 107 | + print '\n-------- Data Source Data --------' |
| 108 | + print 'Table ID = ' + data_source.table_id.text |
| 109 | + print 'Table Name = ' + data_source.table_name.text |
| 110 | + print 'Web Property Id = ' + data_source.GetProperty('ga:webPropertyId').value |
| 111 | + print 'Profile Id = ' + data_source.GetProperty('ga:profileId').value |
| 112 | + print 'Account Name = ' + data_source.GetProperty('ga:accountName').value |
| 113 | + |
| 114 | + def PrintFeedAggregates(self): |
| 115 | + """Prints data found in the aggregates elements. |
| 116 | +
|
| 117 | + This contains the sum of all the metrics defined in the query across. This |
| 118 | + sum spans all the rows matched in the feed.total_results property and not |
| 119 | + just the rows returned by the response. |
| 120 | + """ |
| 121 | + |
| 122 | + aggregates = self.feed.aggregates |
| 123 | + |
| 124 | + print '\n-------- Metric Aggregates --------' |
| 125 | + for met in aggregates.metric: |
| 126 | + print '' |
| 127 | + print 'Metric Name = ' + met.name |
| 128 | + print 'Metric Value = ' + met.value |
| 129 | + print 'Metric Type = ' + met.type |
| 130 | + print 'Metric CI = ' + met.confidence_interval |
| 131 | + |
| 132 | + def PrintOneEntry(self): |
| 133 | + """Prints all the important Google Analytics data found in an entry""" |
| 134 | + |
| 135 | + print '\n-------- One Entry --------' |
| 136 | + if len(self.feed.entry) == 0: |
| 137 | + print 'No entries found' |
| 138 | + return |
| 139 | + |
| 140 | + entry = self.feed.entry[0] |
| 141 | + print 'ID = ' + entry.id.text |
| 142 | + |
| 143 | + for dim in entry.dimension: |
| 144 | + print 'Dimension Name = ' + dim.name |
| 145 | + print 'Dimension Value = ' + dim.value |
| 146 | + |
| 147 | + for met in entry.metric: |
| 148 | + print 'Metric Name = ' + met.name |
| 149 | + print 'Metric Value = ' + met.value |
| 150 | + print 'Metric Type = ' + met.type |
| 151 | + print 'Metric CI = ' + met.confidence_interval |
| 152 | + |
| 153 | + def PrintFeedTable(self): |
| 154 | + """Prints all the entries as a table.""" |
| 155 | + |
| 156 | + print '\n-------- All Entries In a Table --------' |
| 157 | + for entry in self.feed.entry: |
| 158 | + for dim in entry.dimension: |
| 159 | + print ('Dimension Name = %s \t Dimension Value = %s' |
| 160 | + % (dim.name, dim.value)) |
| 161 | + for met in entry.metric: |
| 162 | + print ('Metric Name = %s \t Metric Value = %s' |
| 163 | + % (dim.name, dim.value)) |
| 164 | + print '---' |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == '__main__': |
| 168 | + main() |
| 169 | + |
0 commit comments