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

Commit c299b37

Browse files
ikuleshovparthea
andauthored
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 <partheniou@google.com>
1 parent dffd289 commit c299b37

4 files changed

Lines changed: 154 additions & 2 deletions
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import accounts_search_change_history_events
18+
19+
TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")
20+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
21+
22+
23+
def test_accounts_search_change_history_events(capsys):
24+
accounts_search_change_history_events.search_change_history_events(
25+
TEST_ACCOUNT_ID, TEST_PROPERTY_ID
26+
)
27+
out, _ = capsys.readouterr()
28+
assert "Result" in out

samples/accounts_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""Google Analytics Admin API sample application which updates the Google
1818
Analytics account.
1919
20-
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/update
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/patch
2121
for more information.
2222
"""
2323
# [START analyticsadmin_accounts_update]

samples/accounts_user_links_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""Google Analytics Admin API sample application which updates the account
1818
user link.
1919
20-
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/update
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/patch
2121
for more information.
2222
"""
2323
# [START analyticsadmin_accounts_user_links_update]

0 commit comments

Comments
 (0)