├── .github └── renovate.json5 ├── .gitignore ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── google-analytics-admin ├── README.md ├── account_summaries_list.py ├── account_summaries_list_test.py ├── accounts_access_bindings_batch_create.py ├── accounts_access_bindings_batch_create_test.py ├── accounts_access_bindings_batch_delete.py ├── accounts_access_bindings_batch_delete_test.py ├── accounts_access_bindings_batch_get.py ├── accounts_access_bindings_batch_get_test.py ├── accounts_access_bindings_batch_update.py ├── accounts_access_bindings_batch_update_test.py ├── accounts_access_bindings_create.py ├── accounts_access_bindings_create_test.py ├── accounts_access_bindings_delete.py ├── accounts_access_bindings_delete_test.py ├── accounts_access_bindings_get.py ├── accounts_access_bindings_get_test.py ├── accounts_access_bindings_list.py ├── accounts_access_bindings_list_test.py ├── accounts_access_bindings_update.py ├── accounts_access_bindings_update_test.py ├── accounts_delete.py ├── accounts_delete_test.py ├── accounts_get.py ├── accounts_get_data_sharing_settings.py ├── accounts_get_data_sharing_settings_test.py ├── accounts_get_test.py ├── accounts_list.py ├── accounts_list_test.py ├── accounts_provision_account_ticket.py ├── accounts_provision_account_ticket_test.py ├── accounts_search_change_history_events.py ├── accounts_search_change_history_events_test.py ├── accounts_update.py ├── accounts_update_test.py ├── noxfile.py ├── noxfile_config.py ├── properties_access_bindings_batch_create.py ├── properties_access_bindings_batch_create_test.py ├── properties_access_bindings_batch_delete.py ├── properties_access_bindings_batch_delete_test.py ├── properties_access_bindings_batch_get.py ├── properties_access_bindings_batch_get_test.py ├── properties_access_bindings_batch_update.py ├── properties_access_bindings_batch_update_test.py ├── properties_access_bindings_create.py ├── properties_access_bindings_create_test.py ├── properties_access_bindings_delete.py ├── properties_access_bindings_delete_test.py ├── properties_access_bindings_get.py ├── properties_access_bindings_get_test.py ├── properties_access_bindings_list.py ├── properties_access_bindings_list_test.py ├── properties_access_bindings_update.py ├── properties_access_bindings_update_test.py ├── properties_create.py ├── properties_create_test.py ├── properties_data_streams_create.py ├── properties_data_streams_create_test.py ├── properties_data_streams_delete.py ├── properties_data_streams_delete_test.py ├── properties_data_streams_get.py ├── properties_data_streams_get_global_site_tag.py ├── properties_data_streams_get_global_site_tag_test.py ├── properties_data_streams_get_test.py ├── properties_data_streams_list.py ├── properties_data_streams_list_test.py ├── properties_data_streams_measurement_protocol_secrets_create.py ├── properties_data_streams_measurement_protocol_secrets_create_test.py ├── properties_data_streams_measurement_protocol_secrets_delete.py ├── properties_data_streams_measurement_protocol_secrets_delete_test.py ├── properties_data_streams_measurement_protocol_secrets_get.py ├── properties_data_streams_measurement_protocol_secrets_get_test.py ├── properties_data_streams_measurement_protocol_secrets_list.py ├── properties_data_streams_measurement_protocol_secrets_list_test.py ├── properties_data_streams_measurement_protocol_secrets_update.py ├── properties_data_streams_measurement_protocol_secrets_update_test.py ├── properties_data_streams_update.py ├── properties_data_streams_update_test.py ├── properties_delete.py ├── properties_delete_test.py ├── properties_firebase_links_create.py ├── properties_firebase_links_create_test.py ├── properties_firebase_links_delete.py ├── properties_firebase_links_delete_test.py ├── properties_firebase_links_list.py ├── properties_firebase_links_list_test.py ├── properties_get.py ├── properties_get_test.py ├── properties_google_ads_links_create.py ├── properties_google_ads_links_create_test.py ├── properties_google_ads_links_delete.py ├── properties_google_ads_links_delete_test.py ├── properties_google_ads_links_list.py ├── properties_google_ads_links_list_test.py ├── properties_google_ads_links_update.py ├── properties_google_ads_links_update_test.py ├── properties_key_events_create.py ├── properties_key_events_create_test.py ├── properties_key_events_delete.py ├── properties_key_events_delete_test.py ├── properties_key_events_get.py ├── properties_key_events_get_test.py ├── properties_key_events_list.py ├── properties_key_events_list_test.py ├── properties_list.py ├── properties_list_test.py ├── properties_run_access_report.py ├── properties_run_access_report_test.py ├── properties_update.py ├── properties_update_test.py ├── quickstart.py ├── quickstart_test.py ├── requirements-test.txt └── requirements.txt └── google-analytics-data ├── README.md ├── get_common_metadata.py ├── get_common_metadata_test.py ├── get_metadata_by_property_id.py ├── get_metadata_by_property_id_test.py ├── noxfile.py ├── noxfile_config.py ├── quickstart.py ├── quickstart_json_credentials.py ├── quickstart_json_credentials_test.py ├── quickstart_oauth2.py ├── quickstart_oauth2_test.py ├── quickstart_test.py ├── requirements-test.txt ├── requirements.txt ├── run_batch_report.py ├── run_batch_report_test.py ├── run_funnel_report.py ├── run_funnel_report_test.py ├── run_pivot_report.py ├── run_pivot_report_test.py ├── run_realtime_report.py ├── run_realtime_report_test.py ├── run_realtime_report_with_minute_ranges.py ├── run_realtime_report_with_minute_ranges_test.py ├── run_realtime_report_with_multiple_dimensions.py ├── run_realtime_report_with_multiple_dimensions_test.py ├── run_realtime_report_with_multiple_metrics.py ├── run_realtime_report_with_multiple_metrics_test.py ├── run_report.py ├── run_report_test.py ├── run_report_with_aggregations.py ├── run_report_with_aggregations_test.py ├── run_report_with_cohorts.py ├── run_report_with_cohorts_test.py ├── run_report_with_date_ranges.py ├── run_report_with_date_ranges_test.py ├── run_report_with_dimension_and_metric_filters.py ├── run_report_with_dimension_and_metric_filters_test.py ├── run_report_with_dimension_exclude_filter.py ├── run_report_with_dimension_exclude_filter_test.py ├── run_report_with_dimension_filter.py ├── run_report_with_dimension_filter_test.py ├── run_report_with_dimension_in_list_filter.py ├── run_report_with_dimension_in_list_filter_test.py ├── run_report_with_multiple_dimension_filters.py ├── run_report_with_multiple_dimension_filters_test.py ├── run_report_with_multiple_dimensions.py ├── run_report_with_multiple_dimensions_test.py ├── run_report_with_multiple_metrics.py ├── run_report_with_multiple_metrics_test.py ├── run_report_with_named_date_ranges.py ├── run_report_with_named_date_ranges_test.py ├── run_report_with_ordering.py ├── run_report_with_ordering_test.py ├── run_report_with_pagination.py ├── run_report_with_pagination_test.py ├── run_report_with_property_quota.py └── run_report_with_property_quota_test.py /.github/renovate.json5: -------------------------------------------------------------------------------- 1 | // Based partially on https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/renovate.json 2 | { 3 | "extends": [ 4 | // https://docs.renovatebot.com/presets-config/#configrecommended 5 | "config:recommended", 6 | "schedule:earlyMondays", 7 | ":ignoreUnstable", 8 | ], 9 | // Apply label to PRs. 10 | "labels": [ 11 | "dependencies", 12 | ], 13 | // https://docs.renovatebot.com/configuration-options/#minimumreleaseage 14 | "minimumReleaseAge": "3 days", 15 | "dependencyDashboardLabels": [ 16 | "type: process", 17 | ], 18 | "rebaseWhen": "behind-base-branch", 19 | "semanticCommits": "enabled", 20 | // Create PR for vulnerability alerts immediately. 21 | "vulnerabilityAlerts": { 22 | "labels": [ 23 | "security" 24 | ], 25 | "minimumReleaseAge": null 26 | }, 27 | } 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | *.sw[op] 3 | 4 | # C extensions 5 | *.so 6 | 7 | # Packages 8 | *.egg 9 | *.egg-info 10 | dist 11 | build 12 | eggs 13 | .eggs 14 | parts 15 | bin 16 | var 17 | sdist 18 | develop-eggs 19 | .installed.cfg 20 | lib 21 | lib64 22 | __pycache__ 23 | 24 | # Installer logs 25 | pip-log.txt 26 | 27 | # Unit test / coverage reports 28 | .coverage 29 | .nox 30 | .cache 31 | .pytest_cache 32 | 33 | 34 | # Mac 35 | .DS_Store 36 | 37 | # JetBrains 38 | .idea 39 | 40 | # VS Code 41 | .vscode 42 | 43 | # emacs 44 | *~ 45 | 46 | # Built documentation 47 | docs/_build 48 | docs.metadata 49 | 50 | # Virtual environment 51 | env/ 52 | venv/ 53 | 54 | # Test logs 55 | coverage.xml 56 | *sponge_log.xml 57 | 58 | # System test environment variables. 59 | system_tests/local_test_setup 60 | 61 | # Make sure a generated file isn't accidentally committed. 62 | pylintrc 63 | pylintrc.test 64 | 65 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | @googleanalytics/google-analytics 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, 4 | and in the interest of fostering an open and welcoming community, 5 | we pledge to respect all people who contribute through reporting issues, 6 | posting feature requests, updating documentation, 7 | submitting pull requests or patches, and other activities. 8 | 9 | We are committed to making participation in this project 10 | a harassment-free experience for everyone, 11 | regardless of level of experience, gender, gender identity and expression, 12 | sexual orientation, disability, personal appearance, 13 | body size, race, ethnicity, age, religion, or nationality. 14 | 15 | Examples of unacceptable behavior by participants include: 16 | 17 | * The use of sexualized language or imagery 18 | * Personal attacks 19 | * Trolling or insulting/derogatory comments 20 | * Public or private harassment 21 | * Publishing other's private information, 22 | such as physical or electronic 23 | addresses, without explicit permission 24 | * Other unethical or unprofessional conduct. 25 | 26 | Project maintainers have the right and responsibility to remove, edit, or reject 27 | comments, commits, code, wiki edits, issues, and other contributions 28 | that are not aligned to this Code of Conduct. 29 | By adopting this Code of Conduct, 30 | project maintainers commit themselves to fairly and consistently 31 | applying these principles to every aspect of managing this project. 32 | Project maintainers who do not follow or enforce the Code of Conduct 33 | may be permanently removed from the project team. 34 | 35 | This code of conduct applies both within project spaces and in public spaces 36 | when an individual is representing the project or its community. 37 | 38 | Instances of abusive, harassing, or otherwise unacceptable behavior 39 | may be reported by opening an issue 40 | or contacting one or more of the project maintainers. 41 | 42 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, 43 | available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Analytics Python Samples 2 | 3 | Python samples for [Google Analytics APIs][ga]. 4 | 5 | Check out the `README.md` in one of the following directories to get started: 6 | 7 | - Admin API: [README.md](google-analytics-admin/README.md) 8 | - Data API: [README.md](google-analytics-data/README.md) 9 | 10 | ## Contributing 11 | 12 | Contributions welcome! See the [Contributing Guide](CONTRIBUTING.md). 13 | 14 | [ga]: https://developers.google.com/analytics 15 | 16 | -------------------------------------------------------------------------------- /google-analytics-admin/README.md: -------------------------------------------------------------------------------- 1 | # Google Analytics Admin API examples 2 | 3 | [![Open in Cloud Shell][shell_img]][shell_link] 4 | 5 | [shell_img]: http://gstatic.com/cloudssh/images/open-btn.png 6 | [shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleanalytics/python-docs-samples&page=editor&open_in_editor=/google-analytics-admin/README.md 7 | 8 | These samples show how to use the 9 | [Google Analytics Admin API](https://developers.google.com/analytics/devguides/config/admin/v1) from Python. 10 | 11 | ## Build and Run 12 | 13 | 1. **Enable APIs** - [Enable the Analytics Admin API](https://console.cloud.google.com/flows/enableapi?apiid=analyticsadmin.googleapis.com) 14 | and create a new project or select an existing project. 15 | 2. **Download The Credentials** - Configure your project using [Application Default Credentials][adc]. 16 | Click "Go to credentials" after enabling the APIs. Click "Create Credentials" 17 | and select "Service Account Credentials" and download the credentials file. Then set the path to 18 | this file to the environment variable `GOOGLE_APPLICATION_CREDENTIALS`: 19 | ```sh 20 | export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json 21 | ``` 22 | 3. **Clone the repo** and cd into this directory 23 | ```sh 24 | git clone https://github.com/googleanalytics/python-docs-samples 25 | cd python-docs-samples/google-analytics-admin 26 | ``` 27 | 4. **Install dependencies** via [pip3](https://pip.pypa.io/en/stable). 28 | ```sh 29 | pip3 install --upgrade -r requirements.txt 30 | ``` 31 | 5. **Review the comments starting with `TODO(developer)` and update the code 32 | to use correct values.** 33 | 6. **Run** with the command `python3 SNIPPET_NAME.py`. For example: 34 | ```sh 35 | python3 quickstart.py 36 | ``` 37 | 38 | [adc]: https://cloud.google.com/docs/authentication#adc 39 | -------------------------------------------------------------------------------- /google-analytics-admin/account_summaries_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints summaries of 18 | all accounts accessible by the caller. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accountSummaries/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_account_summaries_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def list_account_summaries(transport: str = None) -> None: 28 | """ 29 | Prints summaries of all accounts accessible by the caller. 30 | 31 | Args: 32 | transport(str): The transport to use. For example, "grpc" 33 | or "rest". If set to None, a transport is chosen automatically. 34 | """ 35 | client = AnalyticsAdminServiceClient(transport=transport) 36 | results = client.list_account_summaries() 37 | 38 | print("Result:") 39 | for account_summary in results: 40 | print("-- Account --") 41 | print(f"Resource name: {account_summary.name}") 42 | print(f"Account name: {account_summary.account}") 43 | print(f"Display name: {account_summary.display_name}") 44 | print() 45 | for property_summary in account_summary.property_summaries: 46 | print("-- Property --") 47 | print(f"Property resource name: {property_summary.property}") 48 | print(f"Property display name: {property_summary.display_name}") 49 | print() 50 | 51 | 52 | # [END analyticsadmin_account_summaries_list] 53 | 54 | 55 | if __name__ == "__main__": 56 | list_account_summaries() 57 | -------------------------------------------------------------------------------- /google-analytics-admin/account_summaries_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 account_summaries_list 16 | 17 | 18 | def test_account_summaries_list(capsys): 19 | transports = ["grpc", "rest"] 20 | for transport in transports: 21 | account_summaries_list.list_account_summaries(transport=transport) 22 | out, _ = capsys.readouterr() 23 | assert "Result" in out 24 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_batch_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_batch_create 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_EMAIL_ADDRESS = "FAKE_EMAIL_ADDRESS" 21 | 22 | 23 | def test_accounts_access_bindings_batch_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_access_bindings_batch_create.batch_create_account_access_binding( 30 | FAKE_ACCOUNT_ID, FAKE_EMAIL_ADDRESS, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_batch_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_batch_delete 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_ACCOUNT_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_accounts_access_bindings_batch_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_access_bindings_batch_delete.batch_delete_account_access_binding( 30 | FAKE_ACCOUNT_ID, FAKE_ACCOUNT_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_batch_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_access_bindings_batch_get 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | TEST_ACCOUNT_ACCESS_BINDING_ID = os.getenv("GA_TEST_ACCOUNT_ACCESS_BINDING_ID") 21 | 22 | 23 | def test_accounts_access_bindings_batch_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | accounts_access_bindings_batch_get.batch_get_account_access_binding( 27 | TEST_ACCOUNT_ID, TEST_ACCOUNT_ACCESS_BINDING_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_batch_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_batch_update 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_ACCOUNT_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_accounts_access_bindings_batch_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_access_bindings_batch_update.batch_update_account_access_binding( 30 | FAKE_ACCOUNT_ID, FAKE_ACCOUNT_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_create 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_EMAIL_ADDRESS = "FAKE_EMAIL_ADDRESS" 21 | 22 | 23 | def test_accounts_access_bindings_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_access_bindings_create.create_account_access_binding( 30 | FAKE_ACCOUNT_ID, FAKE_EMAIL_ADDRESS, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_delete 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_ACCOUNT_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_accounts_access_bindings_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_access_bindings_delete.delete_account_access_binding( 30 | FAKE_ACCOUNT_ID, FAKE_ACCOUNT_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_get.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the account user 18 | link details. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/get 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_access_bindings_get] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 30 | # account ID (e.g. "123456") before running the sample. 31 | account_id = "YOUR-GA-ACCOUNT-ID" 32 | 33 | # TODO(developer): Replace this variable with your Google Analytics 34 | # account access binding ID (e.g. "123456") before running the sample. 35 | account_access_binding_id = "YOUR-ACCOUNT-ACCESS-BINDING-ID" 36 | 37 | get_account_access_binding(account_id, account_access_binding_id) 38 | 39 | 40 | def get_account_access_binding( 41 | account_id: str, account_access_binding_id: str, transport: str = None 42 | ): 43 | """ 44 | Retrieves the account access binding details. 45 | 46 | Args: 47 | account_id(str): The Google Analytics Account ID. 48 | account_access_binding_id(str): Google Analytics account access binding ID. 49 | transport(str): The transport to use. For example, "grpc" 50 | or "rest". If set to None, a transport is chosen automatically. 51 | """ 52 | client = AnalyticsAdminServiceClient(transport=transport) 53 | access_binding = client.get_access_binding( 54 | name=f"accounts/{account_id}/accessBindings/{account_access_binding_id}" 55 | ) 56 | 57 | print("Result:") 58 | print_access_binding(access_binding) 59 | 60 | 61 | def print_access_binding(access_binding): 62 | """Prints the access binding details.""" 63 | print(f"Resource name: {access_binding.name}") 64 | print(f"User: {access_binding.user}") 65 | for role in access_binding.roles: 66 | print(f"Role: {role}") 67 | 68 | 69 | # [END analyticsadmin_accounts_access_bindings_get] 70 | 71 | 72 | if __name__ == "__main__": 73 | run_sample() 74 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_access_bindings_get 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | TEST_ACCOUNT_ACCESS_BINDING_ID = os.getenv("GA_TEST_ACCOUNT_ACCESS_BINDING_ID") 21 | 22 | 23 | def test_accounts_access_bindings_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | accounts_access_bindings_get.get_account_access_binding( 27 | TEST_ACCOUNT_ID, TEST_ACCOUNT_ACCESS_BINDING_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints access bindings 18 | under the specified parent account. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_access_bindings_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 30 | # account ID (e.g. "123456") before running the sample. 31 | account_id = "YOUR-GA-ACCOUNT-ID" 32 | list_account_access_bindings(account_id) 33 | 34 | 35 | def list_account_access_bindings(account_id: str, transport: str = None): 36 | """ 37 | Lists access bindings under the specified parent account. 38 | 39 | Args: 40 | account_id(str): The id of the account. 41 | transport(str): The transport to use. For example, "grpc" 42 | or "rest". If set to None, a transport is chosen automatically. 43 | """ 44 | client = AnalyticsAdminServiceClient(transport=transport) 45 | results = client.list_access_bindings(parent=f"accounts/{account_id}") 46 | 47 | print("Result:") 48 | for access_binding in results: 49 | print(access_binding) 50 | print() 51 | 52 | 53 | # [END analyticsadmin_accounts_access_bindings_list] 54 | 55 | 56 | if __name__ == "__main__": 57 | run_sample() 58 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_access_bindings_list 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | 21 | 22 | def test_accounts_access_bindings_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | accounts_access_bindings_list.list_account_access_bindings( 26 | TEST_ACCOUNT_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_access_bindings_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_access_bindings_update 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | FAKE_ACCOUNT_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_accounts_access_bindings_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises( 29 | Exception, 30 | match="(The caller does not have permission|Invalid access binding name)", 31 | ): 32 | accounts_access_bindings_update.update_account_access_binding( 33 | FAKE_ACCOUNT_ID, FAKE_ACCOUNT_ACCESS_BINDING_ID, transport=transport 34 | ) 35 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which deletes a Google 18 | Analytics account. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/delete 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_delete] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # !!! ATTENTION !!! 31 | # Running this sample may change/delete your Google Analytics account 32 | # configuration. Make sure to not use the Google Analytics account ID from 33 | # your production environment below. 34 | 35 | # TODO(developer): Replace this variable with your Google Analytics 36 | # account ID (e.g. "123456") before running the sample. 37 | account_id = "YOUR-GA-ACCOUNT-ID" 38 | delete_account(account_id) 39 | 40 | 41 | def delete_account(account_id: str, transport: str = None): 42 | """ 43 | Deletes the Google Analytics account. 44 | 45 | Args: 46 | account_id(str): The id of the account to be deleted. 47 | transport(str): The transport to use. For example, "grpc" 48 | or "rest". If set to None, a transport is chosen automatically. 49 | """ 50 | client = AnalyticsAdminServiceClient(transport=transport) 51 | client.delete_account(name=f"accounts/{account_id}") 52 | print("Account deleted") 53 | 54 | 55 | # [END analyticsadmin_accounts_delete] 56 | 57 | 58 | if __name__ == "__main__": 59 | run_sample() 60 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_delete 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | 21 | 22 | def test_accounts_delete(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server. No 26 | # account is being deleted during the test as it is not trivial to 27 | # provision a new account for testing. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | accounts_delete.delete_account(FAKE_ACCOUNT_ID, transport=transport) 30 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_get.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the Google 18 | Analytics account data. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/get 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_get] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 30 | # account ID (e.g. "123456") before running the sample. 31 | account_id = "YOUR-GA-ACCOUNT-ID" 32 | get_account(account_id) 33 | 34 | 35 | def get_account(account_id: str, transport: str = None): 36 | """ 37 | Retrieves the Google Analytics account data. 38 | Args: 39 | account_id(str): The id of the account. 40 | transport(str): The transport to use. For example, "grpc" 41 | or "rest". If set to None, a transport is chosen automatically. 42 | """ 43 | client = AnalyticsAdminServiceClient(transport=transport) 44 | account = client.get_account(name=f"accounts/{account_id}") 45 | 46 | print("Result:") 47 | print_account(account) 48 | 49 | 50 | def print_account(account: str): 51 | """Prints account data.""" 52 | print(f"Resource name: {account.name}") 53 | print(f"Display name: {account.display_name}") 54 | print(f"Region code: {account.region_code}") 55 | print(f"Create time: {account.create_time}") 56 | print(f"Update time: {account.update_time}") 57 | 58 | 59 | # [END analyticsadmin_accounts_get] 60 | 61 | 62 | if __name__ == "__main__": 63 | run_sample() 64 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_get_data_sharing_settings.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the data sharing 18 | settings on an account. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/getDataSharingSettings 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_get_data_sharing_settings] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # TODO(developer): Replace this variable with your Google Analytics 31 | # account ID (e.g. "123456") before running the sample. 32 | account_id = "YOUR-GA-ACCOUNT-ID" 33 | get_data_sharing_settings(account_id) 34 | 35 | 36 | def get_data_sharing_settings(account_id: str, transport=None): 37 | """ 38 | Gets data sharing settings on an account. 39 | Args: 40 | account_id(str): The id of the account. 41 | transport(str): The transport to use. For example, "grpc" 42 | or "rest". If set to None, a transport is chosen automatically. 43 | """ 44 | client = AnalyticsAdminServiceClient(transport=transport) 45 | data_sharing_settings = client.get_data_sharing_settings( 46 | name=f"accounts/{account_id}/dataSharingSettings" 47 | ) 48 | 49 | print("Result:") 50 | print(f"Resource name: {data_sharing_settings.name}") 51 | print( 52 | f"Sharing with Google support enabled: {data_sharing_settings.sharing_with_google_support_enabled}" 53 | ) 54 | print( 55 | f"Sharing with Google assigned sales enabled: {data_sharing_settings.sharing_with_google_assigned_sales_enabled}" 56 | ) 57 | print( 58 | f"Sharing with others enabled: {data_sharing_settings.sharing_with_others_enabled}" 59 | ) 60 | 61 | 62 | # [END analyticsadmin_accounts_get_data_sharing_settings] 63 | 64 | 65 | if __name__ == "__main__": 66 | run_sample() 67 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_get_data_sharing_settings_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_get_data_sharing_settings 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | 21 | 22 | def test_accounts_get_data_sharing_settings(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | accounts_get_data_sharing_settings.get_data_sharing_settings( 26 | TEST_ACCOUNT_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_get 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | 21 | 22 | def test_accounts_get(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | accounts_get.get_account(TEST_ACCOUNT_ID, transport=transport) 26 | out, _ = capsys.readouterr() 27 | assert "Result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the Google 18 | Analytics accounts available to the current user. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | from accounts_get import print_account 27 | 28 | 29 | def list_accounts(transport: str = None): 30 | """ 31 | Lists the Google Analytics accounts available to the current user. 32 | 33 | Args: 34 | transport(str): The transport to use. For example, "grpc" 35 | or "rest". If set to None, a transport is chosen automatically. 36 | """ 37 | client = AnalyticsAdminServiceClient(transport=transport) 38 | results = client.list_accounts() 39 | 40 | print("Result:") 41 | for account in results: 42 | print_account(account) 43 | 44 | 45 | # [END analyticsadmin_accounts_list] 46 | 47 | 48 | if __name__ == "__main__": 49 | list_accounts() 50 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 accounts_list 16 | 17 | 18 | def test_accounts_list(capsys): 19 | transports = ["grpc", "rest"] 20 | for transport in transports: 21 | accounts_list.list_accounts(transport=transport) 22 | out, _ = capsys.readouterr() 23 | assert "Result" in out 24 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_provision_account_ticket_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 accounts_provision_account_ticket 16 | 17 | TEST_REDIRECT_URL = "https://www.google.com" 18 | 19 | 20 | def test_accounts_provision_account_ticket(capsys): 21 | transports = ["grpc", "rest"] 22 | for transport in transports: 23 | accounts_provision_account_ticket.provision_account_ticket( 24 | TEST_REDIRECT_URL, transport=transport 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_search_change_history_events_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import accounts_search_change_history_events 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 21 | 22 | 23 | def test_accounts_search_change_history_events(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | accounts_search_change_history_events.search_change_history_events( 27 | TEST_ACCOUNT_ID, TEST_PROPERTY_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_update.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which updates the Google 18 | Analytics account. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/patch 21 | for more information. 22 | """ 23 | # [START analyticsadmin_accounts_update] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha.types import Account 26 | from google.protobuf.field_mask_pb2 import FieldMask 27 | 28 | from accounts_get import print_account 29 | 30 | 31 | def run_sample(): 32 | """Runs the sample.""" 33 | 34 | # !!! ATTENTION !!! 35 | # Running this sample may change/delete your Google Analytics account 36 | # configuration. Make sure to not use the Google Analytics account ID from 37 | # your production environment below. 38 | 39 | # TODO(developer): Replace this variable with your Google Analytics 40 | # account ID (e.g. "123456") before running the sample. 41 | account_id = "YOUR-GA-ACCOUNT-ID" 42 | update_account(account_id) 43 | 44 | 45 | def update_account(account_id: str, transport: str = None): 46 | """ 47 | Updates the Google Analytics account. 48 | 49 | Args: 50 | account_id(str): The Google Analytics Account ID. 51 | transport(str): The transport to use. For example, "grpc" 52 | or "rest". If set to None, a transport is chosen automatically. 53 | """ 54 | client = AnalyticsAdminServiceClient(transport=transport) 55 | # This call updates the display name and region code of the account, as 56 | # indicated by the value of the `update_mask` field. 57 | # The account to update is specified in the `name` field of the `Account` 58 | # instance. 59 | account = client.update_account( 60 | account=Account( 61 | name=f"accounts/{account_id}", 62 | display_name="This is a test account", 63 | region_code="US", 64 | ), 65 | update_mask=FieldMask(paths=["display_name", "region_code"]), 66 | ) 67 | 68 | print("Result:") 69 | print_account(account) 70 | 71 | 72 | # [END analyticsadmin_accounts_update] 73 | 74 | 75 | if __name__ == "__main__": 76 | run_sample() 77 | -------------------------------------------------------------------------------- /google-analytics-admin/accounts_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import accounts_update 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | 21 | 22 | def test_accounts_update(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | accounts_update.update_account(FAKE_ACCOUNT_ID, transport=transport) 29 | -------------------------------------------------------------------------------- /google-analytics-admin/noxfile_config.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | TEST_CONFIG_OVERRIDE = { 18 | # You can opt out from the test for specific Python versions. 19 | "ignored_versions": ["2.7"], 20 | # An envvar key for determining the project id to use. Change it 21 | # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a 22 | # build specific Cloud project. You can also use your own string 23 | # to use your own Cloud project. 24 | "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", 25 | # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', 26 | # A dictionary you want to inject into your test. Don't put any 27 | # secrets here. These values will override predefined values. 28 | "envs": { 29 | "GA_TEST_PROPERTY_ID": os.getenv("GA_TEST_PROPERTY_ID", "276206997"), 30 | "GA_TEST_ACCOUNT_ID": os.getenv("GA_TEST_ACCOUNT_ID", "199820965"), 31 | "GA_TEST_WEB_DATA_STREAM_ID": os.getenv( 32 | "GA_TEST_WEB_DATA_STREAM_ID", "2828068992" 33 | ), 34 | "GA_TEST_WEB_DATA_SECRET_ID": os.getenv( 35 | "GA_TEST_WEB_DATA_SECRET_ID", "2994983412" 36 | ), 37 | "GA_TEST_KEY_EVENT_ID": os.getenv("GA_TEST_KEY_EVENT_ID", "2719963095"), 38 | }, 39 | } 40 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_batch_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_batch_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_EMAIL_ADDRESS = "test@google.com" 21 | 22 | 23 | def test_properties_access_bindings_batch_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_access_bindings_batch_create.batch_create_property_access_binding( 30 | FAKE_PROPERTY_ID, FAKE_EMAIL_ADDRESS, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_batch_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_batch_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_properties_access_bindings_batch_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_access_bindings_batch_delete.batch_delete_property_access_binding( 30 | FAKE_PROPERTY_ID, FAKE_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_batch_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_access_bindings_batch_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_ACCESS_BINDING_ID = os.getenv("GA_TEST_PROPERTY_ACCESS_BINDING_ID") 21 | 22 | 23 | def test_properties_access_bindings_batch_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_access_bindings_batch_get.batch_get_property_access_binding( 27 | TEST_PROPERTY_ID, TEST_ACCESS_BINDING_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_batch_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_batch_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_properties_access_bindings_batch_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_access_bindings_batch_update.batch_update_property_access_binding( 30 | FAKE_PROPERTY_ID, FAKE_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_EMAIL_ADDRESS = "test@google.com" 21 | 22 | 23 | def test_properties_access_bindings_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_access_bindings_create.create_property_access_binding( 30 | FAKE_PROPERTY_ID, FAKE_EMAIL_ADDRESS, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_properties_access_bindings_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_access_bindings_delete.delete_property_access_binding( 30 | FAKE_PROPERTY_ID, FAKE_ACCESS_BINDING_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_access_bindings_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_ACCESS_BINDING_ID = os.getenv("GA_TEST_PROPERTY_ACCESS_BINDING_ID") 21 | 22 | 23 | def test_properties_access_bindings_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_access_bindings_get.get_property_access_binding( 27 | TEST_PROPERTY_ID, TEST_ACCESS_BINDING_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints access bindings under 18 | the specified parent Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.accessBindings/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_access_bindings_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | list_property_access_bindings(property_id) 33 | 34 | 35 | def list_property_access_bindings(property_id: str, transport: str = None): 36 | """ 37 | Lists access bindings under the specified parent Google Analytics 4 38 | property. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | results = client.list_access_bindings(parent=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | for access_binding in results: 50 | print(access_binding) 51 | print() 52 | 53 | 54 | # [END analyticsadmin_properties_access_bindings_list] 55 | 56 | 57 | if __name__ == "__main__": 58 | run_sample() 59 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_access_bindings_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_access_bindings_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_access_bindings_list.list_property_access_bindings( 26 | TEST_PROPERTY_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_access_bindings_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_access_bindings_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_ACCESS_BINDING_ID = "1" 21 | 22 | 23 | def test_properties_access_bindings_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises( 29 | Exception, 30 | match="The caller does not have permission|Invalid access binding name", 31 | ): 32 | properties_access_bindings_update.update_property_access_binding( 33 | FAKE_PROPERTY_ID, FAKE_ACCESS_BINDING_ID, transport=transport 34 | ) 35 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which creates a Google 18 | Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_create] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha.types import Property 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | 31 | # !!! ATTENTION !!! 32 | # Running this sample may change/delete your Google Analytics account 33 | # configuration. Make sure to not use the Google Analytics account ID from 34 | # your production environment below. 35 | 36 | # TODO(developer): Replace this variable with your Google Analytics 37 | # account ID (e.g. "123456") before running the sample. 38 | account_id = "YOUR-GA-ACCOUNT-ID" 39 | create_property(account_id) 40 | 41 | 42 | def create_property(account_id: str, transport: str = None): 43 | """ 44 | Creates a Google Analytics 4 property. 45 | 46 | Args: 47 | account_id(str): The Google Analytics Account ID. 48 | transport(str): The transport to use. For example, "grpc" 49 | or "rest". If set to None, a transport is chosen automatically. 50 | """ 51 | client = AnalyticsAdminServiceClient(transport=transport) 52 | property_ = client.create_property( 53 | property=Property( 54 | parent=f"accounts/{account_id}", 55 | currency_code="USD", 56 | display_name="Test property", 57 | industry_category="OTHER", 58 | time_zone="America/Los_Angeles", 59 | ) 60 | ) 61 | 62 | print("Result:") 63 | print(property_) 64 | 65 | 66 | # [END analyticsadmin_properties_create] 67 | 68 | 69 | if __name__ == "__main__": 70 | run_sample() 71 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_create 18 | 19 | FAKE_ACCOUNT_ID = "1" 20 | 21 | 22 | def test_properties_create(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | properties_create.create_property(FAKE_ACCOUNT_ID, transport=transport) 29 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which creates a data stream 18 | for the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.dataStreams/create 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_data_streams_create] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha.types import DataStream 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | 31 | # !!! ATTENTION !!! 32 | # Running this sample may change/delete your Google Analytics account 33 | # configuration. Make sure to not use the Google Analytics account ID from 34 | # your production environment below. 35 | 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID (e.g. "123456") before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | create_data_stream(property_id) 40 | 41 | 42 | def create_data_stream(property_id: str, transport: str = None): 43 | """ 44 | Creates a data stream for the Google Analytics 4 property. 45 | 46 | Args: 47 | property_id(str): The Google Analytics Property ID. 48 | transport(str): The transport to use. For example, "grpc" 49 | or "rest". If set to None, a transport is chosen automatically. 50 | """ 51 | client = AnalyticsAdminServiceClient(transport=transport) 52 | data_stream = DataStream( 53 | display_name="Test web data stream", 54 | web_stream_data=DataStream.WebStreamData(default_uri="https://www.google.com"), 55 | ) 56 | data_stream.type_ = "WEB_DATA_STREAM" 57 | result = client.create_data_stream( 58 | parent=f"properties/{property_id}", 59 | data_stream=data_stream, 60 | ) 61 | 62 | print("Result:") 63 | print(result) 64 | 65 | 66 | # [END analyticsadmin_properties_data_streams_create] 67 | 68 | 69 | if __name__ == "__main__": 70 | run_sample() 71 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | 21 | 22 | def test_properties_data_streams_create(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | properties_data_streams_create.create_data_stream( 29 | FAKE_PROPERTY_ID, transport=transport 30 | ) 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which deletes the data 18 | stream from the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.dataStreams/delete 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_data_streams_delete] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # !!! ATTENTION !!! 31 | # Running this sample may change/delete your Google Analytics account 32 | # configuration. Make sure to not use the Google Analytics property ID from 33 | # your production environment below. 34 | 35 | # TODO(developer): Replace this variable with your Google Analytics 4 36 | # property ID (e.g. "123456") before running the sample. 37 | property_id = "YOUR-GA4-PROPERTY-ID" 38 | 39 | # TODO(developer): Replace this variable with your data stream ID 40 | # (e.g. "123456") before running the sample. 41 | stream_id = "YOUR-DATA-STREAM-ID" 42 | 43 | delete_data_stream(property_id, stream_id) 44 | 45 | 46 | def delete_data_stream(property_id: str, stream_id: str, transport: str = None): 47 | """ 48 | Deletes the data stream from the Google Analytics 4 property. 49 | 50 | Args: 51 | property_id(str): The Google Analytics Property ID. 52 | stream_id(str): The data stream ID. 53 | transport(str): The transport to use. For example, "grpc" 54 | or "rest". If set to None, a transport is chosen automatically. 55 | """ 56 | client = AnalyticsAdminServiceClient(transport=transport) 57 | client.delete_data_stream(name=f"properties/{property_id}/dataStreams/{stream_id}") 58 | print("Data stream deleted") 59 | 60 | 61 | # [END analyticsadmin_properties_data_streams_delete] 62 | 63 | 64 | if __name__ == "__main__": 65 | run_sample() 66 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_STREAM_ID = "1" 21 | 22 | 23 | def test_properties_data_streams_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_data_streams_delete.delete_data_stream( 30 | FAKE_PROPERTY_ID, FAKE_STREAM_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_get_global_site_tag.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the Site Tag data 18 | for the specified web stream. 19 | """ 20 | # [START analyticsadmin_properties_data_streams_get_global_site_tag] 21 | from google.analytics.admin import AnalyticsAdminServiceClient 22 | 23 | 24 | def run_sample(): 25 | """Runs the sample.""" 26 | # TODO(developer): Replace this variable with your Google Analytics 4 27 | # property ID (e.g. "123456") before running the sample. 28 | property_id = "YOUR-GA4-PROPERTY-ID" 29 | 30 | # TODO(developer): Replace this variable with your data stream ID 31 | # (e.g. "123456") before running the sample. 32 | stream_id = "YOUR-DATA-STREAM-ID" 33 | 34 | get_global_site_tag(property_id, stream_id) 35 | 36 | 37 | def get_global_site_tag(property_id: str, stream_id: str, transport: str = None): 38 | """ 39 | Retrieves the Site Tag for the specified data stream. 40 | 41 | Args: 42 | property_id(str): The Google Analytics Property ID. 43 | stream_id(str): The data stream ID. 44 | transport(str): The transport to use. For example, "grpc" 45 | or "rest". If set to None, a transport is chosen automatically. 46 | """ 47 | client = AnalyticsAdminServiceClient(transport=transport) 48 | global_site_tag = client.get_global_site_tag( 49 | name=f"properties/{property_id}/dataStreams/{stream_id}/globalSiteTag" 50 | ) 51 | 52 | print("Result:") 53 | print(global_site_tag.snippet) 54 | 55 | 56 | # [END analyticsadmin_properties_data_streams_get_global_site_tag] 57 | 58 | 59 | if __name__ == "__main__": 60 | run_sample() 61 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_get_global_site_tag_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_data_streams_get_global_site_tag 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") 21 | 22 | 23 | def test_properties_data_streams_get_global_site_tag(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_data_streams_get_global_site_tag.get_global_site_tag( 27 | TEST_PROPERTY_ID, TEST_DATA_STREAM_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_data_streams_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") 21 | 22 | 23 | def test_properties_data_streams_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_data_streams_get.get_data_stream( 27 | TEST_PROPERTY_ID, TEST_DATA_STREAM_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints data streams 18 | for the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.dataStreams/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_data_streams_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | 33 | list_data_streams(property_id) 34 | 35 | 36 | def list_data_streams(property_id: str, transport: str = None): 37 | """ 38 | Lists data streams for the Google Analytics 4 property. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | results = client.list_data_streams(parent=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | for data_stream in results: 50 | print(data_stream) 51 | print() 52 | 53 | 54 | # [END analyticsadmin_properties_data_streams_list] 55 | 56 | 57 | if __name__ == "__main__": 58 | run_sample() 59 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_data_streams_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_data_streams_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_data_streams_list.list_data_streams( 26 | TEST_PROPERTY_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_measurement_protocol_secrets_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_STREAM_ID = "1" 21 | 22 | 23 | def test_properties_data_streams_measurement_protocol_secrets_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( 30 | FAKE_PROPERTY_ID, FAKE_STREAM_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_measurement_protocol_secrets_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_STREAM_ID = "1" 21 | FAKE_SECRET_ID = "1" 22 | 23 | 24 | def test_properties_data_streams_measurement_protocol_secrets_delete(): 25 | transports = ["grpc", "rest"] 26 | for transport in transports: 27 | # This test ensures that the call is valid and reaches the server, even 28 | # though the operation does not succeed due to permission error. 29 | with pytest.raises(Exception, match="The caller does not have permission"): 30 | properties_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( 31 | FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID, transport=transport 32 | ) 33 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_data_streams_measurement_protocol_secrets_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") 21 | TEST_SECRET_ID = os.getenv("GA_TEST_WEB_DATA_SECRET_ID") 22 | 23 | 24 | def test_properties_data_streams_measurement_protocol_secrets_get(capsys): 25 | transports = ["grpc", "rest"] 26 | for transport in transports: 27 | properties_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( 28 | TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID, transport=transport 29 | ) 30 | out, _ = capsys.readouterr() 31 | assert "Result" in out 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which lists measurement 18 | protocol secrets for the data stream. 19 | 20 | """ 21 | # [START analyticsadmin_properties_data_streams_measurement_protocol_secrets_list] 22 | from google.analytics.admin import AnalyticsAdminServiceClient 23 | 24 | 25 | def run_sample(): 26 | """Runs the sample.""" 27 | # TODO(developer): Replace this variable with your Google Analytics 4 28 | # property ID (e.g. "123456") before running the sample. 29 | property_id = "YOUR-GA4-PROPERTY-ID" 30 | 31 | # TODO(developer): Replace this variable with your data stream ID 32 | # (e.g. "123456") before running the sample. 33 | stream_id = "YOUR-DATA-STREAM-ID" 34 | list_measurement_protocol_secrets(property_id, stream_id) 35 | 36 | 37 | def list_measurement_protocol_secrets( 38 | property_id: str, stream_id: str, transport: str = None 39 | ): 40 | """ 41 | Lists measurement protocol secrets for the data stream. 42 | 43 | Args: 44 | property_id(str): The Google Analytics Property ID. 45 | stream_id(str): The data stream ID. 46 | transport(str): The transport to use. For example, "grpc" 47 | or "rest". If set to None, a transport is chosen automatically. 48 | """ 49 | client = AnalyticsAdminServiceClient(transport=transport) 50 | results = client.list_measurement_protocol_secrets( 51 | parent=f"properties/{property_id}/dataStreams/{stream_id}" 52 | ) 53 | 54 | print("Result:") 55 | for measurement_protocol_secret in results: 56 | print("Result:") 57 | print(f"Resource name: {measurement_protocol_secret.name}") 58 | print(f"Secret value: {measurement_protocol_secret.secret_value}") 59 | print(f"Display name: {measurement_protocol_secret.display_name}") 60 | print() 61 | 62 | 63 | # [END analyticsadmin_properties_data_streams_measurement_protocol_secrets_list] 64 | 65 | 66 | if __name__ == "__main__": 67 | run_sample() 68 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_data_streams_measurement_protocol_secrets_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") 21 | 22 | 23 | def test_properties_data_streams_measurement_protocol_secrets_list(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( 27 | TEST_PROPERTY_ID, TEST_DATA_STREAM_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_measurement_protocol_secrets_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_measurement_protocol_secrets_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_STREAM_ID = "1" 21 | FAKE_SECRET_ID = "1" 22 | 23 | 24 | def test_properties_data_streams_measurement_protocol_secrets_update(): 25 | transports = ["grpc", "rest"] 26 | for transport in transports: 27 | # This test ensures that the call is valid and reaches the server, even 28 | # though the operation does not succeed due to permission error. 29 | with pytest.raises(Exception, match="The caller does not have permission"): 30 | properties_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( 31 | FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID, transport=transport 32 | ) 33 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_data_streams_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_data_streams_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_STREAM_ID = "1" 21 | 22 | 23 | def test_properties_data_streams_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_data_streams_update.update_data_stream( 30 | FAKE_PROPERTY_ID, FAKE_STREAM_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which deletes the Google 18 | Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/delete 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_delete] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # !!! ATTENTION !!! 31 | # Running this sample may change/delete your Google Analytics account 32 | # configuration. Make sure to not use the Google Analytics property ID from 33 | # your production environment below. 34 | 35 | # TODO(developer): Replace this variable with your Google Analytics 4 36 | # property ID (e.g. "123456") before running the sample. 37 | property_id = "YOUR-GA4-PROPERTY-ID" 38 | delete_property(property_id) 39 | 40 | 41 | def delete_property(property_id: str, transport: str = None): 42 | """ 43 | Deletes the Google Analytics 4 property. 44 | 45 | Args: 46 | property_id(str): The Google Analytics Property ID. 47 | transport(str): The transport to use. For example, "grpc" 48 | or "rest". If set to None, a transport is chosen automatically. 49 | """ 50 | client = AnalyticsAdminServiceClient(transport=transport) 51 | client.delete_property(name=f"properties/{property_id}") 52 | print("Property deleted") 53 | 54 | 55 | # [END analyticsadmin_properties_delete] 56 | 57 | 58 | if __name__ == "__main__": 59 | run_sample() 60 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | 21 | 22 | def test_properties_delete(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | properties_delete.delete_property(FAKE_PROPERTY_ID, transport=transport) 29 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which creates a Firebase link 18 | for the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/create 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_firebase_links_create] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha.types import FirebaseLink 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | 31 | # !!! ATTENTION !!! 32 | # Running this sample may change/delete your Google Analytics account 33 | # configuration. Make sure to not use the Google Analytics account ID from 34 | # your production environment below. 35 | 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID (e.g. "123456") before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | 40 | # TODO(developer): Replace this variable with a Firebase project id. 41 | # This project will be linked to the GA4 property. 42 | firebase_project_id = "YOUR-FIREBASE-PROJECT-ID" 43 | 44 | create_firebase_link(property_id, firebase_project_id) 45 | 46 | 47 | def create_firebase_link( 48 | property_id: str, firebase_project_id: str, transport: str = None 49 | ): 50 | """ 51 | Creates a Firebase link for the Google Analytics 4 property. 52 | 53 | Args: 54 | property_id(str): The Google Analytics Property ID. 55 | firebase_project_id: Firebase project ID. 56 | transport(str): The transport to use. For example, "grpc" 57 | or "rest". If set to None, a transport is chosen automatically. 58 | """ 59 | client = AnalyticsAdminServiceClient(transport=transport) 60 | firebase_link = client.create_firebase_link( 61 | parent=f"properties/{property_id}", 62 | firebase_link=FirebaseLink(project=f"projects/{firebase_project_id}"), 63 | ) 64 | 65 | print("Result:") 66 | print(firebase_link) 67 | 68 | 69 | # [END analyticsadmin_properties_firebase_links_create] 70 | 71 | if __name__ == "__main__": 72 | run_sample() 73 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_firebase_links_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_FIREBASE_PROJECT_ID = "1" 21 | 22 | 23 | def test_properties_firebase_links_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_firebase_links_create.create_firebase_link( 30 | FAKE_PROPERTY_ID, FAKE_FIREBASE_PROJECT_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which deletes the Firebase 18 | link from the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/delete 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_firebase_links_delete] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # !!! ATTENTION !!! 31 | # Running this sample may change/delete your Google Analytics account 32 | # configuration. Make sure to not use the Google Analytics property ID from 33 | # your production environment below. 34 | 35 | # TODO(developer): Replace this variable with your Google Analytics 4 36 | # property ID (e.g. "123456") before running the sample. 37 | property_id = "YOUR-GA4-PROPERTY-ID" 38 | 39 | # TODO(developer): Replace this variable with your Firebase link ID 40 | # (e.g. "123456") before running the sample. 41 | firebase_link_id = "YOUR-FIREBASE-LINK-ID" 42 | 43 | delete_firebase_link(property_id, firebase_link_id) 44 | 45 | 46 | def delete_firebase_link( 47 | property_id: str, firebase_link_id: str, transport: str = None 48 | ): 49 | """ 50 | Deletes the Firebase link. 51 | 52 | Args: 53 | property_id(str): The Google Analytics Property ID. 54 | firebase_link_id: The Firebase link ID. 55 | transport(str): The transport to use. For example, "grpc" 56 | or "rest". If set to None, a transport is chosen automatically. 57 | """ 58 | client = AnalyticsAdminServiceClient(transport=transport) 59 | client.delete_firebase_link( 60 | name=f"properties/{property_id}/firebaseLinks/{firebase_link_id}" 61 | ) 62 | print("Firebase link deleted") 63 | 64 | 65 | # [END analyticsadmin_properties_firebase_links_delete] 66 | 67 | 68 | if __name__ == "__main__": 69 | run_sample() 70 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_firebase_links_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_FIREBASE_LINK_ID = "1" 21 | 22 | 23 | def test_properties_firebase_links_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_firebase_links_delete.delete_firebase_link( 30 | FAKE_PROPERTY_ID, FAKE_FIREBASE_LINK_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which prints Firebase links 18 | under the specified parent Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_firebase_links_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | list_firebase_links(property_id) 33 | 34 | 35 | def list_firebase_links(property_id: str, transport: str = None): 36 | """ 37 | Lists Firebase links under the specified parent Google Analytics 4 38 | property. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | results = client.list_firebase_links(parent=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | for firebase_link in results: 50 | print_firebase_link(firebase_link) 51 | print() 52 | 53 | 54 | def print_firebase_link(firebase_link): 55 | """Prints the Firebase link details.""" 56 | print(f"Resource name: {firebase_link.name}") 57 | print(f"Firebase project: {firebase_link.project}") 58 | print(f"Create time: {firebase_link.create_time}") 59 | 60 | 61 | # [END analyticsadmin_properties_firebase_links_list] 62 | 63 | 64 | if __name__ == "__main__": 65 | run_sample() 66 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_firebase_links_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_firebase_links_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_firebase_links_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_firebase_links_list.list_firebase_links( 26 | TEST_PROPERTY_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_get.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which print the Google 18 | Analytics 4 property details. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/get 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_get] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha.types import IndustryCategory 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | # TODO(developer): Replace this variable with your Google Analytics 4 31 | # property ID (e.g. "123456") before running the sample. 32 | property_id = "YOUR-GA4-PROPERTY-ID" 33 | get_property(property_id) 34 | 35 | 36 | def get_property(property_id: str, transport: str = None): 37 | """ 38 | Retrieves the Google Analytics 4 property details. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | property_ = client.get_property(name=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | print_property(property_) 50 | 51 | 52 | def print_property(property): 53 | """Prints the Google Analytics 4 property details.""" 54 | print(f"Resource name: {property.name}") 55 | print(f"Parent: {property.parent}") 56 | print(f"Display name: {property.display_name}") 57 | print(f"Create time: {property.create_time}") 58 | print(f"Update time: {property.update_time}") 59 | # print(f"Delete time: {property.delete_time}") 60 | # print(f"Expire time: {property.expire_time}") 61 | 62 | if property.industry_category: 63 | print(f"Industry category: {IndustryCategory(property.industry_category).name}") 64 | 65 | print(f"Time zone: {property.time_zone}") 66 | print(f"Currency code: {property.currency_code}") 67 | 68 | 69 | # [END analyticsadmin_properties_get] 70 | 71 | 72 | if __name__ == "__main__": 73 | run_sample() 74 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_get(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_get.get_property(TEST_PROPERTY_ID, transport=transport) 26 | out, _ = capsys.readouterr() 27 | assert "Result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_google_ads_links_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_ADS_CUSTOMER_ID = "1234567890" 21 | 22 | 23 | def test_properties_google_ads_links_create(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_google_ads_links_create.create_google_ads_link( 30 | FAKE_PROPERTY_ID, FAKE_ADS_CUSTOMER_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application deletes the Google Ads link 18 | from the specified Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/delete 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_google_ads_links_delete] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | 30 | # !!! ATTENTION !!! 31 | # Running this sample may change/delete your Google Analytics account 32 | # configuration. Make sure to not use the Google Analytics property ID from 33 | # your production environment below. 34 | 35 | # TODO(developer): Replace this variable with your Google Analytics 4 36 | # property ID (e.g. "123456") before running the sample. 37 | property_id = "YOUR-GA4-PROPERTY-ID" 38 | 39 | # TODO(developer): Replace this variable with your Google Analytics Ads 40 | # link ID (e.g. "123456") before running the sample. 41 | google_ads_link_id = "YOUR-GOOGLE-ADS-LINK-ID" 42 | 43 | delete_google_ads_link(property_id, google_ads_link_id) 44 | 45 | 46 | def delete_google_ads_link( 47 | property_id: str, google_ads_link_id: str, transport: str = None 48 | ): 49 | """ 50 | Deletes the Google Ads link. 51 | 52 | Args: 53 | property_id(str): The Google Analytics Property ID. 54 | google_ads_link_id(str): The Google Analytics Ads Link Id. 55 | transport(str): The transport to use. For example, "grpc" 56 | or "rest". If set to None, a transport is chosen automatically. 57 | """ 58 | client = AnalyticsAdminServiceClient(transport=transport) 59 | client.delete_google_ads_link( 60 | name=f"properties/{property_id}/googleAdsLinks/{google_ads_link_id}" 61 | ) 62 | print("Google Ads link deleted") 63 | 64 | 65 | # [END analyticsadmin_properties_google_ads_links_delete] 66 | 67 | 68 | if __name__ == "__main__": 69 | run_sample() 70 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_google_ads_links_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_GOOGLE_ADS_LINK_ID = "1" 21 | 22 | 23 | def test_properties_google_ads_links_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_google_ads_links_delete.delete_google_ads_link( 30 | FAKE_PROPERTY_ID, FAKE_GOOGLE_ADS_LINK_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints Google Ads links 18 | under the specified parent Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_google_ads_links_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | list_google_ads_links(property_id) 33 | 34 | 35 | def list_google_ads_links(property_id: str, transport: str = None): 36 | """ 37 | Lists Google Ads links under the specified parent Google Analytics 4 38 | property. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | results = client.list_google_ads_links(parent=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | for google_ads_link in results: 50 | print_google_ads_link(google_ads_link) 51 | print() 52 | 53 | 54 | def print_google_ads_link(google_ads_link): 55 | """Prints the Google Ads link details.""" 56 | print(f"Resource name: {google_ads_link.name}") 57 | print(f"Google Ads customer ID: {google_ads_link.customer_id}") 58 | print(f"Can manage clients: {google_ads_link.can_manage_clients}") 59 | print(f"Ads personalization enabled: {google_ads_link.ads_personalization_enabled}") 60 | print(f"Email address of the link creator: {google_ads_link.creator_email_address}") 61 | print(f"Create time: {google_ads_link.create_time}") 62 | print(f"Update time: {google_ads_link.update_time}") 63 | 64 | 65 | # [END analyticsadmin_properties_google_ads_links_list] 66 | 67 | 68 | if __name__ == "__main__": 69 | run_sample() 70 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_google_ads_links_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_google_ads_links_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_google_ads_links_list.list_google_ads_links( 26 | TEST_PROPERTY_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_google_ads_links_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_google_ads_links_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_GOOGLE_ADS_LINK_ID = "1" 21 | 22 | 23 | def test_properties_google_ads_links_update(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_google_ads_links_update.update_google_ads_link( 30 | FAKE_PROPERTY_ID, FAKE_GOOGLE_ADS_LINK_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which creates a key 18 | event for the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.keyEvents/create 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_key_events_create] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | from google.analytics.admin_v1alpha import KeyEvent 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | 31 | # !!! ATTENTION !!! 32 | # Running this sample may change/delete your Google Analytics account 33 | # configuration. Make sure to not use the Google Analytics account ID from 34 | # your production environment below. 35 | 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID (e.g. "123456") before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | 40 | create_key_event(property_id) 41 | 42 | 43 | def create_key_event(property_id: str, transport: str = None): 44 | """ 45 | Creates a key event for the Google Analytics 4 property. 46 | 47 | Args: 48 | property_id(str): The Google Analytics Property ID. 49 | transport(str): The transport to use. For example, "grpc" 50 | or "rest". If set to None, a transport is chosen automatically. 51 | """ 52 | client = AnalyticsAdminServiceClient(transport=transport) 53 | key_event = client.create_key_event( 54 | parent=f"properties/{property_id}", 55 | key_event=KeyEvent(event_name="test_purchase"), 56 | ) 57 | 58 | print("Result:") 59 | print(f"Resource name: {key_event.name}") 60 | print(f"Event name: {key_event.event_name}") 61 | print(f"Create time: {key_event.create_time}") 62 | print(f"Deletable: {key_event.deletable}") 63 | print(f"Custom: {key_event.custom}") 64 | 65 | 66 | # [END analyticsadmin_properties_key_events_create] 67 | 68 | if __name__ == "__main__": 69 | run_sample() 70 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_create_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_key_events_create 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | 21 | 22 | def test_properties_key_events_create(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | properties_key_events_create.create_key_event( 29 | FAKE_PROPERTY_ID, transport=transport 30 | ) 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which deletes a key 18 | event for the Google Analytics 4 property. 19 | 20 | 21 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.keyEvents/delete 22 | for more information. 23 | """ 24 | # [START analyticsadmin_properties_key_events_delete] 25 | from google.analytics.admin import AnalyticsAdminServiceClient 26 | 27 | 28 | def run_sample(): 29 | """Runs the sample.""" 30 | 31 | # !!! ATTENTION !!! 32 | # Running this sample may change/delete your Google Analytics account 33 | # configuration. Make sure to not use the Google Analytics property ID from 34 | # your production environment below. 35 | 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID (e.g. "123456") before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | 40 | # TODO(developer): Replace this variable with your key event ID 41 | # (e.g. "123456") before running the sample. 42 | key_event_id = "YOUR-KEY-EVENT-ID" 43 | 44 | delete_key_event(property_id, key_event_id) 45 | 46 | 47 | def delete_key_event(property_id: str, key_event_id: str, transport: str = None): 48 | """ 49 | Deletes the key event for the Google Analytics 4 property. 50 | 51 | Args: 52 | property_id(str): The Google Analytics Property ID. 53 | key_event_id(str): The key event ID 54 | transport(str): The transport to use. For example, "grpc" 55 | or "rest". If set to None, a transport is chosen automatically. 56 | """ 57 | client = AnalyticsAdminServiceClient(transport=transport) 58 | client.delete_key_event(name=f"properties/{property_id}/keyEvents/{key_event_id}") 59 | print("Key event deleted") 60 | 61 | 62 | # [END analyticsadmin_properties_key_events_delete] 63 | 64 | 65 | if __name__ == "__main__": 66 | run_sample() 67 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_delete_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_key_events_delete 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | FAKE_KEY_EVENT_ID = "1" 21 | 22 | 23 | def test_properties_key_events_delete(): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | # This test ensures that the call is valid and reaches the server, even 27 | # though the operation does not succeed due to permission error. 28 | with pytest.raises(Exception, match="The caller does not have permission"): 29 | properties_key_events_delete.delete_key_event( 30 | FAKE_PROPERTY_ID, FAKE_KEY_EVENT_ID, transport=transport 31 | ) 32 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_get.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which prints the key 18 | event details. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.keyEvents/get 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_key_events_get] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | 33 | # TODO(developer): Replace this variable with your key event ID 34 | # (e.g. "123456") before running the sample. 35 | key_event_id = "YOUR-KEY-EVENT-ID" 36 | 37 | get_key_event(property_id, key_event_id) 38 | 39 | 40 | def get_key_event(property_id: str, key_event_id: str, transport: str = None): 41 | """ 42 | Retrieves the details for the key event. 43 | Args: 44 | property_id(str): The Google Analytics Property ID. 45 | key_event_id(str): The key event ID 46 | transport(str): The transport to use. For example, "grpc" 47 | or "rest". If set to None, a transport is chosen automatically. 48 | """ 49 | client = AnalyticsAdminServiceClient(transport=transport) 50 | key_event = client.get_key_event( 51 | name=f"properties/{property_id}/keyEvents/{key_event_id}" 52 | ) 53 | 54 | print("Result:") 55 | print(f"Resource name: {key_event.name}") 56 | print(f"Event name: {key_event.event_name}") 57 | print(f"Create time: {key_event.create_time}") 58 | print(f"Deletable: {key_event.deletable}") 59 | print(f"Custom: {key_event.custom}") 60 | 61 | 62 | # [END analyticsadmin_properties_key_events_get] 63 | 64 | 65 | if __name__ == "__main__": 66 | run_sample() 67 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_get_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_key_events_get 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | TEST_KEY_EVENT_ID = os.getenv("GA_TEST_KEY_EVENT_ID") 21 | 22 | 23 | def test_properties_key_events_get(capsys): 24 | transports = ["grpc", "rest"] 25 | for transport in transports: 26 | properties_key_events_get.get_key_event( 27 | TEST_PROPERTY_ID, TEST_KEY_EVENT_ID, transport=transport 28 | ) 29 | out, _ = capsys.readouterr() 30 | assert "Result" in out 31 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 Google LLC 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 Admin API sample application which lists key events 18 | for the Google Analytics 4 property. 19 | 20 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.keyEvents/list 21 | for more information. 22 | """ 23 | # [START analyticsadmin_properties_key_events_list] 24 | from google.analytics.admin import AnalyticsAdminServiceClient 25 | 26 | 27 | def run_sample(): 28 | """Runs the sample.""" 29 | # TODO(developer): Replace this variable with your Google Analytics 4 30 | # property ID (e.g. "123456") before running the sample. 31 | property_id = "YOUR-GA4-PROPERTY-ID" 32 | 33 | list_key_events(property_id) 34 | 35 | 36 | def list_key_events(property_id: str, transport: str = None): 37 | """ 38 | Lists key events for the Google Analytics 4 property. 39 | 40 | Args: 41 | property_id(str): The Google Analytics Property ID. 42 | transport(str): The transport to use. For example, "grpc" 43 | or "rest". If set to None, a transport is chosen automatically. 44 | """ 45 | client = AnalyticsAdminServiceClient(transport=transport) 46 | results = client.list_key_events(parent=f"properties/{property_id}") 47 | 48 | print("Result:") 49 | for key_event in results: 50 | print(f"Resource name: {key_event.name}") 51 | print(f"Event name: {key_event.event_name}") 52 | print(f"Create time: {key_event.create_time}") 53 | print(f"Deletable: {key_event.deletable}") 54 | print(f"Custom: {key_event.custom}") 55 | print() 56 | 57 | 58 | # [END analyticsadmin_properties_key_events_list] 59 | 60 | 61 | if __name__ == "__main__": 62 | run_sample() 63 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_key_events_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_key_events_list 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_properties_key_events_list(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_key_events_list.list_key_events( 26 | TEST_PROPERTY_ID, transport=transport 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 Admin API sample application which prints Google Analytics 4 18 | properties under the specified parent account that are available to the 19 | current user. 20 | 21 | See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/list 22 | for more information. 23 | """ 24 | # [START analyticsadmin_properties_list] 25 | from google.analytics.admin import AnalyticsAdminServiceClient 26 | from google.analytics.admin_v1alpha.types import ListPropertiesRequest 27 | 28 | 29 | def run_sample(): 30 | """Runs the sample.""" 31 | # TODO(developer): Replace this variable with your Google Analytics 32 | # account ID (e.g. "123456") before running the sample. 33 | account_id = "YOUR-GA-ACCOUNT-ID" 34 | list_properties(account_id) 35 | 36 | 37 | def list_properties(account_id: str, transport: str = None): 38 | """ 39 | Lists Google Analytics 4 properties under the specified parent account 40 | that are available to the current user. 41 | 42 | Args: 43 | account_id(str): The Google Analytics account ID. 44 | transport(str): The transport to use. For example, "grpc" 45 | or "rest". If set to None, a transport is chosen automatically. 46 | """ 47 | client = AnalyticsAdminServiceClient(transport=transport) 48 | results = client.list_properties( 49 | ListPropertiesRequest(filter=f"parent:accounts/{account_id}", show_deleted=True) 50 | ) 51 | 52 | print("Result:") 53 | for property_ in results: 54 | print(property_) 55 | print() 56 | 57 | 58 | # [END analyticsadmin_properties_list] 59 | 60 | 61 | if __name__ == "__main__": 62 | run_sample() 63 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_list_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | import properties_list 18 | 19 | TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") 20 | 21 | 22 | def test_properties_get(capsys): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | properties_list.list_properties(TEST_ACCOUNT_ID, transport=transport) 26 | out, _ = capsys.readouterr() 27 | assert "Result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_run_access_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 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 os 16 | 17 | import pytest 18 | 19 | import properties_run_access_report 20 | 21 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 22 | 23 | 24 | def test_properties_run_access_report(capsys): 25 | transports = ["grpc", "rest"] 26 | for transport in transports: 27 | # This test ensures that the call is valid and reaches the server, even 28 | # though the operation does not succeed due to the test property not 29 | # being a Google Analytics 360 property. 30 | with pytest.raises( 31 | Exception, 32 | match="Data Access Reports are only allowed on Google Analytics 360 properties.", 33 | ): 34 | properties_run_access_report.run_access_report( 35 | TEST_PROPERTY_ID, transport=transport 36 | ) 37 | -------------------------------------------------------------------------------- /google-analytics-admin/properties_update_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 pytest 16 | 17 | import properties_update 18 | 19 | FAKE_PROPERTY_ID = "1" 20 | 21 | 22 | def test_properties_update(): 23 | transports = ["grpc", "rest"] 24 | for transport in transports: 25 | # This test ensures that the call is valid and reaches the server, even 26 | # though the operation does not succeed due to permission error. 27 | with pytest.raises(Exception, match="The caller does not have permission"): 28 | properties_update.update_property(FAKE_PROPERTY_ID, transport=transport) 29 | -------------------------------------------------------------------------------- /google-analytics-admin/quickstart_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 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_accounts_list(capsys): 19 | transports = ["grpc", "rest"] 20 | for transport in transports: 21 | quickstart.list_accounts(transport=transport) 22 | out, _ = capsys.readouterr() 23 | assert "Result" in out 24 | -------------------------------------------------------------------------------- /google-analytics-admin/requirements-test.txt: -------------------------------------------------------------------------------- 1 | pytest==8.3.5 -------------------------------------------------------------------------------- /google-analytics-admin/requirements.txt: -------------------------------------------------------------------------------- 1 | google-analytics-admin==0.24.0 2 | google-auth-oauthlib==1.2.2 3 | -------------------------------------------------------------------------------- /google-analytics-data/README.md: -------------------------------------------------------------------------------- 1 | # Google Analytics Data API examples 2 | 3 | [![Open in Cloud Shell][shell_img]][shell_link] 4 | 5 | [shell_img]: http://gstatic.com/cloudssh/images/open-btn.png 6 | [shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleanalytics/python-docs-samples&page=editor&open_in_editor=/google-analytics-data/README.md 7 | 8 | These samples show how to use the 9 | [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1) from Python. 10 | 11 | ## Build and Run 12 | 13 | 1. **Enable APIs** - [Enable the Analytics Data API](https://console.cloud.google.com/flows/enableapi?apiid=analyticsdata.googleapis.com) 14 | and create a new project or select an existing project. 15 | 2. **Download The Credentials** - Configure your project using [Application Default Credentials][adc]. 16 | Click "Go to credentials" after enabling the APIs. Click "Create Credentials" 17 | and select "Service Account Credentials" and download the credentials file. Then set the path to 18 | this file to the environment variable `GOOGLE_APPLICATION_CREDENTIALS`: 19 | 20 | ```sh 21 | export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json 22 | ``` 23 | 3. **Clone the repo** and cd into this directory 24 | ```sh 25 | git clone https://github.com/googleanalytics/python-docs-samples 26 | cd python-docs-samples/google-analytics-data 27 | ``` 28 | 4. **Install dependencies** via [pip3](https://pip.pypa.io/en/stable). 29 | ```sh 30 | pip3 install --upgrade -r requirements.txt 31 | ``` 32 | 5. **Review the comments starting with `TODO(developer)` and update the code to 33 | use correct values.** 34 | 6. **Run** with the command `python3 SNIPPET_NAME.py`. For example: 35 | ```sh 36 | $ python3 quickstart.py 37 | ``` 38 | 39 | [adc]: https://cloud.google.com/docs/authentication#adc 40 | -------------------------------------------------------------------------------- /google-analytics-data/get_common_metadata_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 get_common_metadata 16 | 17 | 18 | def test_get_common_metadata(capsys): 19 | get_common_metadata.get_common_metadata() 20 | out, _ = capsys.readouterr() 21 | assert "Dimensions and metrics" in out 22 | -------------------------------------------------------------------------------- /google-analytics-data/get_metadata_by_property_id.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application retrieving dimension and metrics 18 | metadata. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata 21 | for more information. 22 | """ 23 | # [START analyticsdata_get_metadata_by_property_id] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import GetMetadataRequest 26 | 27 | from get_common_metadata import print_get_metadata_response 28 | 29 | 30 | def run_sample(): 31 | """Runs the sample.""" 32 | # TODO(developer): Replace this variable with your Google Analytics 4 33 | # property ID before running the sample. 34 | property_id = "YOUR-GA4-PROPERTY-ID" 35 | get_metadata_by_property_id(property_id) 36 | 37 | 38 | def get_metadata_by_property_id(property_id="YOUR-GA4-PROPERTY-ID"): 39 | """Retrieves dimensions and metrics available for a Google Analytics 4 40 | property, including custom fields.""" 41 | client = BetaAnalyticsDataClient() 42 | 43 | request = GetMetadataRequest(name=f"properties/{property_id}/metadata") 44 | response = client.get_metadata(request) 45 | 46 | print( 47 | f"Dimensions and metrics available for Google Analytics 4 " 48 | f"property {property_id} (including custom fields):" 49 | ) 50 | print_get_metadata_response(response) 51 | 52 | 53 | # [END analyticsdata_get_metadata_by_property_id] 54 | 55 | if __name__ == "__main__": 56 | run_sample() 57 | -------------------------------------------------------------------------------- /google-analytics-data/get_metadata_by_property_id_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import get_metadata_by_property_id 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_get_metadata_by_property_id(capsys): 23 | get_metadata_by_property_id.get_metadata_by_property_id(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Dimensions and metrics" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/noxfile_config.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Google LLC 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 os 16 | 17 | TEST_CONFIG_OVERRIDE = { 18 | # You can opt out from the test for specific Python versions. 19 | "ignored_versions": ["2.7"], 20 | # An envvar key for determining the project id to use. Change it 21 | # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a 22 | # build specific Cloud project. You can also use your own string 23 | # to use your own Cloud project. 24 | "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", 25 | # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', 26 | # A dictionary you want to inject into your test. Don't put any 27 | # secrets here. These values will override predefined values. 28 | # 29 | # Adds the GA_TEST_PROPERTY_ID required by tests for Google Analytics 30 | # Data samples. Uses a specific test property by default, but can 31 | # be overridden by setting this environment variable before running 32 | # nox. 33 | "envs": {"GA_TEST_PROPERTY_ID": os.getenv("GA_TEST_PROPERTY_ID", "222596558")}, 34 | } 35 | -------------------------------------------------------------------------------- /google-analytics-data/quickstart.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 | This application demonstrates the usage of the Analytics Data API using 19 | service account credentials. 20 | Before you start the application, please review the comments starting with 21 | "TODO(developer)" and update the code to use correct values. 22 | Usage: 23 | pip3 install --upgrade google-analytics-data 24 | python3 quickstart.py 25 | """ 26 | # [START analyticsdata_quickstart] 27 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 28 | from google.analytics.data_v1beta.types import ( 29 | DateRange, 30 | Dimension, 31 | Metric, 32 | RunReportRequest, 33 | ) 34 | 35 | 36 | def sample_run_report(property_id="YOUR-GA4-PROPERTY-ID"): 37 | """Runs a simple report on a Google Analytics 4 property.""" 38 | # TODO(developer): Uncomment this variable and replace with your 39 | # Google Analytics 4 property ID before running the sample. 40 | # property_id = "YOUR-GA4-PROPERTY-ID" 41 | 42 | # [START analyticsdata_run_report_initialize] 43 | # Using a default constructor instructs the client to use the credentials 44 | # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. 45 | client = BetaAnalyticsDataClient() 46 | # [END analyticsdata_run_report_initialize] 47 | 48 | # [START analyticsdata_run_report] 49 | request = RunReportRequest( 50 | property=f"properties/{property_id}", 51 | dimensions=[Dimension(name="city")], 52 | metrics=[Metric(name="activeUsers")], 53 | date_ranges=[DateRange(start_date="2020-03-31", end_date="today")], 54 | ) 55 | response = client.run_report(request) 56 | # [END analyticsdata_run_report] 57 | 58 | # [START analyticsdata_run_report_response] 59 | print("Report result:") 60 | for row in response.rows: 61 | print(row.dimension_values[0].value, row.metric_values[0].value) 62 | # [END analyticsdata_run_report_response] 63 | 64 | 65 | # [END analyticsdata_quickstart] 66 | 67 | 68 | if __name__ == "__main__": 69 | sample_run_report() 70 | -------------------------------------------------------------------------------- /google-analytics-data/quickstart_json_credentials_test.py: -------------------------------------------------------------------------------- 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 os 16 | 17 | import quickstart_json_credentials 18 | 19 | 20 | def test_quickstart(capsys): 21 | # Create a temporary service account credentials JSON file to be used by 22 | # the test. 23 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 24 | CREDENTIALS_JSON_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") 25 | quickstart_json_credentials.sample_run_report( 26 | TEST_PROPERTY_ID, CREDENTIALS_JSON_PATH 27 | ) 28 | out, _ = capsys.readouterr() 29 | assert "Report result" in out 30 | -------------------------------------------------------------------------------- /google-analytics-data/quickstart_oauth2_test.py: -------------------------------------------------------------------------------- 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 os 16 | 17 | import quickstart_oauth2 18 | 19 | 20 | def test_quickstart(capsys): 21 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 22 | quickstart_oauth2.sample_run_report(None, TEST_PROPERTY_ID) 23 | out, _ = capsys.readouterr() 24 | assert "Report result" in out 25 | -------------------------------------------------------------------------------- /google-analytics-data/quickstart_test.py: -------------------------------------------------------------------------------- 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 os 16 | 17 | import quickstart 18 | 19 | 20 | def test_quickstart(capsys): 21 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 22 | quickstart.sample_run_report(TEST_PROPERTY_ID) 23 | out, _ = capsys.readouterr() 24 | assert "Report result" in out 25 | -------------------------------------------------------------------------------- /google-analytics-data/requirements-test.txt: -------------------------------------------------------------------------------- 1 | pytest==8.3.5 2 | -------------------------------------------------------------------------------- /google-analytics-data/requirements.txt: -------------------------------------------------------------------------------- 1 | google-analytics-data==0.18.18 2 | google-auth-oauthlib==1.2.2 -------------------------------------------------------------------------------- /google-analytics-data/run_batch_report.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the batch creation 18 | of multiple reports. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/batchRunReports 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_batch_report] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | BatchRunReportsRequest, 27 | DateRange, 28 | Dimension, 29 | Metric, 30 | RunReportRequest, 31 | ) 32 | 33 | from run_report import print_run_report_response 34 | 35 | 36 | def run_sample(): 37 | """Runs the sample.""" 38 | # TODO(developer): Replace this variable with your Google Analytics 4 39 | # property ID before running the sample. 40 | property_id = "YOUR-GA4-PROPERTY-ID" 41 | run_batch_report(property_id) 42 | 43 | 44 | def run_batch_report(property_id="YOUR-GA4-PROPERTY-ID"): 45 | """Runs a batch report on a Google Analytics 4 property.""" 46 | client = BetaAnalyticsDataClient() 47 | 48 | request = BatchRunReportsRequest( 49 | property=f"properties/{property_id}", 50 | requests=[ 51 | RunReportRequest( 52 | dimensions=[ 53 | Dimension(name="country"), 54 | Dimension(name="region"), 55 | Dimension(name="city"), 56 | ], 57 | metrics=[Metric(name="activeUsers")], 58 | date_ranges=[DateRange(start_date="2021-01-03", end_date="2021-01-09")], 59 | ), 60 | RunReportRequest( 61 | dimensions=[Dimension(name="browser")], 62 | metrics=[Metric(name="activeUsers")], 63 | date_ranges=[DateRange(start_date="2021-01-01", end_date="2021-01-31")], 64 | ), 65 | ], 66 | ) 67 | response = client.batch_run_reports(request) 68 | 69 | print("Batch report results:") 70 | for report in response.reports: 71 | print_run_report_response(report) 72 | 73 | 74 | # [END analyticsdata_run_batch_report] 75 | 76 | 77 | if __name__ == "__main__": 78 | run_sample() 79 | -------------------------------------------------------------------------------- /google-analytics-data/run_batch_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_batch_report 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_batch_report(capsys): 23 | run_batch_report.run_batch_report(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Batch report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_funnel_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_funnel_report 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_funnel_report(capsys): 23 | run_funnel_report.run_funnel_report(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_pivot_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_pivot_report 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_pivot_report(capsys): 23 | run_pivot_report.run_pivot_report(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the creation of 18 | a realtime report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_realtime_report] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | Dimension, 27 | Metric, 28 | RunRealtimeReportRequest, 29 | ) 30 | 31 | from run_report import print_run_report_response 32 | 33 | 34 | def run_sample(): 35 | """Runs the sample.""" 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | run_realtime_report(property_id) 40 | 41 | 42 | def run_realtime_report(property_id="YOUR-GA4-PROPERTY-ID"): 43 | """Runs a realtime report on a Google Analytics 4 property.""" 44 | client = BetaAnalyticsDataClient() 45 | 46 | request = RunRealtimeReportRequest( 47 | property=f"properties/{property_id}", 48 | dimensions=[Dimension(name="country")], 49 | metrics=[Metric(name="activeUsers")], 50 | ) 51 | response = client.run_realtime_report(request) 52 | print_run_report_response(response) 53 | 54 | 55 | # [END analyticsdata_run_realtime_report] 56 | 57 | 58 | if __name__ == "__main__": 59 | run_sample() 60 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_realtime_report 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_realtime_report(capsys): 23 | run_realtime_report.run_realtime_report(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_minute_ranges.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2022 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 application demonstrating the creation of 18 | a realtime report using minute ranges. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport#body.request_body.FIELDS.minute_ranges 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_realtime_report_with_minute_ranges] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | Metric, 27 | MinuteRange, 28 | RunRealtimeReportRequest, 29 | ) 30 | 31 | from run_report import print_run_report_response 32 | 33 | 34 | def run_sample(): 35 | """Runs the sample.""" 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | run_realtime_report_with_minute_ranges(property_id) 40 | 41 | 42 | def run_realtime_report_with_minute_ranges(property_id="YOUR-GA4-PROPERTY-ID"): 43 | """Runs a realtime report on a Google Analytics 4 property. Dimensions 44 | field is omitted in the query, which results in total values of active users 45 | returned for each minute range in the report. 46 | 47 | Note the `dateRange` dimension added to the report response automatically 48 | as a result of querying multiple minute ranges. 49 | """ 50 | client = BetaAnalyticsDataClient() 51 | 52 | request = RunRealtimeReportRequest( 53 | property=f"properties/{property_id}", 54 | metrics=[Metric(name="activeUsers")], 55 | minute_ranges=[ 56 | MinuteRange(name="0-4 minutes ago", start_minutes_ago=4), 57 | MinuteRange( 58 | name="25-29 minutes ago", start_minutes_ago=29, end_minutes_ago=25 59 | ), 60 | ], 61 | ) 62 | response = client.run_realtime_report(request) 63 | print_run_report_response(response) 64 | 65 | 66 | # [END analyticsdata_run_realtime_report_with_minute_ranges] 67 | 68 | 69 | if __name__ == "__main__": 70 | run_sample() 71 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_minute_ranges_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 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 os 16 | 17 | import run_realtime_report_with_minute_ranges 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_metrics(capsys): 23 | run_realtime_report_with_minute_ranges.run_realtime_report_with_minute_ranges( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_multiple_dimensions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the creation of 18 | a realtime report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_realtime_report_with_multiple_dimensions] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | Dimension, 27 | Metric, 28 | RunRealtimeReportRequest, 29 | ) 30 | 31 | from run_report import print_run_report_response 32 | 33 | 34 | def run_sample(): 35 | """Runs the sample.""" 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | run_realtime_report_with_multiple_dimensions(property_id) 40 | 41 | 42 | def run_realtime_report_with_multiple_dimensions(property_id="YOUR-GA4-PROPERTY-ID"): 43 | """Runs a realtime report on a Google Analytics 4 property.""" 44 | client = BetaAnalyticsDataClient() 45 | 46 | request = RunRealtimeReportRequest( 47 | property=f"properties/{property_id}", 48 | dimensions=[Dimension(name="country"), Dimension(name="city")], 49 | metrics=[Metric(name="activeUsers")], 50 | ) 51 | response = client.run_realtime_report(request) 52 | print_run_report_response(response) 53 | 54 | 55 | # [END analyticsdata_run_realtime_report_with_multiple_dimensions] 56 | 57 | 58 | if __name__ == "__main__": 59 | run_sample() 60 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_multiple_dimensions_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_multiple_dimensions 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_dimensions(capsys): 23 | run_report_with_multiple_dimensions.run_report_with_multiple_dimensions( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_multiple_metrics.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the creation of 18 | a realtime report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_realtime_report_with_multiple_metrics] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | Dimension, 27 | Metric, 28 | RunRealtimeReportRequest, 29 | ) 30 | 31 | from run_report import print_run_report_response 32 | 33 | 34 | def run_sample(): 35 | """Runs the sample.""" 36 | # TODO(developer): Replace this variable with your Google Analytics 4 37 | # property ID before running the sample. 38 | property_id = "YOUR-GA4-PROPERTY-ID" 39 | run_realtime_report_with_multiple_metrics(property_id) 40 | 41 | 42 | def run_realtime_report_with_multiple_metrics(property_id="YOUR-GA4-PROPERTY-ID"): 43 | """Runs a realtime report on a Google Analytics 4 property.""" 44 | client = BetaAnalyticsDataClient() 45 | 46 | request = RunRealtimeReportRequest( 47 | property=f"properties/{property_id}", 48 | dimensions=[Dimension(name="unifiedScreenName")], 49 | metrics=[Metric(name="screenPageViews"), Metric(name="keyEvents")], 50 | ) 51 | response = client.run_realtime_report(request) 52 | print_run_report_response(response) 53 | 54 | 55 | # [END analyticsdata_run_realtime_report_with_multiple_metrics] 56 | 57 | 58 | if __name__ == "__main__": 59 | run_sample() 60 | -------------------------------------------------------------------------------- /google-analytics-data/run_realtime_report_with_multiple_metrics_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_multiple_metrics 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_metrics(capsys): 23 | run_report_with_multiple_metrics.run_report_with_multiple_metrics(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_basic(capsys): 23 | run_report.run_report(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_aggregations.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the usage of 18 | metric aggregations in a report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.metric_aggregations 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_aggregations] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | MetricAggregation, 30 | RunReportRequest, 31 | ) 32 | 33 | from run_report import print_run_report_response 34 | 35 | 36 | def run_sample(): 37 | """Runs the sample.""" 38 | # TODO(developer): Replace this variable with your Google Analytics 4 39 | # property ID before running the sample. 40 | property_id = "YOUR-GA4-PROPERTY-ID" 41 | run_report_with_aggregations(property_id) 42 | 43 | 44 | def run_report_with_aggregations(property_id="YOUR-GA4-PROPERTY-ID"): 45 | """Runs a report which includes total, maximum and minimum values for 46 | each metric.""" 47 | client = BetaAnalyticsDataClient() 48 | 49 | request = RunReportRequest( 50 | property=f"properties/{property_id}", 51 | dimensions=[Dimension(name="country")], 52 | metrics=[Metric(name="sessions")], 53 | date_ranges=[DateRange(start_date="365daysAgo", end_date="today")], 54 | metric_aggregations=[ 55 | MetricAggregation.TOTAL, 56 | MetricAggregation.MAXIMUM, 57 | MetricAggregation.MINIMUM, 58 | ], 59 | ) 60 | response = client.run_report(request) 61 | print_run_report_response(response) 62 | 63 | 64 | # [END analyticsdata_run_report_with_aggregations] 65 | 66 | 67 | if __name__ == "__main__": 68 | run_sample() 69 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_aggregations_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_aggregations 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_aggregations(capsys): 23 | run_report_with_aggregations.run_report_with_aggregations(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_cohorts_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_cohorts 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_cohorts(capsys): 23 | run_report_with_cohorts.run_report_with_cohorts(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_date_ranges.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the usage of 18 | date ranges in a report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.date_ranges 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_date_ranges] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | RunReportRequest, 30 | ) 31 | 32 | from run_report import print_run_report_response 33 | 34 | 35 | def run_sample(): 36 | """Runs the sample.""" 37 | # TODO(developer): Replace this variable with your Google Analytics 4 38 | # property ID before running the sample. 39 | property_id = "YOUR-GA4-PROPERTY-ID" 40 | run_report_with_date_ranges(property_id) 41 | 42 | 43 | def run_report_with_date_ranges(property_id="YOUR-GA4-PROPERTY-ID"): 44 | """Runs a report using two date ranges.""" 45 | client = BetaAnalyticsDataClient() 46 | 47 | request = RunReportRequest( 48 | property=f"properties/{property_id}", 49 | date_ranges=[ 50 | DateRange(start_date="2019-08-01", end_date="2019-08-14"), 51 | DateRange(start_date="2020-08-01", end_date="2020-08-14"), 52 | ], 53 | dimensions=[Dimension(name="platform")], 54 | metrics=[Metric(name="activeUsers")], 55 | ) 56 | response = client.run_report(request) 57 | print_run_report_response(response) 58 | 59 | 60 | # [END analyticsdata_run_report_with_date_ranges] 61 | 62 | 63 | if __name__ == "__main__": 64 | run_sample() 65 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_date_ranges_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_date_ranges 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_date_ranges(capsys): 23 | run_report_with_date_ranges.run_report_with_date_ranges(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_dimension_and_metric_filters_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_dimension_and_metric_filters 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_dimension_and_metric_filters(capsys): 23 | run_report_with_dimension_and_metric_filters.run_report_with_dimension_and_metric_filters( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_dimension_exclude_filter_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_dimension_exclude_filter 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_dimension_exclude_filter(capsys): 23 | run_report_with_dimension_exclude_filter.run_report_with_dimension_exclude_filter( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_dimension_filter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the usage of 18 | dimension and metric filters in a report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_dimension_filter] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Filter, 29 | FilterExpression, 30 | Metric, 31 | RunReportRequest, 32 | ) 33 | 34 | from run_report import print_run_report_response 35 | 36 | 37 | def run_sample(): 38 | """Runs the sample.""" 39 | # TODO(developer): Replace this variable with your Google Analytics 4 40 | # property ID before running the sample. 41 | property_id = "YOUR-GA4-PROPERTY-ID" 42 | run_report_with_dimension_filter(property_id) 43 | 44 | 45 | def run_report_with_dimension_filter(property_id="YOUR-GA4-PROPERTY-ID"): 46 | """Runs a report using a dimension filter. The call returns a time series 47 | report of `eventCount` when `eventName` is `first_open` for each date. 48 | 49 | This sample uses relative date range values. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange 50 | for more information. 51 | """ 52 | 53 | client = BetaAnalyticsDataClient() 54 | 55 | request = RunReportRequest( 56 | property=f"properties/{property_id}", 57 | dimensions=[Dimension(name="date")], 58 | metrics=[Metric(name="eventCount")], 59 | date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")], 60 | dimension_filter=FilterExpression( 61 | filter=Filter( 62 | field_name="eventName", 63 | string_filter=Filter.StringFilter(value="first_open"), 64 | ) 65 | ), 66 | ) 67 | response = client.run_report(request) 68 | print_run_report_response(response) 69 | 70 | 71 | # [END analyticsdata_run_report_with_dimension_filter] 72 | 73 | 74 | if __name__ == "__main__": 75 | run_sample() 76 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_dimension_filter_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_dimension_filter 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_dimension_filter(capsys): 23 | run_report_with_dimension_filter.run_report_with_dimension_filter(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_dimension_in_list_filter_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_dimension_in_list_filter 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_dimension_in_list_filter(capsys): 23 | run_report_with_dimension_in_list_filter.run_report_with_dimension_in_list_filter( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_multiple_dimension_filters_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_multiple_dimension_filters 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_dimension_filters(capsys): 23 | run_report_with_multiple_dimension_filters.run_report_with_multiple_dimension_filters( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_multiple_dimensions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the creation 18 | of a basic report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_multiple_dimensions] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | RunReportRequest, 30 | ) 31 | 32 | from run_report import print_run_report_response 33 | 34 | 35 | def run_sample(): 36 | """Runs the sample.""" 37 | # TODO(developer): Replace this variable with your Google Analytics 4 38 | # property ID before running the sample. 39 | property_id = "YOUR-GA4-PROPERTY-ID" 40 | run_report_with_multiple_dimensions(property_id) 41 | 42 | 43 | def run_report_with_multiple_dimensions(property_id="YOUR-GA4-PROPERTY-ID"): 44 | """Runs a report of active users grouped by three dimensions.""" 45 | client = BetaAnalyticsDataClient() 46 | 47 | request = RunReportRequest( 48 | property=f"properties/{property_id}", 49 | dimensions=[ 50 | Dimension(name="country"), 51 | Dimension(name="region"), 52 | Dimension(name="city"), 53 | ], 54 | metrics=[Metric(name="activeUsers")], 55 | date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], 56 | ) 57 | response = client.run_report(request) 58 | print_run_report_response(response) 59 | 60 | 61 | # [END analyticsdata_run_report_with_multiple_dimensions] 62 | 63 | if __name__ == "__main__": 64 | run_sample() 65 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_multiple_dimensions_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_multiple_dimensions 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_dimensions(capsys): 23 | run_report_with_multiple_dimensions.run_report_with_multiple_dimensions( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_multiple_metrics.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the creation 18 | of a basic report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_multiple_metrics] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | RunReportRequest, 30 | ) 31 | 32 | from run_report import print_run_report_response 33 | 34 | 35 | def run_sample(): 36 | """Runs the sample.""" 37 | # TODO(developer): Replace this variable with your Google Analytics 4 38 | # property ID before running the sample. 39 | property_id = "YOUR-GA4-PROPERTY-ID" 40 | run_report_with_multiple_metrics(property_id) 41 | 42 | 43 | def run_report_with_multiple_metrics(property_id="YOUR-GA4-PROPERTY-ID"): 44 | """Runs a report of active users, new users and total revenue grouped by 45 | date dimension.""" 46 | client = BetaAnalyticsDataClient() 47 | 48 | # Runs a report of active users grouped by three dimensions. 49 | request = RunReportRequest( 50 | property=f"properties/{property_id}", 51 | dimensions=[Dimension(name="date")], 52 | metrics=[ 53 | Metric(name="activeUsers"), 54 | Metric(name="newUsers"), 55 | Metric(name="totalRevenue"), 56 | ], 57 | date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], 58 | ) 59 | response = client.run_report(request) 60 | print_run_report_response(response) 61 | 62 | 63 | # [END analyticsdata_run_report_with_multiple_metrics] 64 | 65 | if __name__ == "__main__": 66 | run_sample() 67 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_multiple_metrics_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_multiple_metrics 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_multiple_metrics(capsys): 23 | run_report_with_multiple_metrics.run_report_with_multiple_metrics(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_named_date_ranges.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the usage of 18 | date ranges in a report. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange#FIELDS.name 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_named_date_ranges] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | RunReportRequest, 30 | ) 31 | 32 | from run_report import print_run_report_response 33 | 34 | 35 | def run_sample(): 36 | """Runs the sample.""" 37 | # TODO(developer): Replace this variable with your Google Analytics 4 38 | # property ID before running the sample. 39 | property_id = "YOUR-GA4-PROPERTY-ID" 40 | run_report_with_named_date_ranges(property_id) 41 | 42 | 43 | def run_report_with_named_date_ranges(property_id="YOUR-GA4-PROPERTY-ID"): 44 | """Runs a report using named date ranges.""" 45 | client = BetaAnalyticsDataClient() 46 | 47 | request = RunReportRequest( 48 | property=f"properties/{property_id}", 49 | date_ranges=[ 50 | DateRange(start_date="2020-01-01", end_date="2020-01-31", name="year_ago"), 51 | DateRange( 52 | start_date="2021-01-01", end_date="2021-01-31", name="current_year" 53 | ), 54 | ], 55 | dimensions=[Dimension(name="country")], 56 | metrics=[Metric(name="sessions")], 57 | ) 58 | response = client.run_report(request) 59 | print_run_report_response(response) 60 | 61 | 62 | # [END analyticsdata_run_report_with_named_date_ranges] 63 | 64 | 65 | if __name__ == "__main__": 66 | run_sample() 67 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_named_date_ranges_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_named_date_ranges 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_named_date_ranges(capsys): 23 | run_report_with_named_date_ranges.run_report_with_named_date_ranges( 24 | TEST_PROPERTY_ID 25 | ) 26 | out, _ = capsys.readouterr() 27 | assert "Report result" in out 28 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_ordering.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2021 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 application demonstrating the ordering of 18 | report rows. 19 | 20 | See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.order_bys 21 | for more information. 22 | """ 23 | # [START analyticsdata_run_report_with_ordering] 24 | from google.analytics.data_v1beta import BetaAnalyticsDataClient 25 | from google.analytics.data_v1beta.types import ( 26 | DateRange, 27 | Dimension, 28 | Metric, 29 | OrderBy, 30 | RunReportRequest, 31 | ) 32 | 33 | from run_report import print_run_report_response 34 | 35 | 36 | def run_sample(): 37 | """Runs the sample.""" 38 | # TODO(developer): Replace this variable with your Google Analytics 4 39 | # property ID before running the sample. 40 | property_id = "YOUR-GA4-PROPERTY-ID" 41 | run_report_with_ordering(property_id) 42 | 43 | 44 | def run_report_with_ordering(property_id="YOUR-GA4-PROPERTY-ID"): 45 | """Runs a report of active users grouped by three dimensions, ordered by 46 | the total revenue in descending order.""" 47 | client = BetaAnalyticsDataClient() 48 | 49 | request = RunReportRequest( 50 | property=f"properties/{property_id}", 51 | dimensions=[Dimension(name="date")], 52 | metrics=[ 53 | Metric(name="activeUsers"), 54 | Metric(name="newUsers"), 55 | Metric(name="totalRevenue"), 56 | ], 57 | date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], 58 | order_bys=[ 59 | OrderBy(metric=OrderBy.MetricOrderBy(metric_name="totalRevenue"), desc=True) 60 | ], 61 | ) 62 | response = client.run_report(request) 63 | print_run_report_response(response) 64 | 65 | 66 | # [END analyticsdata_run_report_with_ordering] 67 | 68 | 69 | if __name__ == "__main__": 70 | run_sample() 71 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_ordering_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_ordering 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_ordering(capsys): 23 | run_report_with_ordering.run_report_with_ordering(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_pagination_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_pagination 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_pagination(capsys): 23 | run_report_with_pagination.run_report_with_pagination(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Report result" in out 26 | -------------------------------------------------------------------------------- /google-analytics-data/run_report_with_property_quota_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 os 16 | 17 | import run_report_with_property_quota 18 | 19 | TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") 20 | 21 | 22 | def test_run_report_with_property_quota(capsys): 23 | run_report_with_property_quota.run_report_with_property_quota(TEST_PROPERTY_ID) 24 | out, _ = capsys.readouterr() 25 | assert "Tokens per day quota consumed" in out 26 | --------------------------------------------------------------------------------