From bdb85bee0125db8199d6a2a3cf18fbcbd443070b Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Thu, 3 Jun 2021 10:06:44 -0700 Subject: [PATCH] docs: add samples for Google Analytics property management methods (#74) * docs: add Admin API samples for account management methods * update copyright and remove redundant run_sample method * update noxfile template and set enforce_type_hints=False * add type annotations * docs: add Admin API samples for account user link management methods * fix the copyright string, avoid importing functions from other samples * docs: add samples for Google Analytics property management methods --- samples/properties_create.py | 63 ++++++++++++++++ samples/properties_create_test.py | 27 +++++++ samples/properties_delete.py | 52 ++++++++++++++ samples/properties_delete_test.py | 27 +++++++ samples/properties_firebase_links_create.py | 65 +++++++++++++++++ .../properties_firebase_links_create_test.py | 30 ++++++++ samples/properties_firebase_links_delete.py | 59 +++++++++++++++ .../properties_firebase_links_delete_test.py | 30 ++++++++ samples/properties_firebase_links_list.py | 63 ++++++++++++++++ .../properties_firebase_links_list_test.py | 25 +++++++ samples/properties_firebase_links_update.py | 71 +++++++++++++++++++ .../properties_firebase_links_update_test.py | 30 ++++++++ samples/properties_get.py | 66 +++++++++++++++++ samples/properties_get_test.py | 26 +++++++ samples/properties_google_ads_links_create.py | 63 ++++++++++++++++ ...properties_google_ads_links_create_test.py | 30 ++++++++ samples/properties_google_ads_links_delete.py | 59 +++++++++++++++ ...properties_google_ads_links_delete_test.py | 30 ++++++++ samples/properties_google_ads_links_list.py | 62 ++++++++++++++++ .../properties_google_ads_links_list_test.py | 25 +++++++ samples/properties_google_ads_links_update.py | 70 ++++++++++++++++++ ...properties_google_ads_links_update_test.py | 30 ++++++++ samples/properties_list.py | 55 ++++++++++++++ samples/properties_list_test.py | 25 +++++++ samples/properties_update.py | 67 +++++++++++++++++ samples/properties_update_test.py | 27 +++++++ 26 files changed, 1177 insertions(+) create mode 100644 samples/properties_create.py create mode 100644 samples/properties_create_test.py create mode 100644 samples/properties_delete.py create mode 100644 samples/properties_delete_test.py create mode 100644 samples/properties_firebase_links_create.py create mode 100644 samples/properties_firebase_links_create_test.py create mode 100644 samples/properties_firebase_links_delete.py create mode 100644 samples/properties_firebase_links_delete_test.py create mode 100644 samples/properties_firebase_links_list.py create mode 100644 samples/properties_firebase_links_list_test.py create mode 100644 samples/properties_firebase_links_update.py create mode 100644 samples/properties_firebase_links_update_test.py create mode 100644 samples/properties_get.py create mode 100644 samples/properties_get_test.py create mode 100644 samples/properties_google_ads_links_create.py create mode 100644 samples/properties_google_ads_links_create_test.py create mode 100644 samples/properties_google_ads_links_delete.py create mode 100644 samples/properties_google_ads_links_delete_test.py create mode 100644 samples/properties_google_ads_links_list.py create mode 100644 samples/properties_google_ads_links_list_test.py create mode 100644 samples/properties_google_ads_links_update.py create mode 100644 samples/properties_google_ads_links_update_test.py create mode 100644 samples/properties_list.py create mode 100644 samples/properties_list_test.py create mode 100644 samples/properties_update.py create mode 100644 samples/properties_update_test.py diff --git a/samples/properties_create.py b/samples/properties_create.py new file mode 100644 index 00000000..a7bbd796 --- /dev/null +++ b/samples/properties_create.py @@ -0,0 +1,63 @@ +#!/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 Google +Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create +for more information. +""" +# [START analyticsadmin_properties_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import Property + + +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 + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + create_property(account_id) + + +def create_property(account_id): + """Creates a Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + property_ = client.create_property( + property=Property( + parent=f"accounts/{account_id}", + currency_code="USD", + display_name="Test property", + industry_category="OTHER", + time_zone="America/Los_Angeles", + ) + ) + + print("Result:") + print(property_) + + +# [END analyticsadmin_properties_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_create_test.py b/samples/properties_create_test.py new file mode 100644 index 00000000..43b0b106 --- /dev/null +++ b/samples/properties_create_test.py @@ -0,0 +1,27 @@ +# 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_create + + +FAKE_ACCOUNT_ID = "1" + + +def test_properties_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_create.create_property(FAKE_ACCOUNT_ID) diff --git a/samples/properties_delete.py b/samples/properties_delete.py new file mode 100644 index 00000000..e6defc6c --- /dev/null +++ b/samples/properties_delete.py @@ -0,0 +1,52 @@ +#!/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 Google +Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/delete +for more information. +""" +# [START analyticsadmin_properties_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" + delete_property(property_id) + + +def delete_property(property_id): + """Deletes the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_property(name=f"properties/{property_id}") + print("Property deleted") + + +# [END analyticsadmin_properties_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_delete_test.py b/samples/properties_delete_test.py new file mode 100644 index 00000000..5d3cf741 --- /dev/null +++ b/samples/properties_delete_test.py @@ -0,0 +1,27 @@ +# 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_delete + + +FAKE_PROPERTY_ID = "1" + + +def test_properties_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_delete.delete_property(FAKE_PROPERTY_ID) diff --git a/samples/properties_firebase_links_create.py b/samples/properties_firebase_links_create.py new file mode 100644 index 00000000..d259612d --- /dev/null +++ b/samples/properties_firebase_links_create.py @@ -0,0 +1,65 @@ +#!/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 Firebase link +for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/create +for more information. +""" +# [START analyticsadmin_properties_firebase_links_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import FirebaseLink + + +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" + + # TODO(developer): Replace this variable with a Firebase project id. + # This project will be linked to the GA4 property. + firebase_project_id = "YOUR-FIREBASE-PROJECT-ID" + + create_firebase_link(property_id, firebase_project_id) + + +def create_firebase_link(property_id, firebase_project_id): + """Creates a Firebase link for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + 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", + ), + ) + + print("Result:") + print(firebase_link) + + +# [END analyticsadmin_properties_firebase_links_create] + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_firebase_links_create_test.py b/samples/properties_firebase_links_create_test.py new file mode 100644 index 00000000..85f468a3 --- /dev/null +++ b/samples/properties_firebase_links_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_firebase_links_create + + +FAKE_PROPERTY_ID = "1" +FAKE_FIREBASE_PROJECT_ID = "1" + + +def test_properties_firebase_links_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_firebase_links_create.create_firebase_link( + FAKE_PROPERTY_ID, FAKE_FIREBASE_PROJECT_ID + ) diff --git a/samples/properties_firebase_links_delete.py b/samples/properties_firebase_links_delete.py new file mode 100644 index 00000000..9ef1edd3 --- /dev/null +++ b/samples/properties_firebase_links_delete.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 deletes the Firebase +link from the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/delete +for more information. +""" +# [START analyticsadmin_properties_firebase_links_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 Firebase link ID + # (e.g. "123456") before running the sample. + firebase_link_id = "YOUR-FIREBASE-LINK-ID" + + delete_firebase_link(property_id, firebase_link_id) + + +def delete_firebase_link(property_id, firebase_link_id): + """Deletes the Firebase link.""" + client = AnalyticsAdminServiceClient() + client.delete_firebase_link( + name=f"properties/{property_id}/firebaseLinks/{firebase_link_id}" + ) + print("Firebase link deleted") + + +# [END analyticsadmin_properties_firebase_links_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_firebase_links_delete_test.py b/samples/properties_firebase_links_delete_test.py new file mode 100644 index 00000000..e55860ed --- /dev/null +++ b/samples/properties_firebase_links_delete_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_firebase_links_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_FIREBASE_LINK_ID = "1" + + +def test_properties_firebase_links_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_firebase_links_delete.delete_firebase_link( + FAKE_PROPERTY_ID, FAKE_FIREBASE_LINK_ID + ) diff --git a/samples/properties_firebase_links_list.py b/samples/properties_firebase_links_list.py new file mode 100644 index 00000000..0d5c2f17 --- /dev/null +++ b/samples/properties_firebase_links_list.py @@ -0,0 +1,63 @@ +#!/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 prints Firebase links +under the specified parent Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.firebaseLinks/list +for more information. +""" +# [START analyticsadmin_properties_firebase_links_list] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import MaximumUserAccess + + +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_firebase_links(property_id) + + +def list_firebase_links(property_id): + """Lists Firebase links under the specified parent Google Analytics 4 + property.""" + client = AnalyticsAdminServiceClient() + results = client.list_firebase_links(parent=f"properties/{property_id}") + + print("Result:") + for firebase_link in results: + print_firebase_link(firebase_link) + print() + + +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}") + + +# [END analyticsadmin_properties_firebase_links_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_firebase_links_list_test.py b/samples/properties_firebase_links_list_test.py new file mode 100644 index 00000000..b3e1f0ac --- /dev/null +++ b/samples/properties_firebase_links_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_firebase_links_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_firebase_links_list(capsys): + properties_firebase_links_list.list_firebase_links(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_firebase_links_update.py b/samples/properties_firebase_links_update.py new file mode 100644 index 00000000..085f74d5 --- /dev/null +++ b/samples/properties_firebase_links_update.py @@ -0,0 +1,71 @@ +#!/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 new file mode 100644 index 00000000..dc07fb60 --- /dev/null +++ b/samples/properties_firebase_links_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_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 + ) diff --git a/samples/properties_get.py b/samples/properties_get.py new file mode 100644 index 00000000..e63ec3ac --- /dev/null +++ b/samples/properties_get.py @@ -0,0 +1,66 @@ +#!/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 print the Google +Analytics 4 property details. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/get +for more information. +""" +# [START analyticsadmin_properties_get] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import IndustryCategory + + +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" + get_property(property_id) + + +def get_property(property_id): + """Retrieves the Google Analytics 4 property details.""" + client = AnalyticsAdminServiceClient() + property_ = client.get_property(name=f"properties/{property_id}") + + print("Result:") + print_property(property_) + + +def print_property(property): + """Prints the Google Analytics 4 property details.""" + print(f"Resource name: {property.name}") + print(f"Parent: {property.parent}") + print(f"Display name: {property.display_name}") + print(f"Create time: {property.create_time}") + print(f"Update time: {property.update_time}") + # print(f"Delete time: {property.delete_time}") + # print(f"Expire time: {property.expire_time}") + + if property.industry_category: + print(f"Industry category: {IndustryCategory(property.industry_category).name}") + + print(f"Time zone: {property.time_zone}") + print(f"Currency code: {property.currency_code}") + + +# [END analyticsadmin_properties_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_get_test.py b/samples/properties_get_test.py new file mode 100644 index 00000000..90350552 --- /dev/null +++ b/samples/properties_get_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 os + +import properties_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_USER_LINK_ID = os.getenv("GA_USER_LINK_ID") + + +def test_properties_get(capsys): + properties_get.get_property(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_google_ads_links_create.py b/samples/properties_google_ads_links_create.py new file mode 100644 index 00000000..443b1515 --- /dev/null +++ b/samples/properties_google_ads_links_create.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 creates a Google Ads +link for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/create +for more information. +""" +# [START analyticsadmin_properties_google_ads_links_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import GoogleAdsLink + + +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" + + # TODO(developer): Replace this variable with a ten-digit Google Ads + # customer ID (digits only, e.g. "1234567890"). + # This Google Ads account will be linked to the GA4 property. + google_ads_customer_id = "YOUR-GOOGLE-ADS-CUSTOMER-ID" + + create_google_ads_link(property_id, google_ads_customer_id) + + +def create_google_ads_link(property_id, google_ads_customer_id): + """Creates a Google Ads link for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + google_ads_link = client.create_google_ads_link( + parent=f"properties/{property_id}", + google_ads_link=GoogleAdsLink(customer_id=f"{google_ads_customer_id}"), + ) + + print("Result:") + print(google_ads_link) + + +# [END analyticsadmin_properties_google_ads_links_create] + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_google_ads_links_create_test.py b/samples/properties_google_ads_links_create_test.py new file mode 100644 index 00000000..07ae5dde --- /dev/null +++ b/samples/properties_google_ads_links_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_google_ads_links_create + + +FAKE_PROPERTY_ID = "1" +FAKE_ADS_CUSTOMER_ID = "1234567890" + + +def test_properties_google_ads_links_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_google_ads_links_create.create_google_ads_link( + FAKE_PROPERTY_ID, FAKE_ADS_CUSTOMER_ID + ) diff --git a/samples/properties_google_ads_links_delete.py b/samples/properties_google_ads_links_delete.py new file mode 100644 index 00000000..74dd3ba0 --- /dev/null +++ b/samples/properties_google_ads_links_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 deletes the Google Ads link +from the specified Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/delete +for more information. +""" +# [START analyticsadmin_properties_google_ads_links_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 Google Analytics Ads + # link ID (e.g. "123456") before running the sample. + google_ads_link_id = "YOUR-GOOGLE-ADS-LINK-ID" + + delete_google_ads_link(property_id, google_ads_link_id) + + +def delete_google_ads_link(property_id, google_ads_link_id): + """Deletes the Google Ads link.""" + client = AnalyticsAdminServiceClient() + client.delete_google_ads_link( + name=f"properties/{property_id}/googleAdsLinks/{google_ads_link_id}" + ) + print("Google Ads link deleted") + + +# [END analyticsadmin_properties_google_ads_links_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_google_ads_links_delete_test.py b/samples/properties_google_ads_links_delete_test.py new file mode 100644 index 00000000..25c2768d --- /dev/null +++ b/samples/properties_google_ads_links_delete_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_google_ads_links_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_GOOGLE_ADS_LINK_ID = "1" + + +def test_properties_google_ads_links_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_google_ads_links_delete.delete_google_ads_link( + FAKE_PROPERTY_ID, FAKE_GOOGLE_ADS_LINK_ID + ) diff --git a/samples/properties_google_ads_links_list.py b/samples/properties_google_ads_links_list.py new file mode 100644 index 00000000..1aa7e065 --- /dev/null +++ b/samples/properties_google_ads_links_list.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 prints Google Ads links +under the specified parent Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/list +for more information. +""" +# [START analyticsadmin_properties_google_ads_links_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_google_ads_links(property_id) + + +def list_google_ads_links(property_id): + """Lists Google Ads links under the specified parent Google Analytics 4 + property.""" + client = AnalyticsAdminServiceClient() + results = client.list_google_ads_links(parent=f"properties/{property_id}") + + print("Result:") + for google_ads_link in results: + print_google_ads_link(google_ads_link) + print() + + +def print_google_ads_link(google_ads_link): + """Prints the Google Ads link details.""" + print(f"Resource name: {google_ads_link.name}") + 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"Create time: {google_ads_link.create_time}") + print(f"Update time: {google_ads_link.update_time}") + + +# [END analyticsadmin_properties_google_ads_links_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_google_ads_links_list_test.py b/samples/properties_google_ads_links_list_test.py new file mode 100644 index 00000000..1f850a4f --- /dev/null +++ b/samples/properties_google_ads_links_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_google_ads_links_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_firebase_links_list(capsys): + properties_google_ads_links_list.list_google_ads_links(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_google_ads_links_update.py b/samples/properties_google_ads_links_update.py new file mode 100644 index 00000000..8959cfc3 --- /dev/null +++ b/samples/properties_google_ads_links_update.py @@ -0,0 +1,70 @@ +#!/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 updates the Google Ads link. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.googleAdsLinks/update +for more information. +""" +# [START analyticsadmin_properties_google_ads_links_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import GoogleAdsLink +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 Google Analytics Ads + # link ID (e.g. "123456") before running the sample. + google_ads_link_id = "YOUR-GOOGLE-ADS-LINK-ID" + + update_google_ads_link(property_id, google_ads_link_id) + + +def update_google_ads_link(property_id, google_ads_link_id): + """Updates the Google Ads link.""" + client = AnalyticsAdminServiceClient() + # This call updates the adsPersonalizationEnabled setting of the + # Google Ads link as indicated by the value of the `update_mask` field. + # The Google Ads link to update is specified in the `name` field of the + # `Google AdsLink` instance. + google_ads_link = client.update_google_ads_link( + google_ads_link=GoogleAdsLink( + name=f"properties/{property_id}/googleAdsLinks/{google_ads_link_id}", + ads_personalization_enabled=False, + ), + update_mask=FieldMask(paths=["ads_personalization_enabled"]), + ) + + print("Result:") + print(google_ads_link) + + +# [END analyticsadmin_properties_google_ads_links_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_google_ads_links_update_test.py b/samples/properties_google_ads_links_update_test.py new file mode 100644 index 00000000..b734c379 --- /dev/null +++ b/samples/properties_google_ads_links_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_google_ads_links_update + + +FAKE_PROPERTY_ID = "1" +FAKE_GOOGLE_ADS_LINK_ID = "1" + + +def test_properties_google_ads_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_google_ads_links_update.update_google_ads_link( + FAKE_PROPERTY_ID, FAKE_GOOGLE_ADS_LINK_ID + ) diff --git a/samples/properties_list.py b/samples/properties_list.py new file mode 100644 index 00000000..19bfcef8 --- /dev/null +++ b/samples/properties_list.py @@ -0,0 +1,55 @@ +#!/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 prints Google Analytics 4 +properties under the specified parent account that are available to the +current user. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/list +for more information. +""" +# [START analyticsadmin_properties_list] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import ListPropertiesRequest + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + list_properties(account_id) + + +def list_properties(account_id): + """Lists Google Analytics 4 properties under the specified parent account + that are available to the current user.""" + client = AnalyticsAdminServiceClient() + results = client.list_properties( + ListPropertiesRequest(filter=f"parent:accounts/{account_id}", show_deleted=True) + ) + + print("Result:") + for property_ in results: + print(property_) + print() + + +# [END analyticsadmin_properties_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_list_test.py b/samples/properties_list_test.py new file mode 100644 index 00000000..97bf47dd --- /dev/null +++ b/samples/properties_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_list + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") + + +def test_properties_get(capsys): + properties_list.list_properties(TEST_ACCOUNT_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_update.py b/samples/properties_update.py new file mode 100644 index 00000000..8700440d --- /dev/null +++ b/samples/properties_update.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. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/update +for more information. +""" +# [START analyticsadmin_properties_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import Property +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" + update_property(property_id) + + +def update_property(property_id): + """Updates the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name, industry category and time zone of the + # property, as indicated by the value of the `update_mask` field. + # The property to update is specified in the `name` field of the `Property` + # instance. + property_ = client.update_property( + property=Property( + name=f"properties/{property_id}", + display_name="This is an updated test property", + industry_category="GAMES", + time_zone="America/New_York", + ), + update_mask=FieldMask(paths=["display_name", "time_zone", "industry_category"]), + ) + + print("Result:") + print(property_) + + +# [END analyticsadmin_properties_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_update_test.py b/samples/properties_update_test.py new file mode 100644 index 00000000..9d94bb09 --- /dev/null +++ b/samples/properties_update_test.py @@ -0,0 +1,27 @@ +# 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_update + + +FAKE_PROPERTY_ID = "1" + + +def test_properties_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_update.update_property(FAKE_PROPERTY_ID)