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")
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 get_account(account_id: str): """Retrieves the Google Analytics account data.""" client = AnalyticsAdminServiceClient() account = client.get_account(name=f"accounts/{account_id}") print("Result:") print_account(account)
def delete_account_user_link(account_id, account_user_link_id): """Deletes the user link for the account.""" client = AnalyticsAdminServiceClient() client.delete_user_link( DeleteUserLinkRequest( name=f"accounts/{account_id}/userLinks/{account_user_link_id}")) print("User link deleted")
def delete_web_data_stream(property_id, stream_id): """Deletes the web data stream from the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() client.delete_web_data_stream( name=f"properties/{property_id}/webDataStreams/{stream_id}" ) print("Web data stream deleted")
def delete_property_user_link(property_id, property_user_link_id): """Deletes the user link from the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() client.delete_user_link( DeleteUserLinkRequest( name=f"properties/{property_id}/userLinks/{property_user_link_id}") ) print("User link deleted")
def get_ios_app_data_stream(property_id, stream_id): """Retrieves the details for the iOS app data stream.""" client = AnalyticsAdminServiceClient() ios_app_data_stream = client.get_ios_app_data_stream( name=f"properties/{property_id}/iosAppDataStreams/{stream_id}") print("Result:") print_ios_app_data_stream(ios_app_data_stream)
def get_web_data_stream(property_id, stream_id): """Retrieves the details for the web data stream.""" client = AnalyticsAdminServiceClient() web_data_stream = client.get_web_data_stream( name=f"properties/{property_id}/webDataStreams/{stream_id}") print("Result:") print_web_data_stream(web_data_stream)
def get_android_app_data_stream(property_id, stream_id): """Retrieves the details for an Android app data stream.""" client = AnalyticsAdminServiceClient() android_app_data_stream = client.get_android_app_data_stream( name=f"properties/{property_id}/androidAppDataStreams/{stream_id}") print("Result:") print_android_app_data_stream(android_app_data_stream)
def list_accounts(): """Lists the Google Analytics accounts available to the current user.""" client = AnalyticsAdminServiceClient() results = client.list_accounts() print("Result:") for account in results: print_account(account)
def get_account_user_link(account_id, account_user_link_id): """Retrieves the account user link details.""" client = AnalyticsAdminServiceClient() user_link = client.get_user_link( name=f"accounts/{account_id}/userLinks/{account_user_link_id}" ) print("Result:") print_user_link(user_link)
def list_web_data_streams(property_id): """Lists web data streams for the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() results = client.list_web_data_streams(parent=f"properties/{property_id}") print("Result:") for web_data_stream in results: print(web_data_stream) print()
def list_account_user_links(account_id): """Lists user links under the specified parent account.""" client = AnalyticsAdminServiceClient() results = client.list_user_links(parent=f"accounts/{account_id}") print("Result:") for user_link in results: print(user_link) print()
def get_property_user_link(property_id, property_user_link_id): """Retrieves the Google Analytics 4 property user link details.""" client = AnalyticsAdminServiceClient() user_link = client.get_user_link( name=f"properties/{property_id}/userLinks/{property_user_link_id}" ) print("Result:") print_user_link(user_link)
def get_global_site_tag(property_id, stream_id): """Retrieves the Site Tag for the specified web stream.""" client = AnalyticsAdminServiceClient() global_site_tag = client.get_global_site_tag( name=f"properties/{property_id}/webDataStreams/{stream_id}/globalSiteTag" ) print("Result:") print(global_site_tag.snippet)
def get_enhanced_measurement_settings(property_id, stream_id): """Retrieves the enhanced measurement settings for the web stream.""" client = AnalyticsAdminServiceClient() enhanced_measurement_settings = client.get_enhanced_measurement_settings( name= f"properties/{property_id}/webDataStreams/{stream_id}/enhancedMeasurementSettings" ) print("Result:") print_enhanced_measurement_settings(enhanced_measurement_settings)
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}"), ) print("Result:") print(firebase_link)
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 list_android_app_data_streams(property_id): """Lists Android app data streams for a Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() results = client.list_android_app_data_streams( parent=f"properties/{property_id}") print("Result:") for android_app_data_stream in results: print(android_app_data_stream) print()
def list_property_user_links(property_id): """Lists user links under the specified parent Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() results = client.list_user_links(parent=f"properties/{property_id}") print("Result:") for user_link in results: print(user_link) print()
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)
def list_accounts(): """Lists the available Google Analytics accounts.""" from google.analytics.admin import AnalyticsAdminServiceClient # Using a default constructor instructs the client to use the credentials # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. client = AnalyticsAdminServiceClient() # Displays the configuration information for all Google Analytics accounts # available to the authenticated user. for account in client.list_accounts(): print(account)
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()
def create_web_data_stream(property_id): """Creates a web data stream for the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() web_data_stream = client.create_web_data_stream( parent=f"properties/{property_id}", web_data_stream=WebDataStream( default_uri="https://www.google.com", display_name="Test web data stream" ), ) print("Result:") print(web_data_stream)
def create_account_user_link(account_id, email_address): """Creates a user link for the account.""" client = AnalyticsAdminServiceClient() user_link = client.create_user_link( CreateUserLinkRequest( parent=f"accounts/{account_id}", user_link=UserLink(email_address=email_address, direct_roles=["predefinedRoles/read"]), notify_new_user=True, )) print("Result:") print(user_link)
def create_property_user_link(property_id, email_address): """Creates a user link for the Google Analytics 4 property.""" client = AnalyticsAdminServiceClient() user_link = client.create_user_link( CreateUserLinkRequest( parent=f"properties/{property_id}", user_link=UserLink(email_address=email_address, direct_roles=["predefinedRoles/read"]), notify_new_user=True, )) print("Result:") print(user_link)
def update_account_user_link(account_id, account_user_link_id): """Updates the account user link.""" client = AnalyticsAdminServiceClient() # This call updates the email address and direct roles of the user link. # The user link to update is specified in the `name` field of the `UserLink` # instance. user_link = client.update_user_link(user_link=UserLink( name=f"accounts/{account_id}/userLinks/{account_user_link_id}", direct_roles=["predefinedRoles/collaborate"], ), ) print("Result:") print(user_link)
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_)
def batch_delete_property_user_link(property_id, property_user_link_id): """Deletes the GA4 property user link using a batch call.""" client = AnalyticsAdminServiceClient() client.batch_delete_user_links( BatchDeleteUserLinksRequest( parent=f"properties/{property_id}", requests=[ DeleteUserLinkRequest( name= f"properties/{property_id}/userLinks/{property_user_link_id}" ) ], )) print("User link deleted")
def update_web_data_stream(property_id, stream_id): """Updates the web data stream.""" client = AnalyticsAdminServiceClient() # This call updates the display name of the web data stream, as indicated by # the value of the `update_mask` field. The web data stream to update is # specified in the `name` field of the `WebDataStream` instance. web_data_stream = client.update_web_data_stream( web_data_stream=WebDataStream( name=f"properties/{property_id}/webDataStreams/{stream_id}", display_name="This is an updated test web data stream", ), update_mask=FieldMask(paths=["display_name"]), ) print("Result:") print(web_data_stream)