Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
docs: add samples for accounts.search_change_history_events() method (#…
Browse files Browse the repository at this point in the history
…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 <partheniou@google.com>
  • Loading branch information
ikuleshov and parthea authored Oct 7, 2021
1 parent dffd289 commit c299b37
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 2 deletions.
124 changes: 124 additions & 0 deletions samples/accounts_search_change_history_events.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 28 additions & 0 deletions samples/accounts_search_change_history_events_test.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion samples/accounts_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion samples/accounts_user_links_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit c299b37

Please sign in to comment.