Skip to content

Commit 556adc0

Browse files
authored
docs: add Admin API samples for account management methods (#58)
* 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
1 parent e7ac11b commit 556adc0

18 files changed

+868
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 prints summaries of
18+
all accounts accessible by the caller.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accountSummaries/list
21+
for more information.
22+
"""
23+
# [START analyticsadmin_account_summaries_list]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def list_account_summaries():
28+
"""Returns summaries of all accounts accessible by the caller."""
29+
client = AnalyticsAdminServiceClient()
30+
results = client.list_account_summaries()
31+
32+
print("Result:")
33+
for account_summary in results:
34+
print("-- Account --")
35+
print(f"Resource name: {account_summary.name}")
36+
print(f"Account name: {account_summary.account}")
37+
print(f"Display name: {account_summary.display_name}")
38+
print()
39+
for property_summary in account_summary.property_summaries:
40+
print("-- Property --")
41+
print(f"Property resource name: {property_summary.property}")
42+
print(f"Property display name: {property_summary.display_name}")
43+
print()
44+
45+
46+
# [END analyticsadmin_account_summaries_list]
47+
48+
49+
if __name__ == "__main__":
50+
list_account_summaries()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 account_summaries_list
16+
17+
18+
def test_account_summaries_list(capsys):
19+
account_summaries_list.list_account_summaries()
20+
out, _ = capsys.readouterr()
21+
assert "Result" in out
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 deletes a Google
18+
Analytics account.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/delete
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_delete]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
30+
# !!! ATTENTION !!!
31+
# Running this sample may change/delete your Google Analytics account
32+
# configuration. Make sure to not use the Google Analytics account ID from
33+
# your production environment below.
34+
35+
# TODO(developer): Replace this variable with your Google Analytics
36+
# account ID (e.g. "123456") before running the sample.
37+
account_id = "YOUR-GA-ACCOUNT-ID"
38+
delete_account(account_id)
39+
40+
41+
def delete_account(account_id: str):
42+
"""Deletes the Google Analytics account."""
43+
client = AnalyticsAdminServiceClient()
44+
client.delete_account(name=f"accounts/{account_id}")
45+
print("Account deleted")
46+
47+
48+
# [END analyticsadmin_accounts_delete]
49+
50+
51+
if __name__ == "__main__":
52+
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 pytest
16+
17+
import accounts_delete
18+
19+
20+
FAKE_ACCOUNT_ID = "1"
21+
22+
23+
def test_accounts_delete():
24+
# This test ensures that the call is valid and reaches the server. No
25+
# account is being deleted during the test as it is not trivial to
26+
# provision a new account for testing.
27+
with pytest.raises(Exception, match="403 The caller does not have permission"):
28+
accounts_delete.delete_account(FAKE_ACCOUNT_ID)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 prints the Google
18+
Analytics account data.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/get
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_get]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
# TODO(developer): Replace this variable with your Google Analytics
30+
# account ID (e.g. "123456") before running the sample.
31+
account_id = "YOUR-GA-ACCOUNT-ID"
32+
get_account(account_id)
33+
34+
35+
def get_account(account_id: str):
36+
"""Retrieves the Google Analytics account data."""
37+
client = AnalyticsAdminServiceClient()
38+
account = client.get_account(name=f"accounts/{account_id}")
39+
40+
print("Result:")
41+
print_account(account)
42+
43+
44+
def print_account(account: str):
45+
"""Prints account data."""
46+
print(f"Resource name: {account.name}")
47+
print(f"Display name: {account.display_name}")
48+
print(f"Region code: {account.region_code}")
49+
print(f"Create time: {account.create_time}")
50+
print(f"Update time: {account.update_time}")
51+
52+
53+
# [END analyticsadmin_accounts_get]
54+
55+
56+
if __name__ == "__main__":
57+
run_sample()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 prints the data sharing
18+
settings on an account.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/getDataSharingSettings
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_get_data_sharing_settings]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
30+
# TODO(developer): Replace this variable with your Google Analytics
31+
# account ID (e.g. "123456") before running the sample.
32+
account_id = "YOUR-GA-ACCOUNT-ID"
33+
get_data_sharing_settings(account_id)
34+
35+
36+
def get_data_sharing_settings(account_id: str):
37+
"""Gets data sharing settings on an account."""
38+
client = AnalyticsAdminServiceClient()
39+
data_sharing_settings = client.get_data_sharing_settings(
40+
name=f"accounts/{account_id}/dataSharingSettings"
41+
)
42+
43+
print("Result:")
44+
print(f"Resource name: {data_sharing_settings.name}")
45+
print(
46+
f"Sharing with Google support enabled: {data_sharing_settings.sharing_with_google_support_enabled}"
47+
)
48+
print(
49+
f"Sharing with Google assigned sales enabled: {data_sharing_settings.sharing_with_google_assigned_sales_enabled}"
50+
)
51+
print(
52+
f"Sharing with others enabled: {data_sharing_settings.sharing_with_others_enabled}"
53+
)
54+
55+
56+
# [END analyticsadmin_accounts_get_data_sharing_settings]
57+
58+
59+
if __name__ == "__main__":
60+
run_sample()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_get_data_sharing_settings
18+
19+
TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")
20+
21+
22+
def test_accounts_get_data_sharing_settings(capsys):
23+
accounts_get_data_sharing_settings.get_data_sharing_settings(TEST_ACCOUNT_ID)
24+
out, _ = capsys.readouterr()
25+
assert "Result" in out
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_get
18+
19+
TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")
20+
21+
22+
def test_accounts_get(capsys):
23+
accounts_get.get_account(TEST_ACCOUNT_ID)
24+
out, _ = capsys.readouterr()
25+
assert "Result" in out
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 prints the Google
18+
Analytics accounts available to the current user.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/list
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_list]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
from accounts_get import print_account
27+
28+
29+
def list_accounts():
30+
"""Lists the Google Analytics accounts available to the current user."""
31+
client = AnalyticsAdminServiceClient()
32+
results = client.list_accounts()
33+
34+
print("Result:")
35+
for account in results:
36+
print_account(account)
37+
38+
39+
# [END analyticsadmin_accounts_list]
40+
41+
42+
if __name__ == "__main__":
43+
list_accounts()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 accounts_list
16+
17+
18+
def test_accounts_list(capsys):
19+
accounts_list.list_accounts()
20+
out, _ = capsys.readouterr()
21+
assert "Result" in out

0 commit comments

Comments
 (0)