|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2020 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 | +"""Google Analytics Data API sample quickstart application. |
| 18 | +Example usage: |
| 19 | + python quickstart.py |
| 20 | +
|
| 21 | +This application demonstrates the usage of the Analytics Data API using |
| 22 | +service account credentials. For more information on service accounts, see |
| 23 | +
|
| 24 | +https://cloud.google.com/iam/docs/understanding-service-accounts |
| 25 | +
|
| 26 | +The following document provides instructions on setting service account |
| 27 | +credentials for your application: |
| 28 | +
|
| 29 | + https://cloud.google.com/docs/authentication/production |
| 30 | +
|
| 31 | +In a nutshell, you need to: |
| 32 | +1. Create a service account and download the key JSON file. |
| 33 | +
|
| 34 | +https://cloud.google.com/docs/authentication/production#creating_a_service_account |
| 35 | +
|
| 36 | +2. Provide service account credentials using one of the following options: |
| 37 | +- set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API |
| 38 | +client will use the value of this variable to find the service account key |
| 39 | +JSON file. |
| 40 | +
|
| 41 | +https://cloud.google.com/docs/authentication/production#setting_the_environment_variable |
| 42 | +
|
| 43 | +OR |
| 44 | +- manually pass the path to the service account key JSON file to the API client |
| 45 | +by specifying the keyFilename parameter in the constructor: |
| 46 | +https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code |
| 47 | +
|
| 48 | +To install the latest published package dependency, execute the following: |
| 49 | + pip install google-analytics-data |
| 50 | +""" |
| 51 | + |
| 52 | +# [START ga_data_run_report] |
| 53 | +def run_report(): |
| 54 | + """Runs a simple report on a Google Analytics App+Web property.""" |
| 55 | + from google.analytics.data import AlphaAnalyticsDataAsyncClient |
| 56 | + from google.analytics.data_v1alpha.types import Entity |
| 57 | + from google.analytics.data_v1alpha.types import Dimension |
| 58 | + from google.analytics.data_v1alpha.types import Metric |
| 59 | + from google.analytics.data_v1alpha.types import DateRange |
| 60 | + |
| 61 | + # Using a default constructor instructs the client to use the credentials |
| 62 | + # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. |
| 63 | + client = AlphaAnalyticsDataAsyncClient() |
| 64 | + response = client.run_report( |
| 65 | + Entity(property_id='APP+WEB_PROPERTY_ID'), |
| 66 | + [Dimension(name='city')], |
| 67 | + [Metric(name='activeUsers')], |
| 68 | + [DateRange(start_date='2020-03-31', end_date='today')]) |
| 69 | + |
| 70 | + for row in response.rows: |
| 71 | + print(row.dimension_values[0].value, row.metric_values[0].value) |
| 72 | +# [END ga_data_run_report] |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + run_report() |
| 76 | + |
0 commit comments