This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
docs: update quickstart samples to support the Data API v1 beta #50
Merged
Merged
Changes from 20 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
fc5a4d6
docs: added a sample
ikuleshov bfd3488
Merge branch 'master' into samples
ikuleshov 0a74ac1
docs: usage instructions updated to use Python3
ikuleshov 7f709d3
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 4156c32
docs: updated sample to include main() method
ikuleshov a4be69d
Merge branch 'master' into samples
ikuleshov b59f40d
docs: update the sample to support the Google Analytics Data API v1 beta
ikuleshov fc29aa0
docs: update quickstart samples to support the Data API v1 beta
ikuleshov df8b945
docs: update quickstart samples to support the Data API v1 beta
ikuleshov 745bb31
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 937aa44
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov d8a2803
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov f86c4d5
erge remote-tracking branch 'origin/master' into samples
ikuleshov 0b51431
Merge remote-tracking branch 'origin/master' into samples
ikuleshov 5555c63
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov ca55dfd
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 1fe6db5
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 050994a
separate the sample into separate methods to facilitate testing
ikuleshov c13340f
separate the sample into separate methods to facilitate testing
ikuleshov 3d483d2
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 49a7ac7
fix: update formatting
ikuleshov 4d0a023
fix: update formatting
ikuleshov a775e58
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov bf541c1
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov d6b0b2c
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov e07dd3d
add noxfile_config with a test property id value
ikuleshov 618d2e7
add noxfile_config with a test property id value
ikuleshov f17deba
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 2967025
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 6b3d945
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 2b446af
fix: use the credentials json file provided during the test
ikuleshov 7887b79
fix: use the credentials json file provided during the test
ikuleshov a3edcea
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov 170e7ff
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov e5cd787
Merge branch 'samples' of github.com:ikuleshov/python-analytics-data …
ikuleshov File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Google Analytics Data API sample quickstart application. | ||
|
||
This application demonstrates the usage of the Analytics Data API using | ||
service account credentials from a JSON file downloaded from | ||
the Google Cloud Console. | ||
|
||
Before you start the application, please review the comments starting with | ||
"TODO(developer)" and update the code to use correct values. | ||
|
||
Usage: | ||
pip3 install --upgrade google-analytics-data | ||
python3 quickstart_json_credentials.py | ||
""" | ||
# [START google_analytics_data_quickstart] | ||
from google.analytics.data_v1beta import BetaAnalyticsDataClient | ||
from google.analytics.data_v1beta.types import DateRange | ||
from google.analytics.data_v1beta.types import Dimension | ||
from google.analytics.data_v1beta.types import Metric | ||
from google.analytics.data_v1beta.types import RunReportRequest | ||
|
||
def get_client(credentials_json_path=''): | ||
"""Creates an API client instance.""" | ||
# [START google_analytics_data_initialize] | ||
# TODO(developer): Uncomment this variable and replace with a valid path to | ||
# the credentials.json file for your service account downloaded from the | ||
# Cloud Console. | ||
#credentials_json_path = '/path/to/credentials.json' | ||
|
||
# Explicitly use service account credentials by specifying | ||
# the private key file. | ||
return BetaAnalyticsDataClient().from_service_account_json( | ||
ikuleshov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
credentials_json_path) | ||
# [END google_analytics_data_initialize] | ||
|
||
def sample_run_report(client, property_id='YOUR-GA4-PROPERTY-ID'): | ||
"""Runs a simple report on a Google Analytics 4 property.""" | ||
# TODO(developer): Uncomment this variable and replace with your | ||
# Google Analytics 4 property ID before running the sample. | ||
#property_id = 'YOUR-GA4-PROPERTY-ID' | ||
|
||
# [START google_analytics_data_run_report] | ||
request = RunReportRequest(property='properties/' + str(property_id), | ||
dimensions=[Dimension(name='city')], | ||
metrics=[Metric(name='activeUsers')], | ||
date_ranges=[DateRange(start_date='2020-03-31', | ||
end_date='today')]) | ||
response = client.run_report(request) | ||
# [END google_analytics_data_run_report] | ||
|
||
print("Report result:") | ||
for row in response.rows: | ||
print(row.dimension_values[0].value, row.metric_values[0].value) | ||
|
||
def main(): | ||
client = get_client() | ||
sample_run_report(client) | ||
# [END google_analytics_data_quickstart] | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2020 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.analytics.data_v1beta import BetaAnalyticsDataClient | ||
import quickstart_json_credentials | ||
|
||
|
||
def test_quickstart(capsys): | ||
TEST_PROPERTY_ID = '222596558' | ||
ikuleshov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
quickstart_json_credentials.sample_run_report(BetaAnalyticsDataClient(), | ||
TEST_PROPERTY_ID) | ||
out, _ = capsys.readouterr() | ||
assert "Report result" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Google Analytics Data API sample quickstart application. | ||
|
||
This application demonstrates the usage of the Analytics Data API using | ||
OAuth2 credentials. | ||
|
||
Before you start the application, please review the comments starting with | ||
"TODO(developer)" and update the code to use correct values. | ||
|
||
Usage: | ||
pip3 install --upgrade google-auth-oauthlib | ||
pip3 install --upgrade google-analytics-data | ||
python3 quickstart_oauth2.py | ||
""" | ||
# [START google_analytics_data_quickstart] | ||
from google_auth_oauthlib import flow | ||
|
||
from google.analytics.data import BetaAnalyticsDataClient | ||
from google.analytics.data_v1beta.types import DateRange | ||
from google.analytics.data_v1beta.types import Dimension | ||
from google.analytics.data_v1beta.types import Metric | ||
from google.analytics.data_v1beta.types import RunReportRequest | ||
|
||
|
||
def sample_run_report(credentials=None, property_id='YOUR-GA4-PROPERTY-ID'): | ||
"""Runs a simple report on a Google Analytics 4 property.""" | ||
# TODO(developer): Uncomment this variable and replace with your | ||
# Google Analytics 4 property ID before running the sample. | ||
#property_id = 'YOUR-GA4-PROPERTY-ID' | ||
|
||
client = BetaAnalyticsDataClient(credentials=credentials) | ||
request = RunReportRequest(property='properties/' + str(property_id), | ||
dimensions=[Dimension(name='city')], | ||
metrics=[Metric(name='activeUsers')], | ||
date_ranges=[DateRange(start_date='2020-03-31', | ||
end_date='today')]) | ||
|
||
response = client.run_report(request) | ||
|
||
print("Report result:") | ||
for row in response.rows: | ||
print(row.dimension_values[0].value, row.metric_values[0].value) | ||
|
||
def get_credentials(): | ||
"""Creates an OAuth2 credentials instance.""" | ||
# [START google_analytics_data_initialize] | ||
appflow = flow.InstalledAppFlow.from_client_secrets_file( | ||
"client_secrets.json", | ||
scopes=["https://www.googleapis.com/auth/analytics.readonly"] | ||
) | ||
# TODO(developer): Update the line below to set the `launch_browser` variable. | ||
# The `launch_browser` boolean variable indicates if a local server is used | ||
# as the callback URL in the auth flow. A value of `True` is recommended, | ||
# but a local server does not work if accessing the application remotely, | ||
# such as over SSH or from a remote Jupyter notebook. | ||
launch_browser = True | ||
if launch_browser: | ||
appflow.run_local_server() | ||
else: | ||
appflow.run_console() | ||
return appflow.credentials | ||
# [END google_analytics_data_initialize] | ||
|
||
def main(): | ||
credentials = get_credentials() | ||
sample_run_report(credentials) | ||
# [END google_analytics_data_quickstart] | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2020 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import quickstart_oauth2 | ||
|
||
|
||
def test_quickstart(capsys): | ||
TEST_PROPERTY_ID = '222596558' | ||
quickstart_oauth2.sample_run_report(None, TEST_PROPERTY_ID) | ||
out, _ = capsys.readouterr() | ||
assert "Report result" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
google-analytics-data==0.4.0 | ||
google-auth-oauthlib==0.4.3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We're supposed to leave the copyright alone unless it's a new file