From 126f2711c9fdedaa7cddfe8b3c7bdaff03d0297e Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Tue, 19 Oct 2021 04:10:23 -0400 Subject: [PATCH] docs(samples): add samples for Conversion Event management methods (#153) * update test configuration * remove custom noxfile configs for now * remove MaximumUserAccess rom sample * add comment in noxfile_config.py * run blacken session * lint * add pytest * Update noxfile_config.py * Update noxfile_config.py * delete enhanced measurement settings samples as this functionality is no longer supported in v1alpha * fix the samples tests * do not use the `maximum_user_access` field and `update` operation in properties.firebase_links tests as this is no longer supported in v1alpha * use `creator_email_address` instead of `email_address` field in properties.google_ads_links_list() test as the field has been renamed in v1alpha * fix the samples test * add samples for Conversion Event management methods * add samples for Conversion Event management methods Co-authored-by: Anthonios Partheniou --- samples/noxfile_config.py | 1 + .../properties_conversion_events_create.py | 62 +++++++++++++++++++ ...roperties_conversion_events_create_test.py | 26 ++++++++ .../properties_conversion_events_delete.py | 60 ++++++++++++++++++ ...roperties_conversion_events_delete_test.py | 29 +++++++++ samples/properties_conversion_events_get.py | 59 ++++++++++++++++++ .../properties_conversion_events_get_test.py | 28 +++++++++ samples/properties_conversion_events_list.py | 55 ++++++++++++++++ .../properties_conversion_events_list_test.py | 25 ++++++++ 9 files changed, 345 insertions(+) create mode 100644 samples/properties_conversion_events_create.py create mode 100644 samples/properties_conversion_events_create_test.py create mode 100644 samples/properties_conversion_events_delete.py create mode 100644 samples/properties_conversion_events_delete_test.py create mode 100644 samples/properties_conversion_events_get.py create mode 100644 samples/properties_conversion_events_get_test.py create mode 100644 samples/properties_conversion_events_list.py create mode 100644 samples/properties_conversion_events_list_test.py diff --git a/samples/noxfile_config.py b/samples/noxfile_config.py index f2184825..3c8359bf 100644 --- a/samples/noxfile_config.py +++ b/samples/noxfile_config.py @@ -15,5 +15,6 @@ "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_CONVERSION_EVENT_ID": "2719963095" }, } diff --git a/samples/properties_conversion_events_create.py b/samples/properties_conversion_events_create.py new file mode 100644 index 00000000..4c00dd1b --- /dev/null +++ b/samples/properties_conversion_events_create.py @@ -0,0 +1,62 @@ +#!/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 creates a conversion +event for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/create +for more information. +""" +# [START analyticsadmin_properties_conversion_events_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha import ConversionEvent + + +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 account 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" + + create_conversion_event(property_id) + + +def create_conversion_event(property_id): + """Creates a conversion event for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + conversion_event = client.create_conversion_event( + parent=f"properties/{property_id}", + conversion_event=ConversionEvent(event_name="test_purchase"), + ) + + print("Result:") + print(f"Resource name: {conversion_event.name}") + print(f"Event name: {conversion_event.event_name}") + print(f"Create time: {conversion_event.create_time}") + print(f"Deletable: {conversion_event.deletable}") + print(f"Custom: {conversion_event.custom}") + + +# [END analyticsadmin_properties_conversion_events_create] + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_conversion_events_create_test.py b/samples/properties_conversion_events_create_test.py new file mode 100644 index 00000000..7e2c1ad1 --- /dev/null +++ b/samples/properties_conversion_events_create_test.py @@ -0,0 +1,26 @@ +# 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_conversion_events_create + +FAKE_PROPERTY_ID = "1" + + +def test_properties_conversion_events_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_conversion_events_create.create_conversion_event(FAKE_PROPERTY_ID) diff --git a/samples/properties_conversion_events_delete.py b/samples/properties_conversion_events_delete.py new file mode 100644 index 00000000..19d2fc3e --- /dev/null +++ b/samples/properties_conversion_events_delete.py @@ -0,0 +1,60 @@ +#!/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 conversion +event for the Google Analytics 4 property. + + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/delete +for more information. +""" +# [START analyticsadmin_properties_conversion_events_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 conversion event ID + # (e.g. "123456") before running the sample. + conversion_event_id = "YOUR-CONVERSION-EVENT-ID" + + delete_conversion_event(property_id, conversion_event_id) + + +def delete_conversion_event(property_id, conversion_event_id): + """Deletes the conversion event for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_conversion_event( + name=f"properties/{property_id}/conversionEvents/{conversion_event_id}" + ) + print("Conversion event deleted") + + +# [END analyticsadmin_properties_conversion_events_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_conversion_events_delete_test.py b/samples/properties_conversion_events_delete_test.py new file mode 100644 index 00000000..ba42e4e7 --- /dev/null +++ b/samples/properties_conversion_events_delete_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 pytest + +import properties_conversion_events_delete + +FAKE_PROPERTY_ID = "1" +FAKE_CONVERSION_EVENT_ID = "1" + + +def test_properties_conversion_events_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_conversion_events_delete.delete_conversion_event( + FAKE_PROPERTY_ID, FAKE_CONVERSION_EVENT_ID + ) diff --git a/samples/properties_conversion_events_get.py b/samples/properties_conversion_events_get.py new file mode 100644 index 00000000..0c74a399 --- /dev/null +++ b/samples/properties_conversion_events_get.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 prints the conversion +event details. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/get +for more information. +""" +# [START analyticsadmin_properties_conversion_events_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 conversion event ID + # (e.g. "123456") before running the sample. + conversion_event_id = "YOUR-CONVERSION-EVENT-ID" + + get_conversion_event(property_id, conversion_event_id) + + +def get_conversion_event(property_id, conversion_event_id): + """Retrieves the details for the conversion event.""" + client = AnalyticsAdminServiceClient() + conversion_event = client.get_conversion_event( + name=f"properties/{property_id}/conversionEvents/{conversion_event_id}" + ) + + print("Result:") + print(f"Resource name: {conversion_event.name}") + print(f"Event name: {conversion_event.event_name}") + print(f"Create time: {conversion_event.create_time}") + print(f"Deletable: {conversion_event.deletable}") + print(f"Custom: {conversion_event.custom}") + + +# [END analyticsadmin_properties_conversion_events_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_conversion_events_get_test.py b/samples/properties_conversion_events_get_test.py new file mode 100644 index 00000000..5faac8d6 --- /dev/null +++ b/samples/properties_conversion_events_get_test.py @@ -0,0 +1,28 @@ +# 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_conversion_events_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_CONVERSION_EVENT_ID = os.getenv("GA_TEST_CONVERSION_EVENT_ID") + + +def test_properties_conversion_events_get(capsys): + properties_conversion_events_get.get_conversion_event( + TEST_PROPERTY_ID, TEST_CONVERSION_EVENT_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_conversion_events_list.py b/samples/properties_conversion_events_list.py new file mode 100644 index 00000000..71a82af8 --- /dev/null +++ b/samples/properties_conversion_events_list.py @@ -0,0 +1,55 @@ +#!/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 lists conversion events +for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/list +for more information. +""" +# [START analyticsadmin_properties_conversion_events_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" + + list_conversion_events(property_id) + + +def list_conversion_events(property_id): + """Lists conversion events for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + results = client.list_conversion_events(parent=f"properties/{property_id}") + + print("Result:") + for conversion_event in results: + print(f"Resource name: {conversion_event.name}") + print(f"Event name: {conversion_event.event_name}") + print(f"Create time: {conversion_event.create_time}") + print(f"Deletable: {conversion_event.deletable}") + print(f"Custom: {conversion_event.custom}") + print() + + +# [END analyticsadmin_properties_conversion_events_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_conversion_events_list_test.py b/samples/properties_conversion_events_list_test.py new file mode 100644 index 00000000..e93405ab --- /dev/null +++ b/samples/properties_conversion_events_list_test.py @@ -0,0 +1,25 @@ +# 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_conversion_events_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_conversion_events_list(capsys): + properties_conversion_events_list.list_conversion_events(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out