diff --git a/samples/properties_user_links_audit.py b/samples/properties_user_links_audit.py new file mode 100644 index 00000000..366eb7e4 --- /dev/null +++ b/samples/properties_user_links_audit.py @@ -0,0 +1,61 @@ +#!/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 user links audit +data on the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/audit +for more information. +""" +# [START analyticsadmin_properties_user_links_audit] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import AuditUserLinksRequest + + +def run_sample(): + """Runs the sample.""" + # 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" + audit_property_user_links(property_id) + + +def audit_property_user_links(property_id): + """Lists all user links on the Google Analytics 4 property, including + implicit ones that come from effective permissions granted by groups or + organization admin roles.""" + client = AnalyticsAdminServiceClient() + results = client.audit_user_links( + AuditUserLinksRequest(parent=f"properties/{property_id}") + ) + + print("Result:") + for user_link in results: + print(f"Resource name: {user_link.name}") + print(f"Email address: {user_link.email_address}") + for direct_role in user_link.direct_roles: + print(f"Direct role: {direct_role}") + + for effective_role in user_link.effective_roles: + print(f"Effective role: {effective_role}") + print() + + +# [END analyticsadmin_properties_user_links_audit] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_audit_test.py b/samples/properties_user_links_audit_test.py new file mode 100644 index 00000000..07fe7460 --- /dev/null +++ b/samples/properties_user_links_audit_test.py @@ -0,0 +1,25 @@ +# +# 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 properties_user_links_audit + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_user_links_audit(capsys): + properties_user_links_audit.audit_property_user_links(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_user_links_batch_create.py b/samples/properties_user_links_batch_create.py new file mode 100644 index 00000000..c3848d62 --- /dev/null +++ b/samples/properties_user_links_batch_create.py @@ -0,0 +1,80 @@ +#!/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 creates a user link for +the Google Analytics 4 property using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/batchCreate +for more information. +""" +# [START analyticsadmin_properties_user_links_batch_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchCreateUserLinksRequest +from google.analytics.admin_v1alpha.types import CreateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + +from properties_user_links_get import print_user_link + + +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 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with an email address of the user to + # link. This user will be given access to your account after running the + # sample. + email_address = "TEST-EMAIL-ADDRESS" + + batch_create_property_user_link(property_id, email_address) + + +def batch_create_property_user_link(property_id, email_address): + """Creates a user link for the Google Analytics 4 property using a batch + call.""" + client = AnalyticsAdminServiceClient() + response = client.batch_create_user_links( + BatchCreateUserLinksRequest( + parent=f"properties/{property_id}", + requests=[ + CreateUserLinkRequest( + user_link=UserLink( + email_address=email_address, + direct_roles=["predefinedRoles/read"], + ) + ) + ], + notify_new_users=True, + ) + ) + + print("Result:") + for user_link in response.user_links: + print_user_link(user_link) + print() + + +# [END analyticsadmin_properties_user_links_batch_create] + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_batch_create_test.py b/samples/properties_user_links_batch_create_test.py new file mode 100644 index 00000000..8aff7f09 --- /dev/null +++ b/samples/properties_user_links_batch_create_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_batch_create + + +FAKE_PROPERTY_ID = "1" +FAKE_EMAIL_ADDRESS = "test@google.com" + + +def test_properties_user_links_batch_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_batch_create.batch_create_property_user_link( + FAKE_PROPERTY_ID, FAKE_EMAIL_ADDRESS + ) diff --git a/samples/properties_user_links_batch_delete.py b/samples/properties_user_links_batch_delete.py new file mode 100644 index 00000000..b678b0d9 --- /dev/null +++ b/samples/properties_user_links_batch_delete.py @@ -0,0 +1,68 @@ +#!/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 delete the user link for +the Google Analytics 4 property using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/batchDelete +for more information. +""" +# [START analyticsadmin_properties_user_links_batch_delete] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchDeleteUserLinksRequest +from google.analytics.admin_v1alpha.types import DeleteUserLinkRequest + + +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 property ID from + # your production environment below. + + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_delete_property_user_link(property_id, property_user_link_id) + + +def batch_delete_property_user_link(property_id, property_user_link_id): + """Deletes the GA4 property user link using a batch call.""" + client = AnalyticsAdminServiceClient() + client.batch_delete_user_links( + BatchDeleteUserLinksRequest( + parent=f"properties/{property_id}", + requests=[ + DeleteUserLinkRequest( + name=f"properties/{property_id}/userLinks/{property_user_link_id}" + ) + ], + ) + ) + print("User link deleted") + + +# [END analyticsadmin_properties_user_links_batch_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_batch_delete_test.py b/samples/properties_user_links_batch_delete_test.py new file mode 100644 index 00000000..c1f41f12 --- /dev/null +++ b/samples/properties_user_links_batch_delete_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_batch_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_USER_LINK_ID = "1" + + +def test_properties_user_links_batch_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_batch_delete.batch_delete_property_user_link( + FAKE_PROPERTY_ID, FAKE_USER_LINK_ID + ) diff --git a/samples/properties_user_links_batch_get.py b/samples/properties_user_links_batch_get.py new file mode 100644 index 00000000..5a91efe5 --- /dev/null +++ b/samples/properties_user_links_batch_get.py @@ -0,0 +1,64 @@ +#!/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 details for the +Google Analytics 4 property user link using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/batchGet +for more information. +""" +# [START analyticsadmin_properties_user_links_batch_get] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchGetUserLinksRequest + +from properties_user_links_get import print_user_link + + +def run_sample(): + """Runs the sample.""" + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_get_property_user_link(property_id, property_user_link_id) + + +def batch_get_property_user_link(property_id, property_user_link_id): + """Retrieves details for the Google Analytics 4 property user link using a + batch call.""" + client = AnalyticsAdminServiceClient() + response = client.batch_get_user_links( + BatchGetUserLinksRequest( + parent=f"properties/{property_id}", + names=[f"properties/{property_id}/userLinks/{property_user_link_id}"], + ) + ) + + print("Result:") + for user_link in response.user_links: + print_user_link(user_link) + print() + + +# [END analyticsadmin_properties_user_links_batch_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_batch_get_test.py b/samples/properties_user_links_batch_get_test.py new file mode 100644 index 00000000..795267ab --- /dev/null +++ b/samples/properties_user_links_batch_get_test.py @@ -0,0 +1,27 @@ +# +# 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 properties_user_links_batch_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_USER_LINK_ID = os.getenv("GA_USER_LINK_ID") + + +def test_properties_user_links_batch_get(capsys): + properties_user_links_batch_get.batch_get_property_user_link( + TEST_PROPERTY_ID, TEST_USER_LINK_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_user_links_batch_update.py b/samples/properties_user_links_batch_update.py new file mode 100644 index 00000000..4ab27119 --- /dev/null +++ b/samples/properties_user_links_batch_update.py @@ -0,0 +1,81 @@ +#!/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 updates the Google +Analytics 4 property user link using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/batchUpdate +for more information. +""" +# [START analyticsadmin_properties_user_links_batch_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchUpdateUserLinksRequest +from google.analytics.admin_v1alpha.types import UpdateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + +from properties_user_links_get import print_user_link + + +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 property ID from + # your production environment below. + + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_update_property_user_link(property_id, property_user_link_id) + + +def batch_update_property_user_link(property_id, property_user_link_id): + """Updates the Google Analytics 4 property user link using a batch call.""" + client = AnalyticsAdminServiceClient() + # This call updates the email address and direct roles of the user link. + # The user link to update is specified in the `name` field of the `UserLink` + # instance. + response = client.batch_update_user_links( + BatchUpdateUserLinksRequest( + parent=f"properties/{property_id}", + requests=[ + UpdateUserLinkRequest( + user_link=UserLink( + name=f"properties/{property_id}/userLinks/{property_user_link_id}", + direct_roles=["predefinedRoles/collaborate"], + ), + ) + ], + ) + ) + + print("Result:") + for user_link in response.user_links: + print_user_link(user_link) + print() + + +# [END analyticsadmin_properties_user_links_batch_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_batch_update_test.py b/samples/properties_user_links_batch_update_test.py new file mode 100644 index 00000000..b79e88c3 --- /dev/null +++ b/samples/properties_user_links_batch_update_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_batch_update + + +FAKE_PROPERTY_ID = "1" +FAKE_USER_LINK_ID = "1" + + +def test_properties_user_links_batch_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_batch_update.batch_update_property_user_link( + FAKE_PROPERTY_ID, FAKE_USER_LINK_ID + ) diff --git a/samples/properties_user_links_create.py b/samples/properties_user_links_create.py new file mode 100644 index 00000000..ccea9864 --- /dev/null +++ b/samples/properties_user_links_create.py @@ -0,0 +1,71 @@ +#!/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 creates a user link for +the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/create +for more information. +""" +# [START analyticsadmin_properties_user_links_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import CreateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + +from properties_user_links_get import print_user_link + + +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 4 + # property ID (e.g. "123456") before running the sample. + property_id = "YOUR-GA4-PROPERTY-ID" + + # TODO(developer): Replace this variable with an email address of the user to + # link. This user will be given access to your account after running the + # sample. + email_address = "TEST-EMAIL-ADDRESS" + + create_property_user_link(property_id, email_address) + + +def create_property_user_link(property_id, email_address): + """Creates a user link for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + user_link = client.create_user_link( + CreateUserLinkRequest( + parent=f"properties/{property_id}", + user_link=UserLink( + email_address=email_address, direct_roles=["predefinedRoles/read"] + ), + notify_new_user=True, + ) + ) + + print("Result:") + print_user_link(user_link) + + +# [END analyticsadmin_properties_user_links_create] + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_create_test.py b/samples/properties_user_links_create_test.py new file mode 100644 index 00000000..30add93c --- /dev/null +++ b/samples/properties_user_links_create_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_create + + +FAKE_PROPERTY_ID = "1" +FAKE_EMAIL_ADDRESS = "test@google.com" + + +def test_properties_user_links_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_create.create_property_user_link( + FAKE_PROPERTY_ID, FAKE_EMAIL_ADDRESS + ) diff --git a/samples/properties_user_links_delete.py b/samples/properties_user_links_delete.py new file mode 100644 index 00000000..3aeaa6bd --- /dev/null +++ b/samples/properties_user_links_delete.py @@ -0,0 +1,62 @@ +#!/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 the user link +from the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/delete +for more information. +""" +# [START analyticsadmin_properties_user_links_delete] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import DeleteUserLinkRequest + + +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 property ID from + # your production environment below. + + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + delete_property_user_link(property_id, property_user_link_id) + + +def delete_property_user_link(property_id, property_user_link_id): + """Deletes the user link from the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_user_link( + DeleteUserLinkRequest( + name=f"properties/{property_id}/userLinks/{property_user_link_id}" + ) + ) + print("User link deleted") + + +# [END analyticsadmin_properties_user_links_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_delete_test.py b/samples/properties_user_links_delete_test.py new file mode 100644 index 00000000..a12394dd --- /dev/null +++ b/samples/properties_user_links_delete_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_USER_LINK_ID = "1" + + +def test_properties_user_links_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_delete.delete_property_user_link( + FAKE_PROPERTY_ID, FAKE_USER_LINK_ID + ) diff --git a/samples/properties_user_links_get.py b/samples/properties_user_links_get.py new file mode 100644 index 00000000..ba9bacb0 --- /dev/null +++ b/samples/properties_user_links_get.py @@ -0,0 +1,63 @@ +#!/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 4 property user link details. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/get +for more information. +""" +# [START analyticsadmin_properties_user_links_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + get_property_user_link(property_id, property_user_link_id) + + +def get_property_user_link(property_id, property_user_link_id): + """Retrieves the Google Analytics 4 property user link details.""" + client = AnalyticsAdminServiceClient() + user_link = client.get_user_link( + name=f"properties/{property_id}/userLinks/{property_user_link_id}" + ) + + print("Result:") + print_user_link(user_link) + + +def print_user_link(user_link): + """Prints the user link details.""" + print(f"Resource name: {user_link.name}") + print(f"Email address: {user_link.email_address}") + for direct_role in user_link.direct_roles: + print(f"Direct role: {direct_role}") + + +# [END analyticsadmin_properties_user_links_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_get_test.py b/samples/properties_user_links_get_test.py new file mode 100644 index 00000000..68fb4116 --- /dev/null +++ b/samples/properties_user_links_get_test.py @@ -0,0 +1,27 @@ +# +# 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 properties_user_links_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_USER_LINK_ID = os.getenv("GA_USER_LINK_ID") + + +def test_properties_user_links_get(capsys): + properties_user_links_get.get_property_user_link( + TEST_PROPERTY_ID, TEST_USER_LINK_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_user_links_list.py b/samples/properties_user_links_list.py new file mode 100644 index 00000000..0500f5af --- /dev/null +++ b/samples/properties_user_links_list.py @@ -0,0 +1,53 @@ +#!/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 user links under +the specified parent Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/list +for more information. +""" +# [START analyticsadmin_properties_user_links_list] +from google.analytics.admin import AnalyticsAdminServiceClient + +from properties_user_links_get import print_user_link + + +def run_sample(): + """Runs the sample.""" + # 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" + list_property_user_links(property_id) + + +def list_property_user_links(property_id): + """Lists user links under the specified parent Google Analytics 4 + property.""" + client = AnalyticsAdminServiceClient() + results = client.list_user_links(parent=f"properties/{property_id}") + + print("Result:") + for user_link in results: + print_user_link(user_link) + print() + + +# [END analyticsadmin_properties_user_links_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_list_test.py b/samples/properties_user_links_list_test.py new file mode 100644 index 00000000..39f1d839 --- /dev/null +++ b/samples/properties_user_links_list_test.py @@ -0,0 +1,24 @@ +# +# 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 properties_user_links_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_user_links_list(capsys): + properties_user_links_list.list_property_user_links(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_user_links_update.py b/samples/properties_user_links_update.py new file mode 100644 index 00000000..d8d59d09 --- /dev/null +++ b/samples/properties_user_links_update.py @@ -0,0 +1,70 @@ +#!/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 updates the Google +Analytics 4 property user link. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.userLinks/update +for more information. +""" +# [START analyticsadmin_properties_user_links_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import UserLink + +from properties_user_links_get import print_user_link + + +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 property ID from + # your production environment below. + + # 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" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + property_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + update_property_user_link(property_id, property_user_link_id) + + +def update_property_user_link(property_id, property_user_link_id): + """Updates the Google Analytics 4 property user link.""" + client = AnalyticsAdminServiceClient() + # This call updates the email address and direct roles of the user link. + # The user link to update is specified in the `name` field of the `UserLink` + # instance. + user_link = client.update_user_link( + user_link=UserLink( + name=f"properties/{property_id}/userLinks/{property_user_link_id}", + direct_roles=["predefinedRoles/collaborate"], + ), + ) + + print("Result:") + print_user_link(user_link) + + +# [END analyticsadmin_properties_user_links_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_user_links_update_test.py b/samples/properties_user_links_update_test.py new file mode 100644 index 00000000..a9aab1b2 --- /dev/null +++ b/samples/properties_user_links_update_test.py @@ -0,0 +1,29 @@ +# +# 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 properties_user_links_update + + +FAKE_PROPERTY_ID = "1" +FAKE_USER_LINK_ID = "1" + + +def test_properties_user_links_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + properties_user_links_update.update_property_user_link( + FAKE_PROPERTY_ID, FAKE_USER_LINK_ID + )