|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Analytics Admin API sample application which displays the change |
| 18 | +history for the Google Analytics account. |
| 19 | +
|
| 20 | +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/searchChangeHistoryEvents |
| 21 | +for more information. |
| 22 | +""" |
| 23 | +# [START analyticsadmin_properties_conversion_events_create] |
| 24 | +from datetime import datetime |
| 25 | +from datetime import timedelta |
| 26 | + |
| 27 | +from google.analytics.admin import AnalyticsAdminServiceClient |
| 28 | +from google.analytics.admin import SearchChangeHistoryEventsRequest |
| 29 | +from google.analytics.admin_v1alpha.types import ActionType |
| 30 | +from google.analytics.admin_v1alpha.types import ActorType |
| 31 | + |
| 32 | +from google.protobuf.timestamp_pb2 import Timestamp |
| 33 | + |
| 34 | + |
| 35 | +def run_sample(): |
| 36 | + """Runs the sample.""" |
| 37 | + # TODO(developer): Replace this variable with your Google Analytics |
| 38 | + # account ID (e.g. "123456") before running the sample. |
| 39 | + account_id = "YOUR-GA-ACCOUNT-ID" |
| 40 | + |
| 41 | + # TODO(developer): Replace this variable with your Google Analytics 4 |
| 42 | + # property ID (e.g. "123456") before running the sample. |
| 43 | + property_id = "YOUR-GA4-PROPERTY-ID" |
| 44 | + search_change_history_events(account_id, property_id) |
| 45 | + |
| 46 | + |
| 47 | +def search_change_history_events(account_id: str, property_id: str): |
| 48 | + """Lists the change history events for the Google Analytics 4 property |
| 49 | + within the specified date range.""" |
| 50 | + client = AnalyticsAdminServiceClient() |
| 51 | + # Create a timestamp object and subtract 7 days from the current date/time. |
| 52 | + earliest_change_time = Timestamp() |
| 53 | + earliest_change_time.FromDatetime(datetime.now() - timedelta(days=7)) |
| 54 | + |
| 55 | + results = client.search_change_history_events( |
| 56 | + SearchChangeHistoryEventsRequest( |
| 57 | + account=f"accounts/{account_id}", |
| 58 | + property=f"properties/{property_id}", |
| 59 | + action=["CREATED", "UPDATED"], |
| 60 | + earliest_change_time=earliest_change_time, |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + print("Result:") |
| 65 | + for event in results: |
| 66 | + print(f"Event ID: {event.id}") |
| 67 | + print(f"Change time: {event.change_time}") |
| 68 | + print(f"Actor type: {ActorType(event.actor_type).name}") |
| 69 | + print(f"User actor e-mail: {event.user_actor_email}") |
| 70 | + print(f"Changes filtered: {event.changes_filtered}") |
| 71 | + for change in event.changes: |
| 72 | + print(" Change details") |
| 73 | + print(f" Resource name: {change.resource}") |
| 74 | + print(f" Action: {ActionType(change.action).name}") |
| 75 | + print(" Resource before change: ") |
| 76 | + print_resource(change.resource_before_change) |
| 77 | + print(" Resource after change: ") |
| 78 | + print_resource(change.resource_after_change) |
| 79 | + print() |
| 80 | + |
| 81 | + |
| 82 | +def print_resource(resource): |
| 83 | + """Prints the change history resource.""" |
| 84 | + # Detect the type of the resource by checking value of a oneof field. |
| 85 | + if resource.property: |
| 86 | + print(" Property resource") |
| 87 | + elif resource.account: |
| 88 | + print(" Account resource") |
| 89 | + elif resource.web_data_stream: |
| 90 | + print(" WebDataStream resource") |
| 91 | + elif resource.android_app_data_stream: |
| 92 | + print(" AndroidAppDataStream resource") |
| 93 | + elif resource.ios_app_data_stream: |
| 94 | + print(" IosAppDataStream resource") |
| 95 | + elif resource.firebase_link: |
| 96 | + print(" FirebaseLink resource") |
| 97 | + elif resource.google_ads_link: |
| 98 | + print(" GoogleAdsLink resource") |
| 99 | + elif resource.google_signals_settings: |
| 100 | + print(" GoogleSignalsSettings resource") |
| 101 | + elif resource.display_video_360_advertiser_link: |
| 102 | + print(" DisplayVideo360AdvertiserLink resource") |
| 103 | + elif resource.display_video_360_advertiser_link_proposal: |
| 104 | + print(" DisplayVideo360AdvertiserLinkProposal resource") |
| 105 | + elif resource.conversion_event: |
| 106 | + print(" ConversionEvent resource") |
| 107 | + elif resource.measurement_protocol_secret: |
| 108 | + print(" MeasurementProtocolSecret resource") |
| 109 | + elif resource.custom_dimension: |
| 110 | + print(" CustomDimension resource") |
| 111 | + elif resource.custom_metric: |
| 112 | + print(" CustomMetric resource") |
| 113 | + elif resource.data_retention_settings: |
| 114 | + print(" DataRetentionSettings resource") |
| 115 | + else: |
| 116 | + print(" Resource not set") |
| 117 | + print(f" Resource value: {resource}") |
| 118 | + print() |
| 119 | + |
| 120 | + |
| 121 | +# [END analyticsadmin_properties_conversion_events_create] |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + run_sample() |
0 commit comments