From f787a0413462698f21448bd2f3ba7d2ac824e7c0 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Wed, 1 Sep 2021 17:51:46 -0700 Subject: [PATCH 01/23] update test configuration --- samples/noxfile_config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index b883a88e..475aaaa9 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -8,13 +8,13 @@ # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a # build specific Cloud project. You can also use your own string # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + "gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT", # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. "envs": { - "GA_TEST_PROPERTY_ID": "222596558", - "GA_TEST_ACCOUNT_ID": "123", + "GA_TEST_PROPERTY_ID": "276206997", + "GA_TEST_ACCOUNT_ID": "199820965", "GA_TEST_USER_LINK_ID": "123", "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "123", "GA_TEST_IOS_APP_DATA_STREAM_ID": "123", From 79093d1980763fbdf57988d56d6e50293077264f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 05:44:25 -0400 Subject: [PATCH 02/23] remove custom noxfile configs for now --- samples/noxfile_config.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 475aaaa9..635b6c6a 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -1,13 +1,4 @@ TEST_CONFIG_OVERRIDE = { - # You can opt out from the test for specific Python versions. - "ignored_versions": ["2.7"], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - "enforce_type_hints": True, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. "gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT", # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any From 8f1f9579293a6c5e665ed3a3028af6ea57767c40 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 09:51:49 +0000 Subject: [PATCH 03/23] remove MaximumUserAccess rom sample --- samples/properties_firebase_links_list.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/samples/properties_firebase_links_list.py b/samples/properties_firebase_links_list.py index 0d5c2f17..560363c3 100644 --- a/samples/properties_firebase_links_list.py +++ b/samples/properties_firebase_links_list.py @@ -22,7 +22,6 @@ """ # [START analyticsadmin_properties_firebase_links_list] from google.analytics.admin import AnalyticsAdminServiceClient -from google.analytics.admin_v1alpha.types import MaximumUserAccess def run_sample(): @@ -49,10 +48,6 @@ def print_firebase_link(firebase_link): """Prints the Firebase link details.""" print(f"Resource name: {firebase_link.name}") print(f"Firebase project: {firebase_link.project}") - print( - f"Maximum user access to the GA4 property: " - f"{MaximumUserAccess(firebase_link.maximum_user_access).name}" - ) print(f"Create time: {firebase_link.create_time}") From b674e0d2597237be4c70ddff0f0255bd3b9dee1b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 05:53:37 -0400 Subject: [PATCH 04/23] add comment in noxfile_config.py --- samples/noxfile_config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 635b6c6a..600de5ec 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -1,4 +1,8 @@ TEST_CONFIG_OVERRIDE = { + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. "gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT", # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any From de94a9595e67d931b6bacd79febf8ada07b72810 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 10:04:42 +0000 Subject: [PATCH 05/23] run blacken session --- samples/noxfile.py | 44 +++++++++++-------- ...ms_update_enhanced_measurement_settings.py | 1 - samples/quickstart.py | 25 ++++++----- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/samples/noxfile.py b/samples/noxfile.py index e73436a1..b008613f 100644 --- a/samples/noxfile.py +++ b/samples/noxfile.py @@ -39,17 +39,15 @@ TEST_CONFIG = { # You can opt out from the test for specific Python versions. - 'ignored_versions': [], - + "ignored_versions": [], # Old samples are opted out of enforcing Python type hints # All new samples should feature them - 'enforce_type_hints': False, - + "enforce_type_hints": False, # An envvar key for determining the project id to use. Change it # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a # build specific Cloud project. You can also use your own string # to use your own Cloud project. - 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT', + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # If you need to use a specific version of pip, # change pip_version_override to the string representation @@ -57,13 +55,13 @@ "pip_version_override": None, # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. - 'envs': {}, + "envs": {}, } try: # Ensure we can import noxfile_config in the project's directory. - sys.path.append('.') + sys.path.append(".") from noxfile_config import TEST_CONFIG_OVERRIDE except ImportError as e: print("No user noxfile_config found: detail: {}".format(e)) @@ -78,12 +76,12 @@ def get_pytest_env_vars() -> Dict[str, str]: ret = {} # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG['gcloud_project_env'] + env_key = TEST_CONFIG["gcloud_project_env"] # This should error out if not set. - ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key] + ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] # Apply user supplied envs. - ret.update(TEST_CONFIG['envs']) + ret.update(TEST_CONFIG["envs"]) return ret @@ -92,11 +90,14 @@ def get_pytest_env_vars() -> Dict[str, str]: ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9"] # Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] +IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) -INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ("True", "true") +INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( + "True", + "true", +) # # Style Checks # @@ -141,7 +142,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]: @nox.session def lint(session: nox.sessions.Session) -> None: - if not TEST_CONFIG['enforce_type_hints']: + if not TEST_CONFIG["enforce_type_hints"]: session.install("flake8", "flake8-import-order") else: session.install("flake8", "flake8-import-order", "flake8-annotations") @@ -150,9 +151,11 @@ def lint(session: nox.sessions.Session) -> None: args = FLAKE8_COMMON_ARGS + [ "--application-import-names", ",".join(local_names), - "." + ".", ] session.run("flake8", *args) + + # # Black # @@ -165,6 +168,7 @@ def blacken(session: nox.sessions.Session) -> None: session.run("black", *python_files) + # # Sample Tests # @@ -173,7 +177,9 @@ def blacken(session: nox.sessions.Session) -> None: PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] -def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None: +def _session_tests( + session: nox.sessions.Session, post_install: Callable = None +) -> None: if TEST_CONFIG["pip_version_override"]: pip_version = TEST_CONFIG["pip_version_override"] session.install(f"pip=={pip_version}") @@ -203,7 +209,7 @@ def _session_tests(session: nox.sessions.Session, post_install: Callable = None) # on travis where slow and flaky tests are excluded. # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html success_codes=[0, 5], - env=get_pytest_env_vars() + env=get_pytest_env_vars(), ) @@ -213,9 +219,9 @@ def py(session: nox.sessions.Session) -> None: if session.python in TESTED_VERSIONS: _session_tests(session) else: - session.skip("SKIPPED: {} tests are disabled for this sample.".format( - session.python - )) + session.skip( + "SKIPPED: {} tests are disabled for this sample.".format(session.python) + ) # diff --git a/samples/properties_web_data_streams_update_enhanced_measurement_settings.py b/samples/properties_web_data_streams_update_enhanced_measurement_settings.py index f02e1fb0..354cebf2 100644 --- a/samples/properties_web_data_streams_update_enhanced_measurement_settings.py +++ b/samples/properties_web_data_streams_update_enhanced_measurement_settings.py @@ -26,7 +26,6 @@ from google.protobuf.field_mask_pb2 import FieldMask - def run_sample(): """Runs the sample.""" diff --git a/samples/quickstart.py b/samples/quickstart.py index 5653af44..1d6605b7 100644 --- a/samples/quickstart.py +++ b/samples/quickstart.py @@ -51,18 +51,21 @@ # [START ga_admin_list_accounts] def list_accounts(): - """Lists the available Google Analytics accounts.""" - from google.analytics.admin import AnalyticsAdminServiceClient - # Using a default constructor instructs the client to use the credentials - # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. - client = AnalyticsAdminServiceClient() - - # Displays the configuration information for all Google Analytics accounts - # available to the authenticated user. - for account in client.list_accounts(): - print(account) + """Lists the available Google Analytics accounts.""" + from google.analytics.admin import AnalyticsAdminServiceClient + + # Using a default constructor instructs the client to use the credentials + # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. + client = AnalyticsAdminServiceClient() + + # Displays the configuration information for all Google Analytics accounts + # available to the authenticated user. + for account in client.list_accounts(): + print(account) + + # [END ga_admin_list_accounts] if __name__ == "__main__": - list_accounts() + list_accounts() From 761d8aa99d1c94929cef786c0c73dd12c492902c Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 10:16:13 +0000 Subject: [PATCH 06/23] lint --- samples/quickstart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/quickstart.py b/samples/quickstart.py index 1d6605b7..0e22289a 100644 --- a/samples/quickstart.py +++ b/samples/quickstart.py @@ -49,6 +49,7 @@ pip install google-analytics-admin """ + # [START ga_admin_list_accounts] def list_accounts(): """Lists the available Google Analytics accounts.""" From ee91693742a93165b4c446bd69ab5249c1a20a61 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 2 Sep 2021 10:27:40 +0000 Subject: [PATCH 07/23] add pytest --- samples/requirements-test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 samples/requirements-test.txt diff --git a/samples/requirements-test.txt b/samples/requirements-test.txt new file mode 100644 index 00000000..9299a7a8 --- /dev/null +++ b/samples/requirements-test.txt @@ -0,0 +1 @@ +pytest==6.2.5 \ No newline at end of file From 7a7c6d2bcfc2bd5d32f98d3209edb7f7e6a1271e Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Mon, 4 Oct 2021 17:51:21 -0700 Subject: [PATCH 08/23] Update noxfile_config.py --- samples/noxfile_config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 600de5ec..04520022 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -10,9 +10,9 @@ "envs": { "GA_TEST_PROPERTY_ID": "276206997", "GA_TEST_ACCOUNT_ID": "199820965", - "GA_TEST_USER_LINK_ID": "123", - "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "123", - "GA_TEST_IOS_APP_DATA_STREAM_ID": "123", - "GA_TEST_WEB_DATA_STREAM_ID": "123", + "GA_TEST_USER_LINK_ID": "105231969274497648555", + "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949", + "GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289", + "GA_TEST_WEB_DATA_STREAM_ID": "2377024575", }, } From 9366fdc3e40b0474f201393b944a2532b93afe91 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Mon, 4 Oct 2021 17:57:39 -0700 Subject: [PATCH 09/23] Update noxfile_config.py --- samples/noxfile_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 04520022..62d635ce 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -13,6 +13,6 @@ "GA_TEST_USER_LINK_ID": "105231969274497648555", "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949", "GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289", - "GA_TEST_WEB_DATA_STREAM_ID": "2377024575", + "GA_TEST_WEB_DATA_STREAM_ID": "2828068992", }, } From a88d15e5c010ed0c2df943a26f3a9e6618c8a97a Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 5 Oct 2021 01:56:51 -0700 Subject: [PATCH 10/23] delete enhanced measurement settings samples as this functionality is no longer supported in v1alpha --- ...reams_get_enhanced_measurement_settings.py | 79 ------------------- ..._get_enhanced_measurement_settings_test.py | 29 ------- ...ms_update_enhanced_measurement_settings.py | 72 ----------------- ...date_enhanced_measurement_settings_test.py | 30 ------- 4 files changed, 210 deletions(-) delete mode 100644 samples/properties_web_data_streams_get_enhanced_measurement_settings.py delete mode 100644 samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py delete mode 100644 samples/properties_web_data_streams_update_enhanced_measurement_settings.py delete mode 100644 samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py diff --git a/samples/properties_web_data_streams_get_enhanced_measurement_settings.py b/samples/properties_web_data_streams_get_enhanced_measurement_settings.py deleted file mode 100644 index 65d7225d..00000000 --- a/samples/properties_web_data_streams_get_enhanced_measurement_settings.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google LLC All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Admin API sample application which prints the enhanced -measurement settings for the web stream. - -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/getEnhancedMeasurementSettings -for more information. -""" -# [START analyticsadmin_properties_web_data_streams_get_enhanced_measurement_settings] -from google.analytics.admin import AnalyticsAdminServiceClient - - -def run_sample(): - """Runs the sample.""" - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID (e.g. "123456") before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - - # TODO(developer): Replace this variable with your web data stream ID - # (e.g. "123456") before running the sample. - stream_id = "YOUR-WEB-DATA-STREAM-ID" - - get_enhanced_measurement_settings(property_id, stream_id) - - -def get_enhanced_measurement_settings(property_id, stream_id): - """Retrieves the enhanced measurement settings for the web stream.""" - client = AnalyticsAdminServiceClient() - enhanced_measurement_settings = client.get_enhanced_measurement_settings( - name=f"properties/{property_id}/webDataStreams/{stream_id}/enhancedMeasurementSettings" - ) - - print("Result:") - print_enhanced_measurement_settings(enhanced_measurement_settings) - - -def print_enhanced_measurement_settings(enhanced_measurement_settings): - """Prints the enhanced measurement settings for a web stream.""" - print(f"Resource name: {enhanced_measurement_settings.name}") - print(f"Stream enabled: {enhanced_measurement_settings.streamEnabled}") - print(f"Page views enabled: {enhanced_measurement_settings.pageViewsEnabled}") - print(f"Scrolls enabled: {enhanced_measurement_settings.scrollsEnabled}") - print( - f"Outbound clicks enabled: {enhanced_measurement_settings.outboundClicksEnabled}" - ) - print(f"Site search enabled: {enhanced_measurement_settings.siteSearchEnabled}") - print( - f"Video engagement enabled: {enhanced_measurement_settings.videoEngagementEnabled}" - ) - print( - f"File downloads enabled: {enhanced_measurement_settings.fileDownloadsEnabled}" - ) - print(f"Page loads enabled: {enhanced_measurement_settings.pageLoadsEnabled}") - print(f"Page changes enabled: {enhanced_measurement_settings.pageChangesEnabled}") - print( - f"Search query parameter: {enhanced_measurement_settings.searchQueryParameter}" - ) - print(f"Uri query parameter: {enhanced_measurement_settings.uriQueryParameter}") - - -# [END analyticsadmin_properties_web_data_streams_get_enhanced_measurement_settings] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py b/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py deleted file mode 100644 index 34d7ccd4..00000000 --- a/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright 2021 Google LLC All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os - -import properties_web_data_streams_get_enhanced_measurement_settings - - -TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") -TEST_WEB_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") - - -def test_properties_web_data_streams_get_enhanced_measurement_settings(capsys): - properties_web_data_streams_get_enhanced_measurement_settings.get_enhanced_measurement_settings( - TEST_PROPERTY_ID, TEST_WEB_DATA_STREAM_ID - ) - out, _ = capsys.readouterr() - assert "Result" in out diff --git a/samples/properties_web_data_streams_update_enhanced_measurement_settings.py b/samples/properties_web_data_streams_update_enhanced_measurement_settings.py deleted file mode 100644 index 354cebf2..00000000 --- a/samples/properties_web_data_streams_update_enhanced_measurement_settings.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google LLC All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Admin API sample application which updates the enhanced -measurement settings for the web stream. - -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/updateEnhancedMeasurementSettings -for more information. -""" -# [START analyticsadmin_properties_web_data_streams_update_enhanced_measurement_settings] -from google.analytics.admin import AnalyticsAdminServiceClient -from google.analytics.admin_v1alpha.types import EnhancedMeasurementSettings -from google.protobuf.field_mask_pb2 import FieldMask - - -def run_sample(): - """Runs the sample.""" - - # !!! ATTENTION !!! - # Running this sample may change/delete your Google Analytics account - # configuration. Make sure to not use the Google Analytics property ID from - # your production environment below. - - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID (e.g. "123456") before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - - # TODO(developer): Replace this variable with your web data stream ID - # (e.g. "123456") before running the sample. - stream_id = "YOUR-WEB-DATA-STREAM-ID" - - update_enhanced_measurement_settings(property_id, stream_id) - - -def update_enhanced_measurement_settings(property_id, stream_id): - """Updates the enhanced measurement settings for the web stream.""" - client = AnalyticsAdminServiceClient() - # This call updates the `streamEnabled`, `fileDownloadsEnabled` measurement - # settings of the web data stream, as indicated by the value of the - # `update_mask` field. The web data stream to update is specified in the - # `name` field of the `EnhancedMeasurementSettings` instance. - enhanced_measurement_settings = client.update_enhanced_measurement_settings( - enhanced_measurement_settings=EnhancedMeasurementSettings( - name=f"properties/{property_id}/webDataStreams/{stream_id}/enhancedMeasurementSettings", - stream_enabled=False, - file_downloads_enabled=False, - ), - update_mask=FieldMask(paths=["stream_enabled", "file_downloads_enabled"]), - ) - - print("Result:") - print(enhanced_measurement_settings) - - -# [END analyticsadmin_properties_web_data_streams_update_enhanced_measurement_settings] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py b/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py deleted file mode 100644 index d692a0eb..00000000 --- a/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2021 Google LLC All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import pytest - -import properties_web_data_streams_update_enhanced_measurement_settings - - -FAKE_PROPERTY_ID = "1" -FAKE_STREAM_ID = "1" - - -def test_properties_web_data_streams_update(): - # This test ensures that the call is valid and reaches the server, even - # though the operation does not succeed due to permission error. - with pytest.raises(Exception, match="403 The caller does not have permission"): - properties_web_data_streams_update_enhanced_measurement_settings.update_enhanced_measurement_settings( - FAKE_PROPERTY_ID, FAKE_STREAM_ID - ) From e76ba965c6d8c715e46854083753a0f8cd43545d Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 5 Oct 2021 02:03:26 -0700 Subject: [PATCH 11/23] fix the samples tests --- samples/noxfile_config.py | 3 ++- samples/properties_ios_app_data_streams_get.py | 2 +- samples/properties_user_links_batch_get_test.py | 2 +- samples/properties_user_links_get_test.py | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 62d635ce..f2184825 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -10,7 +10,8 @@ "envs": { "GA_TEST_PROPERTY_ID": "276206997", "GA_TEST_ACCOUNT_ID": "199820965", - "GA_TEST_USER_LINK_ID": "105231969274497648555", + "GA_TEST_USER_LINK_ID": "103401743041912607932", + "GA_TEST_PROPERTY_USER_LINK_ID": "105231969274497648555", "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949", "GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289", "GA_TEST_WEB_DATA_STREAM_ID": "2828068992", diff --git a/samples/properties_ios_app_data_streams_get.py b/samples/properties_ios_app_data_streams_get.py index 02b775da..50a617af 100644 --- a/samples/properties_ios_app_data_streams_get.py +++ b/samples/properties_ios_app_data_streams_get.py @@ -53,7 +53,7 @@ def print_ios_app_data_stream(ios_app_data_stream): print(f"Resource name: {ios_app_data_stream.name}") print(f"Display name: {ios_app_data_stream.display_name}") print(f"Firebase app ID: {ios_app_data_stream.firebase_app_id}") - print(f"Bundle ID: {ios_app_data_stream.bundleId}") + print(f"Bundle ID: {ios_app_data_stream.bundle_id}") print(f"Create time: {ios_app_data_stream.create_time}") print(f"Update time: {ios_app_data_stream.update_time}") diff --git a/samples/properties_user_links_batch_get_test.py b/samples/properties_user_links_batch_get_test.py index a862070d..4c8c4bb5 100644 --- a/samples/properties_user_links_batch_get_test.py +++ b/samples/properties_user_links_batch_get_test.py @@ -17,7 +17,7 @@ import properties_user_links_batch_get TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") -TEST_USER_LINK_ID = os.getenv("GA_USER_LINK_ID") +TEST_USER_LINK_ID = os.getenv("GA_TEST_PROPERTY_USER_LINK_ID") def test_properties_user_links_batch_get(capsys): diff --git a/samples/properties_user_links_get_test.py b/samples/properties_user_links_get_test.py index dca1ca87..14c99708 100644 --- a/samples/properties_user_links_get_test.py +++ b/samples/properties_user_links_get_test.py @@ -17,7 +17,7 @@ import properties_user_links_get TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") -TEST_USER_LINK_ID = os.getenv("GA_USER_LINK_ID") +TEST_USER_LINK_ID = os.getenv("GA_TEST_PROPERTY_USER_LINK_ID") def test_properties_user_links_get(capsys): From 3836b7255af693beb5c75e0e1ba85fcec39dc383 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 5 Oct 2021 02:09:24 -0700 Subject: [PATCH 12/23] do not use the `maximum_user_access` field and `update` operation in properties.firebase_links tests as this is no longer supported in v1alpha --- samples/properties_firebase_links_create.py | 3 +- samples/properties_firebase_links_update.py | 71 ------------------- .../properties_firebase_links_update_test.py | 30 -------- 3 files changed, 1 insertion(+), 103 deletions(-) delete mode 100644 samples/properties_firebase_links_update.py delete mode 100644 samples/properties_firebase_links_update_test.py diff --git a/samples/properties_firebase_links_create.py b/samples/properties_firebase_links_create.py index d259612d..9dc7c773 100644 --- a/samples/properties_firebase_links_create.py +++ b/samples/properties_firebase_links_create.py @@ -50,8 +50,7 @@ def create_firebase_link(property_id, firebase_project_id): firebase_link = client.create_firebase_link( parent=f"properties/{property_id}", firebase_link=FirebaseLink( - project=f"projects/{firebase_project_id}", - maximum_user_access="READ_AND_ANALYZE", + project=f"projects/{firebase_project_id}" ), ) diff --git a/samples/properties_firebase_links_update.py b/samples/properties_firebase_links_update.py deleted file mode 100644 index 085f74d5..00000000 --- a/samples/properties_firebase_links_update.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2021 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Google Analytics Admin API sample application which updates the Firebase -link on the specified Google Analytics 4 property. - -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/update -for more information. -""" -# [START analyticsadmin_properties_firebase_links_update] -from google.analytics.admin import AnalyticsAdminServiceClient -from google.analytics.admin_v1alpha.types import FirebaseLink -from google.protobuf.field_mask_pb2 import FieldMask - - -def run_sample(): - """Runs the sample.""" - - # !!! ATTENTION !!! - # Running this sample may change/delete your Google Analytics account - # configuration. Make sure to not use the Google Analytics property ID from - # your production environment below. - - # TODO(developer): Replace this variable with your Google Analytics 4 - # property ID (e.g. "123456") before running the sample. - property_id = "YOUR-GA4-PROPERTY-ID" - - # TODO(developer): Replace this variable with your Firebase link ID - # (e.g. "123456") before running the sample. - firebase_link_id = "YOUR-FIREBASE-LINK-ID" - - update_firebase_link(property_id, firebase_link_id) - - -def update_firebase_link(property_id, firebase_link_id): - """Updates the Firebase link.""" - client = AnalyticsAdminServiceClient() - # This call updates the maximum user access to the GA4 property of the - # Firebase link as indicated by the value of the `update_mask` field. - # The Firebase link to update is specified in the `name` field of the - # `FirebaseLink` instance. - firebase_link = client.update_firebase_link( - firebase_link=FirebaseLink( - name=f"properties/{property_id}/firebaseLinks/{firebase_link_id}", - maximum_user_access="EDITOR_WITHOUT_LINK_MANAGEMENT", - ), - update_mask=FieldMask(paths=["maximum_user_access"]), - ) - - print("Result:") - print(firebase_link) - - -# [END analyticsadmin_properties_firebase_links_update] - - -if __name__ == "__main__": - run_sample() diff --git a/samples/properties_firebase_links_update_test.py b/samples/properties_firebase_links_update_test.py deleted file mode 100644 index dc07fb60..00000000 --- a/samples/properties_firebase_links_update_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2021 Google LLC All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import pytest - -import properties_firebase_links_update - - -FAKE_PROPERTY_ID = "1" -FAKE_FIREBASE_LINK_ID = "1" - - -def test_properties_firebase_links_update(): - # This test ensures that the call is valid and reaches the server, even - # though the operation does not succeed due to permission error. - with pytest.raises(Exception, match="403 The caller does not have permission"): - properties_firebase_links_update.update_firebase_link( - FAKE_PROPERTY_ID, FAKE_FIREBASE_LINK_ID - ) From d235358cb403231dd0ff3f5dbf68f6c251c2614e Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 5 Oct 2021 02:12:54 -0700 Subject: [PATCH 13/23] use `creator_email_address` instead of `email_address` field in properties.google_ads_links_list() test as the field has been renamed in v1alpha --- samples/properties_google_ads_links_list.py | 2 +- samples/properties_google_ads_links_list_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/properties_google_ads_links_list.py b/samples/properties_google_ads_links_list.py index 1aa7e065..7109f1e3 100644 --- a/samples/properties_google_ads_links_list.py +++ b/samples/properties_google_ads_links_list.py @@ -50,7 +50,7 @@ def print_google_ads_link(google_ads_link): print(f"Google Ads customer ID: {google_ads_link.customer_id}") print(f"Can manage clients: {google_ads_link.can_manage_clients}") print(f"Ads personalization enabled: {google_ads_link.ads_personalization_enabled}") - print(f"Email address of the link creator: {google_ads_link.email_address}") + print(f"Email address of the link creator: {google_ads_link.creator_email_address}") print(f"Create time: {google_ads_link.create_time}") print(f"Update time: {google_ads_link.update_time}") diff --git a/samples/properties_google_ads_links_list_test.py b/samples/properties_google_ads_links_list_test.py index 1f850a4f..5396da0e 100644 --- a/samples/properties_google_ads_links_list_test.py +++ b/samples/properties_google_ads_links_list_test.py @@ -19,7 +19,7 @@ TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") -def test_properties_firebase_links_list(capsys): +def test_properties_google_ads_links_list(capsys): properties_google_ads_links_list.list_google_ads_links(TEST_PROPERTY_ID) out, _ = capsys.readouterr() assert "Result" in out From d5b923afacf4a17a5c367e74092aa08bf8aa16e0 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 5 Oct 2021 02:13:34 -0700 Subject: [PATCH 14/23] fix the samples test --- samples/noxfile.py | 3 --- samples/properties_firebase_links_create.py | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/samples/noxfile.py b/samples/noxfile.py index 1b76bef8..1fd8956f 100644 --- a/samples/noxfile.py +++ b/samples/noxfile.py @@ -98,13 +98,10 @@ def get_pytest_env_vars() -> Dict[str, str]: "True", "true", ) -<<<<<<<<< Temporary merge branch 1 -========= # Error if a python version is missing nox.options.error_on_missing_interpreters = True ->>>>>>>>> Temporary merge branch 2 # # Style Checks # diff --git a/samples/properties_firebase_links_create.py b/samples/properties_firebase_links_create.py index 9dc7c773..843179e1 100644 --- a/samples/properties_firebase_links_create.py +++ b/samples/properties_firebase_links_create.py @@ -49,9 +49,7 @@ def create_firebase_link(property_id, firebase_project_id): client = AnalyticsAdminServiceClient() firebase_link = client.create_firebase_link( parent=f"properties/{property_id}", - firebase_link=FirebaseLink( - project=f"projects/{firebase_project_id}" - ), + firebase_link=FirebaseLink(project=f"projects/{firebase_project_id}"), ) print("Result:") From f7c0cd6bebbe74574f570c7e40606e04731ee1b6 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 12 Oct 2021 22:27:35 -0700 Subject: [PATCH 15/23] add samples for Measurement Protocol Secrets management methods --- ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 64 +++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 31 ++++++++ ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 64 +++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 31 ++++++++ ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 59 ++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 30 ++++++++ 30 files changed, 1428 insertions(+) create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..f7e6b9d7 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the Android app data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..a373f865 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..50426ab2 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/delete +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + delete_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): + """Deletes the measurement protocol secret for the Android app data + stream.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + print("Measurement protocol secret deleted") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..5f17bced --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..c26b8f5a --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..7b4bbca1 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_android_app_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_SECRET_ID") + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_get(capsys): + properties_android_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..4fd17b7a --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the Android app data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..e2f417f5 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_android_app_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID") + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_list(capsys): + properties_android_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..9370cd6d --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + android_app_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..67fd9540 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..53da458f --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/iosAppDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..cbacd2dc --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..8a5a5bf7 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/delete +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + delete_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): + """Deletes the measurement protocol secret for the web data + stream.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + print("Measurement protocol secret deleted") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..0070d007 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..75f20eed --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..28a5b15e --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_ios_app_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_IOS_APP_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_IOS_APP_DATA_SECRET_ID") + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_get(capsys): + properties_ios_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..689313f5 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/iosAppDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..fe9869ca --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_ios_app_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_IOS_APP_DATA_STREAM_ID = os.getenv("GA_TEST_IOS_APP_DATA_STREAM_ID") + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_list(capsys): + properties_ios_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_IOS_APP_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..a008bbed --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + ios_app_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..7c850fa6 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_create.py b/samples/properties_web_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..d0a9ee19 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the web data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/webDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..1fb76f38 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..83c087ed --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the web data +stream from the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/delete +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + delete_measurement_protocol_secret(property_id, stream_id) + + +def delete_measurement_protocol_secret(property_id, stream_id): + """Deletes the web data stream from the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + print("Web data stream deleted") + + +# [END analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..df5c48fc --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_get.py b/samples/properties_web_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..78b8235b --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..6e1f5307 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_web_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_WEB_DATA_SECRET_ID") + + +def test_properties_web_data_streams_measurement_protocol_secrets_get(capsys): + properties_web_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_list.py b/samples/properties_web_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..cf123448 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the web data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..87d70689 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_web_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") + + +def test_properties_web_data_streams_measurement_protocol_secrets_list(capsys): + properties_web_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_update.py b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..2423ec5d --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + web_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..8a4d4589 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_update + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) From cd55ddf2b60c4107cf3c682ec59115e93d65a14f Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 12 Oct 2021 22:27:35 -0700 Subject: [PATCH 16/23] add samples for Measurement Protocol Secrets management methods --- samples/noxfile_config.py | 3 + ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 64 +++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 31 ++++++++ ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 64 +++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 31 ++++++++ ...ams_measurement_protocol_secrets_create.py | 67 ++++++++++++++++ ...easurement_protocol_secrets_create_test.py | 30 ++++++++ ...ams_measurement_protocol_secrets_delete.py | 59 ++++++++++++++ ...easurement_protocol_secrets_delete_test.py | 31 ++++++++ ...treams_measurement_protocol_secrets_get.py | 61 +++++++++++++++ ...s_measurement_protocol_secrets_get_test.py | 29 +++++++ ...reams_measurement_protocol_secrets_list.py | 59 ++++++++++++++ ..._measurement_protocol_secrets_list_test.py | 29 +++++++ ...ams_measurement_protocol_secrets_update.py | 77 +++++++++++++++++++ ...easurement_protocol_secrets_update_test.py | 30 ++++++++ 31 files changed, 1431 insertions(+) create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_create.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_delete.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_get.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_list.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_update.py create mode 100644 samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index f2184825..78f96691 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -15,5 +15,8 @@ "GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949", "GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289", "GA_TEST_WEB_DATA_STREAM_ID": "2828068992", + "GA_TEST_ANDROID_APP_DATA_SECRET_ID": "2994941777", + "GA_TEST_IOS_APP_DATA_SECRET_ID": "2994979281", + "GA_TEST_WEB_DATA_SECRET_ID": "2994983412" }, } diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..f7e6b9d7 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the Android app data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..a373f865 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..50426ab2 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/delete +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + delete_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): + """Deletes the measurement protocol secret for the Android app data + stream.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + print("Measurement protocol secret deleted") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..5f17bced --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..c26b8f5a --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..7b4bbca1 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_android_app_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_SECRET_ID") + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_get(capsys): + properties_android_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..4fd17b7a --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the Android app data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..e2f417f5 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_android_app_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID") + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_list(capsys): + properties_android_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..9370cd6d --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + android_app_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..67fd9540 --- /dev/null +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_android_app_data_streams_measurement_protocol_secrets_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_android_app_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_android_app_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..53da458f --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/iosAppDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..cbacd2dc --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..8a5a5bf7 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/delete +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + delete_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): + """Deletes the measurement protocol secret for the web data + stream.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + print("Measurement protocol secret deleted") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..0070d007 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..75f20eed --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..28a5b15e --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_ios_app_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_IOS_APP_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_IOS_APP_DATA_SECRET_ID") + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_get(capsys): + properties_ios_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..689313f5 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/iosAppDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..fe9869ca --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_ios_app_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_IOS_APP_DATA_STREAM_ID = os.getenv("GA_TEST_IOS_APP_DATA_STREAM_ID") + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_list(capsys): + properties_ios_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_IOS_APP_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..a008bbed --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the iOS app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + ios_app_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_ios_app_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..7c850fa6 --- /dev/null +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_ios_app_data_streams_measurement_protocol_secrets_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_ios_app_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_ios_app_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_create.py b/samples/properties_web_data_streams_measurement_protocol_secrets_create.py new file mode 100644 index 00000000..d0a9ee19 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_create.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a measurement +protocol secret for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/create +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + create_measurement_protocol_secret(property_id, stream_id) + + +def create_measurement_protocol_secret(property_id, stream_id): + """Creates a measurement protocol secret for the web data stream.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.create_measurement_protocol_secret( + parent=f"properties/{property_id}/webDataStreams/{stream_id}", + measurement_protocol_secret=MeasurementProtocolSecret( + display_name="New secret" + ), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py new file mode 100644 index 00000000..1fb76f38 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_create_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_create + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py new file mode 100644 index 00000000..83c087ed --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the web data +stream from the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/delete +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + delete_measurement_protocol_secret(property_id, stream_id) + + +def delete_measurement_protocol_secret(property_id, stream_id): + """Deletes the web data stream from the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_measurement_protocol_secret( + name=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + print("Web data stream deleted") + + +# [END analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py new file mode 100644 index 00000000..df5c48fc --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete_test.py @@ -0,0 +1,31 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_get.py b/samples/properties_web_data_streams_measurement_protocol_secrets_get.py new file mode 100644 index 00000000..78b8235b --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which retrieves the details +for the measurement protocol secret. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/get +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + get_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def get_measurement_protocol_secret(property_id, stream_id, secret_id): + """Retrieves the details for the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + measurement_protocol_secret = client.get_measurement_protocol_secret( + name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py new file mode 100644 index 00000000..6e1f5307 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_get_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_web_data_streams_measurement_protocol_secrets_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") +TEST_SECRET_ID = os.getenv("GA_TEST_WEB_DATA_SECRET_ID") + + +def test_properties_web_data_streams_measurement_protocol_secrets_get(capsys): + properties_web_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret( + TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_list.py b/samples/properties_web_data_streams_measurement_protocol_secrets_list.py new file mode 100644 index 00000000..cf123448 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_list.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which lists measurement +protocol secrets for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/list +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + list_measurement_protocol_secrets(property_id, stream_id) + + +def list_measurement_protocol_secrets(property_id, stream_id): + """Lists measurement protocol secrets for the web data stream.""" + client = AnalyticsAdminServiceClient() + results = client.list_measurement_protocol_secrets( + parent=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + + print("Result:") + for measurement_protocol_secret in results: + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + print() + + +# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py new file mode 100644 index 00000000..87d70689 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_list_test.py @@ -0,0 +1,29 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import properties_web_data_streams_measurement_protocol_secrets_list + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") + + +def test_properties_web_data_streams_measurement_protocol_secrets_list(capsys): + properties_web_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets( + TEST_PROPERTY_ID, TEST_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_update.py b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py new file mode 100644 index 00000000..2423ec5d --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the measurement +protocol secret for the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/patch +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import MeasurementProtocolSecret +from google.protobuf.field_mask_pb2 import FieldMask + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with your web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + + update_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def update_measurement_protocol_secret(property_id, stream_id, secret_id): + """Updates the measurement protocol secret.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the measurement protocol secret, as + # indicated by the value of the `update_mask` field. The measurement + # protocol secret to update is specified in the `name` field of the + # `MeasurementProtocolSecret` instance. + measurement_protocol_secret = client.update_measurement_protocol_secret( + web_data_stream=MeasurementProtocolSecret( + name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", + display_name="This is an updated measurement protocol secret", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(f"Resource name: {measurement_protocol_secret.name}") + print(f"Secret value: {measurement_protocol_secret.secret_value}") + print(f"Display name: {measurement_protocol_secret.display_name}") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py b/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py new file mode 100644 index 00000000..8a4d4589 --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_update_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import properties_web_data_streams_measurement_protocol_secrets_update + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" +FAKE_SECRET_ID = "1" + + +def test_properties_web_data_streams_measurement_protocol_secrets_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_web_data_streams_measurement_protocol_secrets_update.update_measurement_protocol_secret( + FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID + ) From bf19bbb88d64d90a52efb316dd47aabc8e737c7c Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Wed, 13 Oct 2021 01:07:20 -0700 Subject: [PATCH 17/23] add samples for Measurement Protocol Secrets management methods --- ...data_streams_measurement_protocol_secrets_update.py | 2 +- ...data_streams_measurement_protocol_secrets_update.py | 2 +- ...data_streams_measurement_protocol_secrets_delete.py | 10 +++++++--- ...data_streams_measurement_protocol_secrets_update.py | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py index 9370cd6d..12b3c9bc 100644 --- a/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py +++ b/samples/properties_android_app_data_streams_measurement_protocol_secrets_update.py @@ -57,7 +57,7 @@ def update_measurement_protocol_secret(property_id, stream_id, secret_id): # protocol secret to update is specified in the `name` field of the # `MeasurementProtocolSecret` instance. measurement_protocol_secret = client.update_measurement_protocol_secret( - android_app_data_stream=MeasurementProtocolSecret( + measurement_protocol_secret=MeasurementProtocolSecret( name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", display_name="This is an updated measurement protocol secret", ), diff --git a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py index a008bbed..b5cbfd05 100644 --- a/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py +++ b/samples/properties_ios_app_data_streams_measurement_protocol_secrets_update.py @@ -57,7 +57,7 @@ def update_measurement_protocol_secret(property_id, stream_id, secret_id): # protocol secret to update is specified in the `name` field of the # `MeasurementProtocolSecret` instance. measurement_protocol_secret = client.update_measurement_protocol_secret( - ios_app_data_stream=MeasurementProtocolSecret( + measurement_protocol_secret=MeasurementProtocolSecret( name=f"properties/{property_id}/iosAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", display_name="This is an updated measurement protocol secret", ), diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index 83c087ed..a3c99141 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -40,14 +40,18 @@ def run_sample(): # (e.g. "123456") before running the sample. stream_id = "YOUR-WEB-DATA-STREAM-ID" - delete_measurement_protocol_secret(property_id, stream_id) + # TODO(developer): Replace this variable with your measurement protocol + # secret ID (e.g. "123456") before running the sample. + secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID" + delete_measurement_protocol_secret(property_id, stream_id, secret_id) -def delete_measurement_protocol_secret(property_id, stream_id): + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): """Deletes the web data stream from the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() client.delete_measurement_protocol_secret( - name=f"properties/{property_id}/webDataStreams/{stream_id}" + name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" ) print("Web data stream deleted") diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_update.py b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py index 2423ec5d..32bd92c6 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_update.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_update.py @@ -57,7 +57,7 @@ def update_measurement_protocol_secret(property_id, stream_id, secret_id): # protocol secret to update is specified in the `name` field of the # `MeasurementProtocolSecret` instance. measurement_protocol_secret = client.update_measurement_protocol_secret( - web_data_stream=MeasurementProtocolSecret( + measurement_protocol_secret=MeasurementProtocolSecret( name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}", display_name="This is an updated measurement protocol secret", ), From 5b10eef30e3281313caca032209056a947971319 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 03:59:46 -0400 Subject: [PATCH 18/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...ties_web_data_streams_measurement_protocol_secrets_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index a3c99141..9c9a982d 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -48,7 +48,7 @@ def run_sample(): def delete_measurement_protocol_secret(property_id, stream_id, secret_id): - """Deletes the web data stream from the Google Analytics 4 property.""" + """Deletes a measurement protocol secret for the web data stream.""" client = AnalyticsAdminServiceClient() client.delete_measurement_protocol_secret( name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" From 267033a70a842eb7304af744c6d060e137470b6f Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 03:59:56 -0400 Subject: [PATCH 19/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...ties_web_data_streams_measurement_protocol_secrets_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index 9c9a982d..00d96813 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -17,7 +17,7 @@ """Google Analytics Admin API sample application which deletes the web data stream from the Google Analytics 4 property. -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/delete +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/delete for more information. """ # [START analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] From e30b7cab989c2acc5a1bb25221f2a9ad1f472cee Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 04:00:03 -0400 Subject: [PATCH 20/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...ties_web_data_streams_measurement_protocol_secrets_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index 00d96813..e6996bc0 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -20,7 +20,7 @@ See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/delete for more information. """ -# [START analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] +# [START analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_delete] from google.analytics.admin import AnalyticsAdminServiceClient From 9d674e02e82a9d0cfd33d30f14060473af07da06 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 04:00:15 -0400 Subject: [PATCH 21/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...ties_web_data_streams_measurement_protocol_secrets_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index e6996bc0..b7a20ea8 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -56,7 +56,7 @@ def delete_measurement_protocol_secret(property_id, stream_id, secret_id): print("Web data stream deleted") -# [END analyticsadmin_properties_web_data_streams_streams_measurement_protocol_delete] +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_delete] if __name__ == "__main__": From 505fdc58ad34fbf0862f98d253b3c4bb17ee6793 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 04:00:22 -0400 Subject: [PATCH 22/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...ties_web_data_streams_measurement_protocol_secrets_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index b7a20ea8..0f98c255 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -53,7 +53,7 @@ def delete_measurement_protocol_secret(property_id, stream_id, secret_id): client.delete_measurement_protocol_secret( name=f"properties/{property_id}/webDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}" ) - print("Web data stream deleted") + print("Measurement protocol secret deleted") # [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_delete] From 53ff7fe2d8bf7af6ff6d534fafb97c7c1c73f39d Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 04:00:30 -0400 Subject: [PATCH 23/23] Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou --- ...es_web_data_streams_measurement_protocol_secrets_delete.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py index 0f98c255..527de4ef 100644 --- a/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Analytics Admin API sample application which deletes the web data -stream from the Google Analytics 4 property. +"""Google Analytics Admin API sample application which deletes a measurement +protocol secret for the web data stream. See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams.measurementProtocolSecrets/delete for more information.