├── .gitignore ├── CONTRIBUTING ├── LICENSE ├── README.md ├── gav4 ├── README.md ├── __init__.py └── gav4.py ├── setup.cfg ├── setup.py └── tests ├── README.md ├── __init__.py ├── data.py └── test_gav4.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled python modules. 2 | *.pyc 3 | 4 | # Setuptools distribution folder. 5 | /dist/ 6 | 7 | # Python egg metadata, regenerated from source files by setuptools. 8 | /*.egg-info 9 | /*.egg -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at the end). 2 | 3 | ### Before you contribute 4 | Before we can use your code, you must sign the 5 | [Google Individual Contributor License Agreement] 6 | (https://cla.developers.google.com/about/google-individual) 7 | (CLA), which you can do online. The CLA is necessary mainly because you own the 8 | copyright to your changes, even after your contribution becomes part of our 9 | codebase, so we need your permission to use and distribute your code. We also 10 | need to be sure of various other things—for instance that you'll tell us if you 11 | know that your code infringes on other people's patents. You don't have to sign 12 | the CLA until after you've submitted your code for review and a member has 13 | approved it, but you must do it before we can put your code into our codebase. 14 | Before you start working on a larger contribution, you should get in touch with 15 | us first through the issue tracker with your idea so that we can help out and 16 | possibly guide you. Coordinating up front makes it much easier to avoid 17 | frustration later on. 18 | 19 | ### Code reviews 20 | All submissions, including submissions by project members, require review. We 21 | use Github pull requests for this purpose. 22 | 23 | ### The small print 24 | Contributions made by corporations are covered by a different agreement than 25 | the one above, the 26 | [Software Grant and Corporate Contributor License Agreement] 27 | (https://cla.developers.google.com/about/google-corporate). 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015 Google Inc. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # No longer actively maintained 2 | 3 | # GAV4 - Analytics Reporting API V4 Compatibility Library [![Analytics](https://ga-beacon.appspot.com/UA-76561751-1/googleanalytics/gav4-python?pixel)](https://github.com/googleanalytics/gav4-python) 4 | 5 | A library for converting Google Analytics [Core Reporting API V3](https://developers.google.com/analytics/devguides/reporting/core/v3/) requests to [Analytics Reporting API V4](https://developers.google.com/analytics/devguides/reporting/core/v4/) requests. 6 | 7 | ## Installation 8 | 9 | $ pip install --upgrade gav4 10 | 11 | 12 | ## Typical usage example 13 | 14 | There are two methods of using the gav4 library. You can `apply` the library to an authorized `analyticsreporting` Service object, which exposes a get method that operates much like the current Core Reporting API V3. 15 | 16 | import gav4 17 | 18 | # Apply the gav4 get method to the analyticsreporting service object. 19 | gav4.apply_gav4(analyticsreporting) 20 | 21 | # Call the gav4_get method with a V3 request and get a V3 response. 22 | v3_response = analyticsreporting.gav4_get(v3_request).execute() 23 | 24 | Alternatively, you can convert the requests and responses directly. 25 | 26 | # Convert a V3 request into a V4 request. 27 | v4_request = gav4.convert_request(v3_request) 28 | 29 | # Call the V4 API. 30 | v4_response = analyticsreporting.reports().batchGet(body=v4_request).execute() 31 | 32 | # Convert the V4 API response into a V3 response. 33 | v3_response = gav4.convert_report(v4_response.get('reports', [])[0]) 34 | 35 | ## Testing 36 | 37 | Run the tests with the following command: 38 | 39 | python setup.py tests 40 | 41 | This may require you to install the nose library -- `pip install nose`. 42 | 43 | ## Contributing 44 | 45 | 1. **Please sign one of the contributor license agreements below.** 46 | 2. Fork the repository, develop and test your code changes, add docs. 47 | 3. Make sure that your commit messages clearly describe the changes. 48 | 4. Send a pull request. 49 | 50 | 51 | ### Contributor License Agreements 52 | 53 | 54 | Before we can accept your pull requests you'll need to sign a Contributor License Agreement (CLA): 55 | 56 | - **If you are an individual writing original source code** and **you own the intellectual property**, then you'll need to sign an [individual CLA](https://developers.google.com/open-source/cla/individual). 57 | - **If you work for a company that wants to allow you to contribute your work**, then you'll need to sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate). 58 | 59 | You can sign these electronically (just scroll to the bottom). After that, we'll be able to accept your pull requests. 60 | 61 | 62 | ### Code reviews 63 | All submissions, including submissions by project members, require review. We 64 | use Github pull requests for this purpose. 65 | 66 | ### Naming 67 | 68 | This library is strictly for the Analytics Reporting API and not to be confused with the [Google Analytics Android SDK V4](https://developers.google.com/analytics/devguides/collection/android/v4/). 69 | -------------------------------------------------------------------------------- /gav4/README.md: -------------------------------------------------------------------------------- 1 | # GAV4 [![Analytics](https://ga-beacon.appspot.com/UA-76561751-1/googleanalytics/gav4-python/gav4?pixel)](https://github.com/googleanalytics/gav4-python) 2 | 3 | ## Contents 4 | 5 | ### [`gav4.py`](gav4.py) 6 | 7 | The `gav4.py` contains functions to convert each of the [Core Reporting API V3](https://developers.google.com/analytics/devguides/reporting/core/v3/) parameters to [Analytics Reporting API V4](https://developers.google.com/analytics/devguides/reporting/core/v4/) request fields. 8 | 9 | The file also contains the `apply_gav4` function which can be called with an authorized `analyticsreporting` service object; once applied, you can call: 10 | 11 | `analyticsreporting.gav4_get(...)` 12 | 13 | which takes the same parameters and returns the same response as when you call the Core reporting API V3 method: 14 | 15 | `analytics.data().ga().get(...)` 16 | -------------------------------------------------------------------------------- /gav4/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from .gav4 import * 3 | -------------------------------------------------------------------------------- /gav4/gav4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2016 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Library for converting V3 API request to V4 API requests. 17 | 18 | This library contains helper functions which convert a Core Reporting API V3 19 | request into a Analytics Reporting API V4 request. 20 | It is designed to work in concert to the Google API Python client library. 21 | 22 | import gav4 23 | 24 | # Apply the gav4 get method to the analyticsreporting service object. 25 | gav4.apply_gav4(analyticsreporting) 26 | 27 | # Call the gav4_get method with a V3 request and get a V3 response. 28 | v3_response = analyticsreporting.gav4_get(v3_request).execute() 29 | 30 | Alternatively, you can convert the requests and responses directly. 31 | 32 | # Convert a V3 request into a V4 request. 33 | v4_request = gav4.convert_request(v3_request) 34 | 35 | # Call the V4 API. 36 | v4_response = analyticsreporting.reports().batchGet(body=v4_request).execute() 37 | 38 | # Convert the V4 API response into a V3 response. 39 | v3_response = gav4.convert_report(v4_response.get('reports', [])[0]) 40 | 41 | """ 42 | 43 | from functools import partial 44 | 45 | 46 | __author__ = 'mcohoon@google.com (Matthew Cohoon)' 47 | 48 | GA_SEGMENT = 'ga:segment' 49 | 50 | SAMPLING_MAP = { 51 | 'DEFAULT': 'DEFAULT', 52 | 'FASTER': 'SMALL', 53 | 'HIGHER_PRECESION': 'LARGE' 54 | } 55 | 56 | 57 | def convert_dimensions(dimensions): 58 | """Converts a V3 dimensions parameter into V4 Dimension objects.""" 59 | return [{'name': name} for name in dimensions.split(',')] 60 | 61 | 62 | def convert_metrics(metrics): 63 | """Converts a V3 metrics parameter into V4 Metric objects.""" 64 | converted_metrics = [] 65 | for expression in metrics.split(','): 66 | metric = { 67 | 'expression': expression, 68 | } 69 | converted_metrics.append(metric) 70 | return converted_metrics 71 | 72 | 73 | def convert_segments(segment): 74 | """Converts a V3 segment parameter into an list with a V4 Segment object.""" 75 | return [{'segmentId': segment}] 76 | 77 | 78 | def convert_sorting(sortings): 79 | """Convert a V3 sort parameter into the V4 orderBys field. 80 | 81 | Args: 82 | sortings: A string representing the V3 sort parameter. 83 | Returns: 84 | A list of V4 orderBy objects. 85 | """ 86 | order_bys = [] 87 | 88 | for sorting in sortings.split(','): 89 | order_by = {} 90 | order_by['orderType'] = 'VALUE' 91 | if sorting.startswith('-'): 92 | order_by['fieldName'] = sorting[1:] 93 | order_by['sortOrder'] = 'DESCENDING' 94 | else: 95 | order_by['fieldName'] = sorting 96 | order_bys.append(order_by) 97 | return order_bys 98 | 99 | 100 | def convert_sampling(sampling): 101 | """Converts the V4 sampling parameter into V4 syntax.""" 102 | return SAMPLING_MAP.get(sampling, 'SAMPLING_UNSPECIFIED') 103 | 104 | 105 | def convert_dateranges(kwargs): 106 | """Creates the DateRange object from the parameters dictionary. 107 | 108 | Args: 109 | kwargs: a dict containing the parameters passed in the request. 110 | Returns: 111 | A list containing a DateRange object. 112 | """ 113 | date_range = { 114 | 'startDate': kwargs.get('start_date', None), 115 | 'endDate': kwargs.get('end_date', None) 116 | } 117 | return [date_range] 118 | 119 | 120 | def gav4_execute(self): 121 | """A method to hijack the execute method to convert the response. 122 | 123 | This method calls the V4 execute method captures the returned reports 124 | and then converts the initial Report object into V3 response. 125 | """ 126 | 127 | # Call the old execute. 128 | reports = self.old_execute() 129 | 130 | # Convert the response. 131 | return convert_report(reports.get('reports', [])[0]) 132 | 133 | 134 | def convert_request(**kwargs): 135 | """Converts the V3 request to a V4 request.""" 136 | report_request = {} 137 | 138 | # Convert the required arguments 139 | report_request['viewId'] = kwargs.get('ids') 140 | report_request['dateRanges'] = convert_dateranges(kwargs) 141 | report_request['metrics'] = convert_metrics(kwargs.get('metrics')) 142 | 143 | if 'dimensions' in kwargs: 144 | report_request['dimensions'] = convert_dimensions(kwargs.get('dimensions')) 145 | 146 | if 'segment' in kwargs: 147 | report_request['segments'] = convert_segments(kwargs.get('segment')) 148 | 149 | # Add the 'ga:segment' dimension. 150 | dimensions = report_request.get('dimensions', []) 151 | dimensions.append({'name': GA_SEGMENT}) 152 | report_request['dimensions'] = dimensions 153 | 154 | if 'filters' in kwargs: 155 | report_request['filtersExpression'] = kwargs.get('filters') 156 | 157 | if 'sort' in kwargs: 158 | report_request['order_bys'] = convert_sorting(kwargs.get('sort')) 159 | 160 | if 'samplingLevel' in kwargs: 161 | report_request['samplingLevel'] = convert_sampling( 162 | kwargs.get('samplingLevel')) 163 | 164 | if 'max_results' in kwargs: 165 | report_request['pageSize'] = kwargs.get('max_results') 166 | 167 | if 'include_empty_rows' in kwargs: 168 | report_request['includeEmptyRows'] = kwargs.get('include_empty_rows') 169 | 170 | return {'reportRequests': [report_request]} 171 | 172 | 173 | def gav4_get(self, **kwargs): 174 | """A service object method which converts and calls the requests. 175 | 176 | This method gets attached to an authorized analytics V4 service object. 177 | It operates like a V3 data().ga().get() method. 178 | 179 | Args: 180 | kwargs: The keyword arguments to a V3 request. 181 | Returns: 182 | A V4 BatchGet object. 183 | """ 184 | batch_get = self.reports().batchGet(body=convert_request(**kwargs)) 185 | 186 | # Rename the existing batch get so we hijack the response. 187 | batch_get.old_execute = batch_get.execute 188 | batch_get.execute = partial(gav4_execute, batch_get) 189 | return batch_get 190 | 191 | 192 | def apply_gav4(analytics): 193 | """Adds the gav4_get method to the authorized service object. 194 | 195 | Args: 196 | analytics: a V4 authorized service object. 197 | """ 198 | analytics.gav4_get = partial(gav4_get, analytics) 199 | 200 | 201 | def convert_column_headers(report): 202 | """Converts the V4 report headers into the V3 report column headers. 203 | 204 | Args: 205 | report: a Google Analytics V4 Report object. 206 | Returns: 207 | A list of V3 report column headers. 208 | """ 209 | column_header = report.get('columnHeader', {}) 210 | dimensions = column_header.get('dimensions', []) 211 | metric_headers = column_header.get('metricHeader').get('metricHeaderEntries') 212 | 213 | headers = [] 214 | for dimension in dimensions: 215 | if dimension == GA_SEGMENT: 216 | continue 217 | header = {} 218 | header['columnType'] = 'DIMENSION' 219 | header['dataType'] = 'STRING' 220 | header['name'] = dimension 221 | headers.append(header) 222 | for metric_header in metric_headers: 223 | header = {} 224 | header['columnType'] = 'METRIC' 225 | header['dataType'] = metric_header.get('type', 'FLOAT') 226 | header['name'] = metric_header.get('name', '') 227 | headers.append(header) 228 | return headers 229 | 230 | 231 | def convert_rows(report): 232 | """Converts the V4 report row data into V3 report row data. 233 | 234 | Args: 235 | report: a Google Analytics V4 Report object. 236 | Returns: 237 | A list of rows of the V3 report object. 238 | """ 239 | dimensions = report.get('columnHeader', {}).get('dimensions', []) 240 | 241 | rows = [] 242 | report_data = report.get('data', {}) 243 | for row in report_data.get('rows', []): 244 | row_data = [] 245 | 246 | # Append the dimensions. 247 | for dimension, value in zip(dimensions, row.get('dimensions', [])): 248 | if dimension == GA_SEGMENT: 249 | continue 250 | row_data.append(value) 251 | 252 | # Append the metrics. 253 | for value in row.get('metrics', [])[0].get('values', []): 254 | row_data.append(value) 255 | 256 | rows.append(row_data) 257 | return rows 258 | 259 | 260 | def convert_totals(report): 261 | """Converts the V4 report totals into the V3 report totals. 262 | 263 | Args: 264 | report: a Google Analytics V4 Report object. 265 | Returns: 266 | A list of rows of the V3 report object. 267 | """ 268 | report_totals = report.get('data').get('totals')[0].get('values', []) 269 | metric_headers = report.get('columnHeader', {}).get( 270 | 'metricHeader').get('metricHeaderEntries') 271 | 272 | totals = {} 273 | for metric_header, value in zip(metric_headers, report_totals): 274 | totals[metric_header.get('name')] = value 275 | return totals 276 | 277 | 278 | def convert_report(report): 279 | """Converts the V4 report response into a V3 report response.""" 280 | data = {} 281 | data['kind'] = 'analytics#gaData' 282 | data['columnHeaders'] = convert_column_headers(report) 283 | data['rows'] = convert_rows(report) 284 | data['totalsForAllResults'] = convert_totals(report) 285 | 286 | # Calculated sampling. 287 | report_data = report.get('data', {}) 288 | sample_sizes = report_data.get('samplesReadCounts', []) 289 | sample_spaces = report_data.get('samplingSpaceSizes', []) 290 | if sample_sizes and sample_spaces: 291 | data['sampleSize'] = sample_sizes[0] 292 | data['sampleSpace'] = sample_spaces[0] 293 | data['containsSampledData'] = True 294 | else: 295 | data['containsSampledData'] = False 296 | 297 | data['isDataGolden'] = report_data.get('isDataGolden') 298 | if 'filteredForPrivacyReasons' in report_data: 299 | data['filteredForPrivacyReasons'] = report_data.get( 300 | 'filteredForPrivacyReasons') 301 | 302 | return data 303 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bdist_wheel] 2 | universal = 1 3 | 4 | [metadata] 5 | description-file = README.md 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2016 Google Inc. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS-IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import os 17 | 18 | from setuptools import find_packages 19 | from setuptools import setup 20 | 21 | 22 | here = os.path.abspath(os.path.dirname(__file__)) 23 | 24 | 25 | REQUIREMENTS = [ 26 | 'google-api-python-client >= 1.2.0', 27 | 'google-apputils >= 0.4.0', 28 | ] 29 | 30 | 31 | setup( 32 | name='gav4', 33 | version='0.1.2', 34 | description='Google Analytics V4 API Compatibility Library', 35 | author='Google Analytics Platform', 36 | author_email='mcohoon+gav4-python@google.com', 37 | scripts=[], 38 | url='https://github.com/googleanalytics/gav4-python', 39 | packages=find_packages(), 40 | license='Apache 2.0', 41 | platforms='Posix; MacOS X; Windows', 42 | test_suite='nose.collector', 43 | tests_require=['nose', 'nose-cover3'], 44 | include_package_data=True, 45 | zip_safe=False, 46 | install_requires=REQUIREMENTS, 47 | classifiers=[ 48 | 'Development Status :: 1 - Planning', 49 | 'Intended Audience :: Developers', 50 | 'License :: OSI Approved :: Apache Software License', 51 | 'Operating System :: OS Independent', 52 | 'Programming Language :: Python :: 2', 53 | 'Programming Language :: Python :: 2.6', 54 | 'Programming Language :: Python :: 2.7', 55 | 'Programming Language :: Python :: 3', 56 | 'Programming Language :: Python :: 3.4', 57 | 'Topic :: Internet', 58 | ] 59 | ) 60 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | # GAV4 Tests [![Analytics](https://ga-beacon.appspot.com/UA-76561751-1/googleanalytics/gav4-python/tests?pixel)](https://github.com/googleanalytics/gav4-python) 2 | 3 | ## Testing 4 | 5 | Run the tests with the following command: 6 | 7 | python setup.py tests 8 | 9 | This may require you to install the nose library -- `pip install nose`. 10 | 11 | ## Contents 12 | 13 | ### [data.py](data.py) 14 | 15 | The `data.py` file contain requests requests, converted requests, responses, converted responses, and mocks of the service objects and its member objects. 16 | 17 | ### [test_gav4.py](test_gav4.py) 18 | 19 | The `test_gav4.py` file contains tests for the gav4 library. It includes unit tests for the individual conversion methods as well as an end-to-end integration test. 20 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | -------------------------------------------------------------------------------- /tests/data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2016 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """This file contains requests and response objects for testing purposes.""" 17 | 18 | V3_RESPONSE = { 19 | "columnHeaders": [ 20 | { 21 | "columnType": "DIMENSION", 22 | "dataType": "STRING", 23 | "name": "ga:source" 24 | }, 25 | { 26 | "columnType": "DIMENSION", 27 | "dataType": "STRING", 28 | "name": "ga:city" 29 | }, 30 | { 31 | "columnType": "METRIC", 32 | "dataType": "INTEGER", 33 | "name": "ga:sessions" 34 | }, 35 | { 36 | "columnType": "METRIC", 37 | "dataType": "INTEGER", 38 | "name": "ga:users" 39 | } 40 | ], 41 | "containsSampledData": False, 42 | "id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:90851825&dimensions=ga:source,ga:city&metrics=ga:sessions,ga:users&sort=-ga:sessions,ga:source&filters=ga:source%3D%3Dgoogle&segment=sessions::condition::ga:city!~not&start-date=2014-11-01&end-date=2014-11-30&max-results=25", 43 | "itemsPerPage": 25, 44 | "kind": "analytics#gaData", 45 | "profileInfo": { 46 | "accountId": "54535478", 47 | "internalWebPropertyId": "87498559", 48 | "profileId": "90851825", 49 | "profileName": "All Web Site Data", 50 | "tableId": "ga:90851825", 51 | "webPropertyId": "UA-54535478-1" 52 | }, 53 | "query": { 54 | "dimensions": "ga:source,ga:city", 55 | "end-date": "2014-11-30", 56 | "filters": "ga:source==google", 57 | "ids": "ga:90851825", 58 | "max-results": 25, 59 | "metrics": [ 60 | "ga:sessions", 61 | "ga:users" 62 | ], 63 | "samplingLevel": "FASTER", 64 | "segment": "sessions::condition::ga:city!~not", 65 | "sort": [ 66 | "-ga:sessions", 67 | "ga:source" 68 | ], 69 | "start-date": "2014-11-01", 70 | "start-index": 1 71 | }, 72 | "rows": [ 73 | [ 74 | "google", 75 | "Philadelphia", 76 | "60", 77 | "5" 78 | ], 79 | [ 80 | "google", 81 | "Johnstown", 82 | "21", 83 | "1" 84 | ], 85 | [ 86 | "google", 87 | "Progress", 88 | "7", 89 | "1" 90 | ], 91 | [ 92 | "google", 93 | "Scranton", 94 | "2", 95 | "1" 96 | ], 97 | [ 98 | "google", 99 | "Burgess Hill", 100 | "1", 101 | "1" 102 | ], 103 | [ 104 | "google", 105 | "Los Angeles", 106 | "1", 107 | "1" 108 | ], 109 | [ 110 | "google", 111 | "San Francisco", 112 | "1", 113 | "1" 114 | ], 115 | [ 116 | "google", 117 | "Santa Clara", 118 | "1", 119 | "1" 120 | ] 121 | ], 122 | "selfLink": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:90851825&dimensions=ga:source,ga:city&metrics=ga:sessions,ga:users&sort=-ga:sessions,ga:source&filters=ga:source%3D%3Dgoogle&segment=sessions::condition::ga:city!~not&start-date=2014-11-01&end-date=2014-11-30&max-results=25", 123 | "totalResults": 8, 124 | "totalsForAllResults": { 125 | "ga:sessions": "94", 126 | "ga:users": "12" 127 | } 128 | } 129 | 130 | 131 | V4_RESPONSE = { 132 | "reports": [ 133 | { 134 | "columnHeader": { 135 | "dimensions": [ 136 | "ga:source", 137 | "ga:city", 138 | "ga:segment" 139 | ], 140 | "metricHeader": { 141 | "metricHeaderEntries": [ 142 | { 143 | "name": "ga:sessions", 144 | "type": "INTEGER" 145 | }, 146 | { 147 | "name": "ga:users", 148 | "type": "INTEGER" 149 | } 150 | ] 151 | } 152 | }, 153 | "data": { 154 | "isDataGolden": True, 155 | "maximums": [ 156 | { 157 | "values": [ 158 | "60", 159 | "5" 160 | ] 161 | } 162 | ], 163 | "minimums": [ 164 | { 165 | "values": [ 166 | "1", 167 | "1" 168 | ] 169 | } 170 | ], 171 | "rowCount": 8, 172 | "rows": [ 173 | { 174 | "dimensions": [ 175 | "google", 176 | "Philadelphia", 177 | "Dynamic Segment" 178 | ], 179 | "metrics": [ 180 | { 181 | "values": [ 182 | "60", 183 | "5" 184 | ] 185 | } 186 | ] 187 | }, 188 | { 189 | "dimensions": [ 190 | "google", 191 | "Johnstown", 192 | "Dynamic Segment" 193 | ], 194 | "metrics": [ 195 | { 196 | "values": [ 197 | "21", 198 | "1" 199 | ] 200 | } 201 | ] 202 | }, 203 | { 204 | "dimensions": [ 205 | "google", 206 | "Progress", 207 | "Dynamic Segment" 208 | ], 209 | "metrics": [ 210 | { 211 | "values": [ 212 | "7", 213 | "1" 214 | ] 215 | } 216 | ] 217 | }, 218 | { 219 | "dimensions": [ 220 | "google", 221 | "Scranton", 222 | "Dynamic Segment" 223 | ], 224 | "metrics": [ 225 | { 226 | "values": [ 227 | "2", 228 | "1" 229 | ] 230 | } 231 | ] 232 | }, 233 | { 234 | "dimensions": [ 235 | "google", 236 | "Burgess Hill", 237 | "Dynamic Segment" 238 | ], 239 | "metrics": [ 240 | { 241 | "values": [ 242 | "1", 243 | "1" 244 | ] 245 | } 246 | ] 247 | }, 248 | { 249 | "dimensions": [ 250 | "google", 251 | "Los Angeles", 252 | "Dynamic Segment" 253 | ], 254 | "metrics": [ 255 | { 256 | "values": [ 257 | "1", 258 | "1" 259 | ] 260 | } 261 | ] 262 | }, 263 | { 264 | "dimensions": [ 265 | "google", 266 | "San Francisco", 267 | "Dynamic Segment" 268 | ], 269 | "metrics": [ 270 | { 271 | "values": [ 272 | "1", 273 | "1" 274 | ] 275 | } 276 | ] 277 | }, 278 | { 279 | "dimensions": [ 280 | "google", 281 | "Santa Clara", 282 | "Dynamic Segment" 283 | ], 284 | "metrics": [ 285 | { 286 | "values": [ 287 | "1", 288 | "1" 289 | ] 290 | } 291 | ] 292 | } 293 | ], 294 | "totals": [ 295 | { 296 | "values": [ 297 | "94", 298 | "12" 299 | ] 300 | } 301 | ] 302 | } 303 | } 304 | ] 305 | } 306 | 307 | V4_REQUEST = { 308 | "reportRequests": [ 309 | { 310 | "dateRanges": [ 311 | { 312 | "endDate": "2014-11-30", 313 | "startDate": "2014-11-01" 314 | } 315 | ], 316 | "dimensions": [ 317 | { 318 | "name": "ga:source" 319 | }, 320 | { 321 | "name": "ga:city" 322 | }, 323 | { 324 | "name": "ga:segment" 325 | } 326 | ], 327 | "filtersExpression": "ga:source==google", 328 | "metrics": [ 329 | { 330 | "expression": "ga:sessions" 331 | }, 332 | { 333 | "expression": "ga:users" 334 | } 335 | ], 336 | "order_bys": [ 337 | { 338 | "fieldName": "ga:sessions", 339 | "sortOrder": "DESCENDING", 340 | "orderType": "VALUE" 341 | }, 342 | { 343 | "fieldName": "ga:source", 344 | "orderType": "VALUE" 345 | } 346 | ], 347 | "pageSize": "25", 348 | "samplingLevel": "SMALL", 349 | "segments": [ 350 | { 351 | "segmentId": "sessions::condition::ga:city!~not" 352 | } 353 | ], 354 | "viewId": "ga:90851825" 355 | } 356 | ] 357 | } 358 | 359 | 360 | V3_CONVERTED_RESPONSE = { 361 | "columnHeaders": [ 362 | { 363 | "columnType": "DIMENSION", 364 | "dataType": "STRING", 365 | "name": "ga:source" 366 | }, 367 | { 368 | "columnType": "DIMENSION", 369 | "dataType": "STRING", 370 | "name": "ga:city" 371 | }, 372 | { 373 | "columnType": "METRIC", 374 | "dataType": "INTEGER", 375 | "name": "ga:sessions" 376 | }, 377 | { 378 | "columnType": "METRIC", 379 | "dataType": "INTEGER", 380 | "name": "ga:users" 381 | } 382 | ], 383 | "containsSampledData": False, 384 | "isDataGolden": True, 385 | "kind": "analytics#gaData", 386 | "rows": [ 387 | [ 388 | "google", 389 | "Philadelphia", 390 | "60", 391 | "5" 392 | ], 393 | [ 394 | "google", 395 | "Johnstown", 396 | "21", 397 | "1" 398 | ], 399 | [ 400 | "google", 401 | "Progress", 402 | "7", 403 | "1" 404 | ], 405 | [ 406 | "google", 407 | "Scranton", 408 | "2", 409 | "1" 410 | ], 411 | [ 412 | "google", 413 | "Burgess Hill", 414 | "1", 415 | "1" 416 | ], 417 | [ 418 | "google", 419 | "Los Angeles", 420 | "1", 421 | "1" 422 | ], 423 | [ 424 | "google", 425 | "San Francisco", 426 | "1", 427 | "1" 428 | ], 429 | [ 430 | "google", 431 | "Santa Clara", 432 | "1", 433 | "1" 434 | ] 435 | ], 436 | "totalsForAllResults": { 437 | "ga:sessions": "94", 438 | "ga:users": "12" 439 | } 440 | } 441 | 442 | 443 | ORDER_BYS = [ 444 | { 445 | "fieldName": "ga:country", 446 | "orderType": "VALUE" 447 | }, 448 | { 449 | "fieldName": "ga:browser", 450 | "sortOrder": "DESCENDING", 451 | "orderType": "VALUE" 452 | }, 453 | { 454 | "fieldName": "ga:sessions", 455 | "sortOrder": "DESCENDING", 456 | "orderType": "VALUE" 457 | } 458 | ] 459 | 460 | V3_REQUEST = { 461 | "ids": "ga:90851825", 462 | "start_date": "2014-11-01", 463 | "end_date": "2014-11-30", 464 | "metrics": "ga:sessions,ga:users", 465 | "dimensions": "ga:source,ga:city", 466 | "segment": "sessions::condition::ga:city!~not", 467 | "sort": "-ga:sessions,ga:source", 468 | "filters": "ga:source==google", 469 | "max_results": "25", 470 | "samplingLevel": "FASTER", 471 | } 472 | 473 | 474 | class BatchGet(object): 475 | """Mock class of the BatchGet Reports Request object.""" 476 | 477 | def __init__(self, body): 478 | self.body = body 479 | 480 | def execute(self): 481 | """Mock of the Batch get execute method. 482 | 483 | Returns: 484 | A v4 response object. 485 | """ 486 | return V4_RESPONSE 487 | 488 | 489 | class Reports(object): 490 | """Mock class of the Google Analytics Reports object.""" 491 | 492 | def batchGet(self, body): # pylint: disable=invalid-name 493 | """Returns a mock BatchGet Object.""" 494 | return BatchGet(body) 495 | 496 | 497 | class AnalyticsReporting(object): 498 | """Mock AnalyticsReporting service object.""" 499 | 500 | def reports(self): 501 | """Returns a mock Reports object.""" 502 | return Reports() 503 | -------------------------------------------------------------------------------- /tests/test_gav4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright 2016 Google Inc. All rights reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """Tests for the gav4 library.""" 17 | 18 | from google.apputils import basetest as googletest 19 | 20 | from gav4 import gav4 21 | from tests import data 22 | 23 | 24 | __author__ = 'mcohoon@google.com (Matthew Cohoon)' 25 | 26 | 27 | class Gav4LibTest(googletest.TestCase): 28 | 29 | def testConvertResponse(self): 30 | """Test the report conversion.""" 31 | report = data.V4_RESPONSE.get('reports', [])[0] 32 | converted_report = gav4.convert_report(report) 33 | self.assertDictContainsSubset(data.V3_CONVERTED_RESPONSE, converted_report) 34 | 35 | def testConvertDimensions(self): 36 | """Test the conversion of Dimensions.""" 37 | dimensions = 'ga:country,ga:browser' 38 | response = gav4.convert_dimensions(dimensions) 39 | reference = [{'name': 'ga:country'}, {'name': 'ga:browser'}] 40 | self.assertItemsEqual(reference, response) 41 | 42 | def testConvertMetrics(self): 43 | """Test the conversion of metrics.""" 44 | metrics = 'ga:sessions,ga:users' 45 | response = gav4.convert_metrics(metrics) 46 | reference = [{'expression': 'ga:sessions'}, {'expression': 'ga:users'}] 47 | self.assertItemsEqual(reference, response) 48 | 49 | def testConvertSegments(self): 50 | """Test the conversion of segments.""" 51 | segments = 'users::condition::ga:city==London;ga:browser==Chrome' 52 | response = gav4.convert_segments(segments) 53 | reference = [{'segmentId': segments}] 54 | self.assertEquals(response, reference) 55 | 56 | def testConvertOrderings(self): 57 | """Test the conversion of V3 sorts into V4 orderBys.""" 58 | sort = 'ga:country,-ga:browser,-ga:sessions' 59 | order_bys = gav4.convert_sorting(sort) 60 | self.assertItemsEqual(order_bys, data.ORDER_BYS) 61 | 62 | def testConvertRequest(self): 63 | """Tests the conversion of a V3 request.""" 64 | response = gav4.convert_request(**data.V3_REQUEST) 65 | self.assertDictContainsSubset(response, data.V4_REQUEST) 66 | 67 | def testGav4Get(self): 68 | """An end to end integration test of the gav4 library. 69 | 70 | This represents what the end user would do with the gav4 library. 71 | Given an authenticated service object, we apply gav4, and then 72 | make a call as if we were still using the V3 library. 73 | """ 74 | 75 | # Get the MOCK authenticated service object. 76 | analyticsreporting = data.AnalyticsReporting() 77 | 78 | # Apply the gav4 library. 79 | gav4.apply_gav4(analyticsreporting) 80 | 81 | # Call the gav4_get() method. 82 | request = analyticsreporting.gav4_get(**data.V3_REQUEST) 83 | 84 | # Call the wrapped execute method. 85 | response = request.execute() 86 | self.assertItemsEqual(response, data.V3_CONVERTED_RESPONSE) 87 | 88 | 89 | if __name__ == '__main__': 90 | googletest.main() 91 | --------------------------------------------------------------------------------