From 610104473759db5308c993d186a7a06bca824dd2 Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Thu, 7 Oct 2021 06:41:47 -0400 Subject: [PATCH] docs: add samples for accounts.search_change_history_events() method (#137) * 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 * add samples for accounts.search_change_hostory_events method * fix broken documentation urls * fix imports Co-authored-by: Anthonios Partheniou --- .../accounts_search_change_history_events.py | 124 ++++++++++++++++++ ...ounts_search_change_history_events_test.py | 28 ++++ .../samples/accounts_update.py | 2 +- .../samples/accounts_user_links_update.py | 2 +- 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 packages/google-analytics-admin/samples/accounts_search_change_history_events.py create mode 100644 packages/google-analytics-admin/samples/accounts_search_change_history_events_test.py diff --git a/packages/google-analytics-admin/samples/accounts_search_change_history_events.py b/packages/google-analytics-admin/samples/accounts_search_change_history_events.py new file mode 100644 index 000000000000..38f0fec9f10f --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_search_change_history_events.py @@ -0,0 +1,124 @@ +#!/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 displays the change +history for the Google Analytics account. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/searchChangeHistoryEvents +for more information. +""" +# [START analyticsadmin_properties_conversion_events_create] +from datetime import datetime +from datetime import timedelta + +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin import SearchChangeHistoryEventsRequest +from google.analytics.admin_v1alpha.types import ActionType +from google.analytics.admin_v1alpha.types import ActorType + +from google.protobuf.timestamp_pb2 import Timestamp + + +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" + + # 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" + search_change_history_events(account_id, property_id) + + +def search_change_history_events(account_id: str, property_id: str): + """Lists the change history events for the Google Analytics 4 property + within the specified date range.""" + client = AnalyticsAdminServiceClient() + # Create a timestamp object and subtract 7 days from the current date/time. + earliest_change_time = Timestamp() + earliest_change_time.FromDatetime(datetime.now() - timedelta(days=7)) + + results = client.search_change_history_events( + SearchChangeHistoryEventsRequest( + account=f"accounts/{account_id}", + property=f"properties/{property_id}", + action=["CREATED", "UPDATED"], + earliest_change_time=earliest_change_time, + ) + ) + + print("Result:") + for event in results: + print(f"Event ID: {event.id}") + print(f"Change time: {event.change_time}") + print(f"Actor type: {ActorType(event.actor_type).name}") + print(f"User actor e-mail: {event.user_actor_email}") + print(f"Changes filtered: {event.changes_filtered}") + for change in event.changes: + print(" Change details") + print(f" Resource name: {change.resource}") + print(f" Action: {ActionType(change.action).name}") + print(" Resource before change: ") + print_resource(change.resource_before_change) + print(" Resource after change: ") + print_resource(change.resource_after_change) + print() + + +def print_resource(resource): + """Prints the change history resource.""" + # Detect the type of the resource by checking value of a oneof field. + if resource.property: + print(" Property resource") + elif resource.account: + print(" Account resource") + elif resource.web_data_stream: + print(" WebDataStream resource") + elif resource.android_app_data_stream: + print(" AndroidAppDataStream resource") + elif resource.ios_app_data_stream: + print(" IosAppDataStream resource") + elif resource.firebase_link: + print(" FirebaseLink resource") + elif resource.google_ads_link: + print(" GoogleAdsLink resource") + elif resource.google_signals_settings: + print(" GoogleSignalsSettings resource") + elif resource.display_video_360_advertiser_link: + print(" DisplayVideo360AdvertiserLink resource") + elif resource.display_video_360_advertiser_link_proposal: + print(" DisplayVideo360AdvertiserLinkProposal resource") + elif resource.conversion_event: + print(" ConversionEvent resource") + elif resource.measurement_protocol_secret: + print(" MeasurementProtocolSecret resource") + elif resource.custom_dimension: + print(" CustomDimension resource") + elif resource.custom_metric: + print(" CustomMetric resource") + elif resource.data_retention_settings: + print(" DataRetentionSettings resource") + else: + print(" Resource not set") + print(f" Resource value: {resource}") + print() + + +# [END analyticsadmin_properties_conversion_events_create] + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_search_change_history_events_test.py b/packages/google-analytics-admin/samples/accounts_search_change_history_events_test.py new file mode 100644 index 000000000000..9a3e63cf42a2 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_search_change_history_events_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 accounts_search_change_history_events + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_accounts_search_change_history_events(capsys): + accounts_search_change_history_events.search_change_history_events( + TEST_ACCOUNT_ID, TEST_PROPERTY_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/packages/google-analytics-admin/samples/accounts_update.py b/packages/google-analytics-admin/samples/accounts_update.py index e3ae56ed9aba..b87142c4b551 100644 --- a/packages/google-analytics-admin/samples/accounts_update.py +++ b/packages/google-analytics-admin/samples/accounts_update.py @@ -17,7 +17,7 @@ """Google Analytics Admin API sample application which updates the Google Analytics account. -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/update +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/patch for more information. """ # [START analyticsadmin_accounts_update] diff --git a/packages/google-analytics-admin/samples/accounts_user_links_update.py b/packages/google-analytics-admin/samples/accounts_user_links_update.py index 1a9424abc765..868403f083d5 100644 --- a/packages/google-analytics-admin/samples/accounts_user_links_update.py +++ b/packages/google-analytics-admin/samples/accounts_user_links_update.py @@ -17,7 +17,7 @@ """Google Analytics Admin API sample application which updates the account user link. -See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/update +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/patch for more information. """ # [START analyticsadmin_accounts_user_links_update]