diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index 3c8359bf..04d745d9 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -15,6 +15,9 @@ "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", "GA_TEST_CONVERSION_EVENT_ID": "2719963095" }, } 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..12b3c9bc --- /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( + measurement_protocol_secret=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..b5cbfd05 --- /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( + measurement_protocol_secret=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..527de4ef --- /dev/null +++ b/samples/properties_web_data_streams_measurement_protocol_secrets_delete.py @@ -0,0 +1,63 @@ +#!/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 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. +""" +# [START analyticsadmin_properties_web_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 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" + + delete_measurement_protocol_secret(property_id, stream_id, secret_id) + + +def delete_measurement_protocol_secret(property_id, stream_id, secret_id): + """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}" + ) + print("Measurement protocol secret deleted") + + +# [END analyticsadmin_properties_web_data_streams_measurement_protocol_secrets_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..32bd92c6 --- /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( + measurement_protocol_secret=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 + )