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

Commit

Permalink
docs: add Admin API samples for account management methods (#58)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ikuleshov committed May 14, 2021
1 parent 7c340f0 commit 2ecc350
Show file tree
Hide file tree
Showing 18 changed files with 868 additions and 0 deletions.
50 changes: 50 additions & 0 deletions samples/account_summaries_list.py
@@ -0,0 +1,50 @@
#!/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 prints summaries of
all accounts accessible by the caller.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accountSummaries/list
for more information.
"""
# [START analyticsadmin_account_summaries_list]
from google.analytics.admin import AnalyticsAdminServiceClient


def list_account_summaries():
"""Returns summaries of all accounts accessible by the caller."""
client = AnalyticsAdminServiceClient()
results = client.list_account_summaries()

print("Result:")
for account_summary in results:
print("-- Account --")
print(f"Resource name: {account_summary.name}")
print(f"Account name: {account_summary.account}")
print(f"Display name: {account_summary.display_name}")
print()
for property_summary in account_summary.property_summaries:
print("-- Property --")
print(f"Property resource name: {property_summary.property}")
print(f"Property display name: {property_summary.display_name}")
print()


# [END analyticsadmin_account_summaries_list]


if __name__ == "__main__":
list_account_summaries()
21 changes: 21 additions & 0 deletions samples/account_summaries_list_test.py
@@ -0,0 +1,21 @@
# 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 account_summaries_list


def test_account_summaries_list(capsys):
account_summaries_list.list_account_summaries()
out, _ = capsys.readouterr()
assert "Result" in out
52 changes: 52 additions & 0 deletions samples/accounts_delete.py
@@ -0,0 +1,52 @@
#!/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 deletes a Google
Analytics account.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/delete
for more information.
"""
# [START analyticsadmin_accounts_delete]
from google.analytics.admin import AnalyticsAdminServiceClient


def run_sample():
"""Runs the sample."""

# !!! ATTENTION !!!
# Running this sample may change/delete your Google Analytics account
# configuration. Make sure to not use the Google Analytics account ID from
# your production environment below.

# TODO(developer): Replace this variable with your Google Analytics
# account ID (e.g. "123456") before running the sample.
account_id = "YOUR-GA-ACCOUNT-ID"
delete_account(account_id)


def delete_account(account_id: str):
"""Deletes the Google Analytics account."""
client = AnalyticsAdminServiceClient()
client.delete_account(name=f"accounts/{account_id}")
print("Account deleted")


# [END analyticsadmin_accounts_delete]


if __name__ == "__main__":
run_sample()
28 changes: 28 additions & 0 deletions samples/accounts_delete_test.py
@@ -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 pytest

import accounts_delete


FAKE_ACCOUNT_ID = "1"


def test_accounts_delete():
# This test ensures that the call is valid and reaches the server. No
# account is being deleted during the test as it is not trivial to
# provision a new account for testing.
with pytest.raises(Exception, match="403 The caller does not have permission"):
accounts_delete.delete_account(FAKE_ACCOUNT_ID)
57 changes: 57 additions & 0 deletions samples/accounts_get.py
@@ -0,0 +1,57 @@
#!/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 prints the Google
Analytics account data.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/get
for more information.
"""
# [START analyticsadmin_accounts_get]
from google.analytics.admin import AnalyticsAdminServiceClient


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"
get_account(account_id)


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 print_account(account: str):
"""Prints account data."""
print(f"Resource name: {account.name}")
print(f"Display name: {account.display_name}")
print(f"Region code: {account.region_code}")
print(f"Create time: {account.create_time}")
print(f"Update time: {account.update_time}")


# [END analyticsadmin_accounts_get]


if __name__ == "__main__":
run_sample()
60 changes: 60 additions & 0 deletions samples/accounts_get_data_sharing_settings.py
@@ -0,0 +1,60 @@
#!/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 prints the data sharing
settings on an account.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/getDataSharingSettings
for more information.
"""
# [START analyticsadmin_accounts_get_data_sharing_settings]
from google.analytics.admin import AnalyticsAdminServiceClient


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"
get_data_sharing_settings(account_id)


def get_data_sharing_settings(account_id: str):
"""Gets data sharing settings on an account."""
client = AnalyticsAdminServiceClient()
data_sharing_settings = client.get_data_sharing_settings(
name=f"accounts/{account_id}/dataSharingSettings"
)

print("Result:")
print(f"Resource name: {data_sharing_settings.name}")
print(
f"Sharing with Google support enabled: {data_sharing_settings.sharing_with_google_support_enabled}"
)
print(
f"Sharing with Google assigned sales enabled: {data_sharing_settings.sharing_with_google_assigned_sales_enabled}"
)
print(
f"Sharing with others enabled: {data_sharing_settings.sharing_with_others_enabled}"
)


# [END analyticsadmin_accounts_get_data_sharing_settings]


if __name__ == "__main__":
run_sample()
25 changes: 25 additions & 0 deletions samples/accounts_get_data_sharing_settings_test.py
@@ -0,0 +1,25 @@
# 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_get_data_sharing_settings

TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")


def test_accounts_get_data_sharing_settings(capsys):
accounts_get_data_sharing_settings.get_data_sharing_settings(TEST_ACCOUNT_ID)
out, _ = capsys.readouterr()
assert "Result" in out
25 changes: 25 additions & 0 deletions samples/accounts_get_test.py
@@ -0,0 +1,25 @@
# 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_get

TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")


def test_accounts_get(capsys):
accounts_get.get_account(TEST_ACCOUNT_ID)
out, _ = capsys.readouterr()
assert "Result" in out
43 changes: 43 additions & 0 deletions samples/accounts_list.py
@@ -0,0 +1,43 @@
#!/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 prints the Google
Analytics accounts available to the current user.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts/list
for more information.
"""
# [START analyticsadmin_accounts_list]
from google.analytics.admin import AnalyticsAdminServiceClient

from accounts_get import print_account


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)


# [END analyticsadmin_accounts_list]


if __name__ == "__main__":
list_accounts()
21 changes: 21 additions & 0 deletions samples/accounts_list_test.py
@@ -0,0 +1,21 @@
# 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 accounts_list


def test_accounts_list(capsys):
accounts_list.list_accounts()
out, _ = capsys.readouterr()
assert "Result" in out

0 comments on commit 2ecc350

Please sign in to comment.