Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8ef3f55
docs: add Admin API samples for account management methods
ikuleshov May 7, 2021
5448d5c
Merge branch 'master' into samples_accounts
ikuleshov May 7, 2021
0cddaf2
Merge branch 'master' into samples_accounts
ikuleshov May 12, 2021
d969ff9
Merge branch 'master' into samples_accounts
ikuleshov May 14, 2021
4368634
update copyright and remove redundant run_sample method
ikuleshov May 14, 2021
381a8d9
update noxfile template and set enforce_type_hints=False
ikuleshov May 14, 2021
d86e5b0
Merge branch 'samples_accounts' of github.com:ikuleshov/python-analyt…
ikuleshov May 14, 2021
85a38ec
add type annotations
ikuleshov May 14, 2021
42f158f
docs: add Admin API samples for account user link management methods
ikuleshov May 19, 2021
6b4932f
Merge remote-tracking branch 'upstream/master' into samples_accounts
ikuleshov May 19, 2021
c1919a7
fix the copyright string, avoid importing functions from other samples
ikuleshov May 25, 2021
c5c30e3
Merge remote-tracking branch 'upstream/master' into samples_accounts
ikuleshov May 25, 2021
f531f69
Merge branch 'master' into samples_accounts
ikuleshov May 27, 2021
381641c
Merge branch 'master' into samples_accounts
ikuleshov May 27, 2021
1c800ed
Merge remote-tracking branch 'upstream/master' into samples_accounts
ikuleshov Jun 1, 2021
259d530
docs: add samples for Google Analytics property management methods
ikuleshov Jun 1, 2021
ea4ad88
Merge branch 'samples_accounts' of github.com:ikuleshov/python-analyt…
ikuleshov Jun 1, 2021
8b7a53f
add samples for accounts.search_change_hostory_events method
ikuleshov Sep 29, 2021
b9bbc86
fix broken documentation urls
ikuleshov Sep 29, 2021
8eb90d9
add samples for accounts.search_change_history_events method
ikuleshov Sep 29, 2021
7ba90ee
Merge branch 'main' into samples_accounts
parthea Oct 5, 2021
e98053d
Merge remote-tracking branch 'upstream/main' into samples_accounts
ikuleshov Oct 6, 2021
fea3907
Merge branch 'samples_accounts' of github.com:ikuleshov/python-analyt…
ikuleshov Oct 6, 2021
868fdfe
fix imports
ikuleshov Oct 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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