Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit a4bcc31

Browse files
authored
docs: added a sample (#7)
1 parent 86f57ce commit a4bcc31

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

samples/snippets/quickstart.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 --property_id <PROPERTY_ID>
20+
21+
where <PROPERTY_ID> is the Google Analytics property id to use for a query.
22+
23+
Note: you need to have the Google Analytics Data API enabled in your project
24+
prior to running this sample. Please visit the following URL and make sure the
25+
API is enabled:
26+
27+
https://console.developers.google.com/apis/library/analyticsdata.googleapis.com
28+
29+
This application demonstrates the usage of the Analytics Data API using
30+
service account credentials. For more information on service accounts, see
31+
32+
https://cloud.google.com/iam/docs/understanding-service-accounts
33+
34+
The following document provides instructions on setting service account
35+
credentials for your application:
36+
37+
https://cloud.google.com/docs/authentication/production
38+
39+
In a nutshell, you need to:
40+
1. Create a service account and download the key JSON file.
41+
42+
https://cloud.google.com/docs/authentication/production#creating_a_service_account
43+
44+
2. Provide service account credentials using one of the following options:
45+
- set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API
46+
client will use the value of this variable to find the service account key
47+
JSON file.
48+
49+
https://cloud.google.com/docs/authentication/production#setting_the_environment_variable
50+
51+
OR
52+
- manually pass the path to the service account key JSON file to the API client
53+
by specifying the keyFilename parameter in the constructor:
54+
https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code
55+
56+
To install the latest published package dependency, execute the following:
57+
pip install google-analytics-data
58+
"""
59+
import argparse
60+
61+
# [START ga_data_run_report]
62+
from google.analytics.data import AlphaAnalyticsDataClient
63+
from google.analytics.data_v1alpha.types import DateRange
64+
from google.analytics.data_v1alpha.types import Dimension
65+
from google.analytics.data_v1alpha.types import Entity
66+
from google.analytics.data_v1alpha.types import Metric
67+
from google.analytics.data_v1alpha.types import RunReportRequest
68+
69+
70+
def sample_run_report(property_id):
71+
"""Runs a simple report on a Google Analytics App+Web property."""
72+
73+
# Using a default constructor instructs the client to use the credentials
74+
# specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
75+
client = AlphaAnalyticsDataClient()
76+
request = RunReportRequest(entity=Entity(property_id=property_id),
77+
dimensions=[Dimension(name='city')],
78+
metrics=[Metric(name='activeUsers')],
79+
date_ranges=[DateRange(start_date='2020-03-31',
80+
end_date='today')])
81+
response = client.run_report(request)
82+
83+
print("Report result:")
84+
for row in response.rows:
85+
print(row.dimension_values[0].value, row.metric_values[0].value)
86+
87+
88+
# [END ga_data_run_report]
89+
90+
if __name__ == "__main__":
91+
parser = argparse.ArgumentParser(
92+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
93+
)
94+
parser.add_argument(
95+
"--property_id",
96+
type=str,
97+
required=True,
98+
help="Google Analytics property ID to use for a query.",
99+
)
100+
args = parser.parse_args()
101+
sample_run_report(args.property_id)

samples/snippets/quickstart_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2020 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import quickstart
16+
17+
18+
def test_quickstart(capsys):
19+
TEST_PROPERTY_ID = '222596558'
20+
quickstart.sample_run_report(TEST_PROPERTY_ID)
21+
out, _ = capsys.readouterr()
22+
assert "Report result" in out

0 commit comments

Comments
 (0)