diff --git a/samples/properties_android_app_data_streams_delete.py b/samples/properties_android_app_data_streams_delete.py new file mode 100644 index 00000000..dfd69397 --- /dev/null +++ b/samples/properties_android_app_data_streams_delete.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. 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 Android app +data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/delete +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_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 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 Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + delete_android_app_data_stream(property_id, stream_id) + + +def delete_android_app_data_stream(property_id, stream_id): + """Deletes the Android app data stream.""" + client = AnalyticsAdminServiceClient() + client.delete_android_app_data_stream( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}" + ) + print("Android app data stream deleted") + + +# [END analyticsadmin_properties_android_app_data_streams_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_delete_test.py b/samples/properties_android_app_data_streams_delete_test.py new file mode 100644 index 00000000..bd633880 --- /dev/null +++ b/samples/properties_android_app_data_streams_delete_test.py @@ -0,0 +1,30 @@ +# 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 properties_android_app_data_streams_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_android_app_data_streams_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_android_app_data_streams_delete.delete_android_app_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_android_app_data_streams_get.py b/samples/properties_android_app_data_streams_get.py new file mode 100644 index 00000000..68fcb8b3 --- /dev/null +++ b/samples/properties_android_app_data_streams_get.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. 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 details for +an Android app data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/get +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_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 Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + get_android_app_data_stream(property_id, stream_id) + + +def get_android_app_data_stream(property_id, stream_id): + """Retrieves the details for an Android app data stream.""" + client = AnalyticsAdminServiceClient() + android_app_data_stream = client.get_android_app_data_stream( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}" + ) + + print("Result:") + print_android_app_data_stream(android_app_data_stream) + + +def print_android_app_data_stream(android_app_data_stream): + """Prints the Android app data stream details.""" + print(f"Resource name: {android_app_data_stream.name}") + print(f"Display name: {android_app_data_stream.display_name}") + print(f"Firebase app ID: {android_app_data_stream.firebase_app_id}") + print(f"Package name: {android_app_data_stream.package_name}") + print(f"Create time: {android_app_data_stream.create_time}") + print(f"Update time: {android_app_data_stream.update_time}") + + +# [END analyticsadmin_properties_android_app_data_streams_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_get_test.py b/samples/properties_android_app_data_streams_get_test.py new file mode 100644 index 00000000..33d2cea9 --- /dev/null +++ b/samples/properties_android_app_data_streams_get_test.py @@ -0,0 +1,29 @@ +# 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 properties_android_app_data_streams_get + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_ANDROID_APP_DATA_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID") + + +def test_properties_android_app_data_streams_get(capsys): + properties_android_app_data_streams_get.get_android_app_data_stream( + TEST_PROPERTY_ID, TEST_ANDROID_APP_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_list.py b/samples/properties_android_app_data_streams_list.py new file mode 100644 index 00000000..97b67b0b --- /dev/null +++ b/samples/properties_android_app_data_streams_list.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. 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 Android app data +streams for a Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/list +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_list] +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" + + list_android_app_data_streams(property_id) + + +def list_android_app_data_streams(property_id): + """Lists Android app data streams for a Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + results = client.list_android_app_data_streams(parent=f"properties/{property_id}") + + print("Result:") + for android_app_data_stream in results: + print(android_app_data_stream) + print() + + +# [END analyticsadmin_properties_android_app_data_streams_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_list_test.py b/samples/properties_android_app_data_streams_list_test.py new file mode 100644 index 00000000..90b47f72 --- /dev/null +++ b/samples/properties_android_app_data_streams_list_test.py @@ -0,0 +1,27 @@ +# 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 properties_android_app_data_streams_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_android_app_data_streams_list(capsys): + properties_android_app_data_streams_list.list_android_app_data_streams( + TEST_PROPERTY_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_android_app_data_streams_update.py b/samples/properties_android_app_data_streams_update.py new file mode 100644 index 00000000..72fc0a7e --- /dev/null +++ b/samples/properties_android_app_data_streams_update.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +# Copyright 2021 Google Inc. 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 Android app +data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/update +for more information. +""" +# [START analyticsadmin_properties_android_app_data_streams_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import AndroidAppDataStream +from google.protobuf.field_mask_pb2 import FieldMask + + +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 Android app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID" + + update_android_app_data_stream(property_id, stream_id) + + +def update_android_app_data_stream(property_id, stream_id): + """Updates the Android app data stream.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the Android app data stream, as + # indicated by the value of the `update_mask` field. The Android app data + # stream to update is specified in the `name` field of the + # `AndroidAppDataStream` instance. + android_app_data_stream = client.update_android_app_data_stream( + android_app_data_stream=AndroidAppDataStream( + name=f"properties/{property_id}/androidAppDataStreams/{stream_id}", + display_name="This is an updated test Android app data stream", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(android_app_data_stream) + + +# [END analyticsadmin_properties_android_app_data_streams_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_android_app_data_streams_update_test.py b/samples/properties_android_app_data_streams_update_test.py new file mode 100644 index 00000000..de310b46 --- /dev/null +++ b/samples/properties_android_app_data_streams_update_test.py @@ -0,0 +1,30 @@ +# 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 properties_android_app_data_streams_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_android_app_data_streams_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_android_app_data_streams_update.update_android_app_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_ios_app_data_streams_delete.py b/samples/properties_ios_app_data_streams_delete.py new file mode 100644 index 00000000..b02a280f --- /dev/null +++ b/samples/properties_ios_app_data_streams_delete.py @@ -0,0 +1,59 @@ +#!/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 iOS app data +stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams/delete +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_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 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 iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + delete_ios_app_data_stream(property_id, stream_id) + + +def delete_ios_app_data_stream(property_id, stream_id): + """Deletes the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + client.delete_ios_app_data_stream( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}" + ) + print("iOS app data stream deleted") + + +# [END analyticsadmin_properties_ios_app_data_streams_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_delete_test.py b/samples/properties_ios_app_data_streams_delete_test.py new file mode 100644 index 00000000..6c207280 --- /dev/null +++ b/samples/properties_ios_app_data_streams_delete_test.py @@ -0,0 +1,30 @@ +# 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 properties_ios_app_data_streams_delete + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_ios_app_data_streams_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_ios_app_data_streams_delete.delete_ios_app_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_ios_app_data_streams_get.py b/samples/properties_ios_app_data_streams_get.py new file mode 100644 index 00000000..02b775da --- /dev/null +++ b/samples/properties_ios_app_data_streams_get.py @@ -0,0 +1,65 @@ +#!/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 iOS app data +stream details. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams/get +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_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 iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + get_ios_app_data_stream(property_id, stream_id) + + +def get_ios_app_data_stream(property_id, stream_id): + """Retrieves the details for the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + ios_app_data_stream = client.get_ios_app_data_stream( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}" + ) + + print("Result:") + print_ios_app_data_stream(ios_app_data_stream) + + +def print_ios_app_data_stream(ios_app_data_stream): + """Prints the iOS app data stream details.""" + print(f"Resource name: {ios_app_data_stream.name}") + print(f"Display name: {ios_app_data_stream.display_name}") + print(f"Firebase app ID: {ios_app_data_stream.firebase_app_id}") + print(f"Bundle ID: {ios_app_data_stream.bundleId}") + print(f"Create time: {ios_app_data_stream.create_time}") + print(f"Update time: {ios_app_data_stream.update_time}") + + +# [END analyticsadmin_properties_ios_app_data_streams_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_get_test.py b/samples/properties_ios_app_data_streams_get_test.py new file mode 100644 index 00000000..6eb81092 --- /dev/null +++ b/samples/properties_ios_app_data_streams_get_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 os + +import properties_ios_app_data_streams_get + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_IOS_APP_DATA_STREAM_ID = os.getenv("GA_TEST_IOS_APP_DATA_STREAM_ID") + + +def test_properties_ios_app_data_streams_get(capsys): + properties_ios_app_data_streams_get.get_ios_app_data_stream( + TEST_PROPERTY_ID, TEST_IOS_APP_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_list.py b/samples/properties_ios_app_data_streams_list.py new file mode 100644 index 00000000..4260802f --- /dev/null +++ b/samples/properties_ios_app_data_streams_list.py @@ -0,0 +1,51 @@ +#!/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 iOS app data +streams for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams/list +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_list] +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" + + list_ios_app_data_streams(property_id) + + +def list_ios_app_data_streams(property_id): + """Lists iOS app data streams for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + results = client.list_ios_app_data_streams(parent=f"properties/{property_id}") + + print("Result:") + for ios_app_data_stream in results: + print(ios_app_data_stream) + print() + + +# [END analyticsadmin_properties_ios_app_data_streams_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_list_test.py b/samples/properties_ios_app_data_streams_list_test.py new file mode 100644 index 00000000..449520cc --- /dev/null +++ b/samples/properties_ios_app_data_streams_list_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 properties_ios_app_data_streams_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_ios_app_data_streams_list(capsys): + properties_ios_app_data_streams_list.list_ios_app_data_streams(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_ios_app_data_streams_update.py b/samples/properties_ios_app_data_streams_update.py new file mode 100644 index 00000000..46126512 --- /dev/null +++ b/samples/properties_ios_app_data_streams_update.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 updates the iOS app data +stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.iosAppDataStreams/update +for more information. +""" +# [START analyticsadmin_properties_ios_app_data_streams_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import IosAppDataStream +from google.protobuf.field_mask_pb2 import FieldMask + + +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 iOS app data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-IOS-APP-DATA-STREAM-ID" + + update_ios_app_data_stream(property_id, stream_id) + + +def update_ios_app_data_stream(property_id, stream_id): + """Updates the iOS app data stream.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the iOS app data stream, as + # indicated by the value of the `update_mask` field. The iOS app data + # stream to update is specified in the `name` field of the + # `IosAppDataStream` instance. + ios_app_data_stream = client.update_ios_app_data_stream( + ios_app_data_stream=IosAppDataStream( + name=f"properties/{property_id}/iosAppDataStreams/{stream_id}", + display_name="This is an updated test iOS app data stream", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(ios_app_data_stream) + + +# [END analyticsadmin_properties_ios_app_data_streams_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_ios_app_data_streams_update_test.py b/samples/properties_ios_app_data_streams_update_test.py new file mode 100644 index 00000000..8c0843ef --- /dev/null +++ b/samples/properties_ios_app_data_streams_update_test.py @@ -0,0 +1,30 @@ +# 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 properties_ios_app_data_streams_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_ios_app_data_streams_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_ios_app_data_streams_update.update_ios_app_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_web_data_streams_create.py b/samples/properties_web_data_streams_create.py new file mode 100644 index 00000000..17f80856 --- /dev/null +++ b/samples/properties_web_data_streams_create.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 creates a web data stream +for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/create +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import WebDataStream + + +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" + create_web_data_stream(property_id) + + +def create_web_data_stream(property_id): + """Creates a web data stream for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + web_data_stream = client.create_web_data_stream( + parent=f"properties/{property_id}", + web_data_stream=WebDataStream( + default_uri="https://www.google.com", display_name="Test web data stream" + ), + ) + + print("Result:") + print(web_data_stream) + + +# [END analyticsadmin_properties_web_data_streams_create] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_create_test.py b/samples/properties_web_data_streams_create_test.py new file mode 100644 index 00000000..e79281cd --- /dev/null +++ b/samples/properties_web_data_streams_create_test.py @@ -0,0 +1,26 @@ +# 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 properties_web_data_streams_create + +FAKE_PROPERTY_ID = "1" + + +def test_properties_web_data_streams_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_web_data_streams_create.create_web_data_stream(FAKE_PROPERTY_ID) diff --git a/samples/properties_web_data_streams_delete.py b/samples/properties_web_data_streams_delete.py new file mode 100644 index 00000000..5c3da2f6 --- /dev/null +++ b/samples/properties_web_data_streams_delete.py @@ -0,0 +1,59 @@ +#!/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 web data +stream from the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/delete +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_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 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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + delete_web_data_stream(property_id, stream_id) + + +def delete_web_data_stream(property_id, stream_id): + """Deletes the web data stream from the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + client.delete_web_data_stream( + name=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + print("Web data stream deleted") + + +# [END analyticsadmin_properties_web_data_streams_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_delete_test.py b/samples/properties_web_data_streams_delete_test.py new file mode 100644 index 00000000..6e4276b1 --- /dev/null +++ b/samples/properties_web_data_streams_delete_test.py @@ -0,0 +1,29 @@ +# 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 properties_web_data_streams_delete + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_web_data_streams_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_web_data_streams_delete.delete_web_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_web_data_streams_get.py b/samples/properties_web_data_streams_get.py new file mode 100644 index 00000000..27b9779e --- /dev/null +++ b/samples/properties_web_data_streams_get.py @@ -0,0 +1,66 @@ +#!/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 details for +the web data stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/get +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + get_web_data_stream(property_id, stream_id) + + +def get_web_data_stream(property_id, stream_id): + """Retrieves the details for the web data stream.""" + client = AnalyticsAdminServiceClient() + web_data_stream = client.get_web_data_stream( + name=f"properties/{property_id}/webDataStreams/{stream_id}" + ) + + print("Result:") + print_web_data_stream(web_data_stream) + + +def print_web_data_stream(web_data_stream): + """Prints the web data stream details.""" + print(f"Resource name: {web_data_stream.name}") + print(f"Display name: {web_data_stream.display_name}") + print(f"Default URI: {web_data_stream.default_uri}") + print(f"Measurement ID: {web_data_stream.measurement_id}") + print(f"Firebase App ID: {web_data_stream.firebase_app_id}") + print(f"Create time: {web_data_stream.create_time}") + print(f"Update time: {web_data_stream.update_time}") + + +# [END analyticsadmin_properties_web_data_streams_get] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_get_enhanced_measurement_settings.py b/samples/properties_web_data_streams_get_enhanced_measurement_settings.py new file mode 100644 index 00000000..65d7225d --- /dev/null +++ b/samples/properties_web_data_streams_get_enhanced_measurement_settings.py @@ -0,0 +1,79 @@ +#!/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 enhanced +measurement settings for the web stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/getEnhancedMeasurementSettings +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_get_enhanced_measurement_settings] +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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + get_enhanced_measurement_settings(property_id, stream_id) + + +def get_enhanced_measurement_settings(property_id, stream_id): + """Retrieves the enhanced measurement settings for the web stream.""" + client = AnalyticsAdminServiceClient() + enhanced_measurement_settings = client.get_enhanced_measurement_settings( + name=f"properties/{property_id}/webDataStreams/{stream_id}/enhancedMeasurementSettings" + ) + + print("Result:") + print_enhanced_measurement_settings(enhanced_measurement_settings) + + +def print_enhanced_measurement_settings(enhanced_measurement_settings): + """Prints the enhanced measurement settings for a web stream.""" + print(f"Resource name: {enhanced_measurement_settings.name}") + print(f"Stream enabled: {enhanced_measurement_settings.streamEnabled}") + print(f"Page views enabled: {enhanced_measurement_settings.pageViewsEnabled}") + print(f"Scrolls enabled: {enhanced_measurement_settings.scrollsEnabled}") + print( + f"Outbound clicks enabled: {enhanced_measurement_settings.outboundClicksEnabled}" + ) + print(f"Site search enabled: {enhanced_measurement_settings.siteSearchEnabled}") + print( + f"Video engagement enabled: {enhanced_measurement_settings.videoEngagementEnabled}" + ) + print( + f"File downloads enabled: {enhanced_measurement_settings.fileDownloadsEnabled}" + ) + print(f"Page loads enabled: {enhanced_measurement_settings.pageLoadsEnabled}") + print(f"Page changes enabled: {enhanced_measurement_settings.pageChangesEnabled}") + print( + f"Search query parameter: {enhanced_measurement_settings.searchQueryParameter}" + ) + print(f"Uri query parameter: {enhanced_measurement_settings.uriQueryParameter}") + + +# [END analyticsadmin_properties_web_data_streams_get_enhanced_measurement_settings] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py b/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py new file mode 100644 index 00000000..34d7ccd4 --- /dev/null +++ b/samples/properties_web_data_streams_get_enhanced_measurement_settings_test.py @@ -0,0 +1,29 @@ +# 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 properties_web_data_streams_get_enhanced_measurement_settings + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_WEB_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") + + +def test_properties_web_data_streams_get_enhanced_measurement_settings(capsys): + properties_web_data_streams_get_enhanced_measurement_settings.get_enhanced_measurement_settings( + TEST_PROPERTY_ID, TEST_WEB_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_get_global_site_tag.py b/samples/properties_web_data_streams_get_global_site_tag.py new file mode 100644 index 00000000..7534b2cc --- /dev/null +++ b/samples/properties_web_data_streams_get_global_site_tag.py @@ -0,0 +1,55 @@ +#!/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 Site Tag data +for the specified web stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/getGlobalSiteTag +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_get_global_site_tag] +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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + get_global_site_tag(property_id, stream_id) + + +def get_global_site_tag(property_id, stream_id): + """Retrieves the Site Tag for the specified web stream.""" + client = AnalyticsAdminServiceClient() + global_site_tag = client.get_global_site_tag( + name=f"properties/{property_id}/webDataStreams/{stream_id}/globalSiteTag" + ) + + print("Result:") + print(global_site_tag.snippet) + + +# [END analyticsadmin_properties_web_data_streams_get_global_site_tag] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_get_global_site_tag_test.py b/samples/properties_web_data_streams_get_global_site_tag_test.py new file mode 100644 index 00000000..b38a4134 --- /dev/null +++ b/samples/properties_web_data_streams_get_global_site_tag_test.py @@ -0,0 +1,29 @@ +# 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 properties_web_data_streams_get_global_site_tag + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_WEB_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") + + +def test_properties_web_data_streams_get_global_site_tag(capsys): + properties_web_data_streams_get_global_site_tag.get_global_site_tag( + TEST_PROPERTY_ID, TEST_WEB_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_get_test.py b/samples/properties_web_data_streams_get_test.py new file mode 100644 index 00000000..c03c3bee --- /dev/null +++ b/samples/properties_web_data_streams_get_test.py @@ -0,0 +1,29 @@ +# 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 properties_web_data_streams_get + + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") +TEST_WEB_DATA_STREAM_ID = os.getenv("GA_TEST_WEB_DATA_STREAM_ID") + + +def test_properties_android_app_data_streams_get(capsys): + properties_web_data_streams_get.get_web_data_stream( + TEST_PROPERTY_ID, TEST_WEB_DATA_STREAM_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_list.py b/samples/properties_web_data_streams_list.py new file mode 100644 index 00000000..5ef657a4 --- /dev/null +++ b/samples/properties_web_data_streams_list.py @@ -0,0 +1,51 @@ +#!/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 web data streams +for the Google Analytics 4 property. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/list +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_list] +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" + + list_web_data_streams(property_id) + + +def list_web_data_streams(property_id): + """Lists web data streams for the Google Analytics 4 property.""" + client = AnalyticsAdminServiceClient() + results = client.list_web_data_streams(parent=f"properties/{property_id}") + + print("Result:") + for web_data_stream in results: + print(web_data_stream) + print() + + +# [END analyticsadmin_properties_web_data_streams_list] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_list_test.py b/samples/properties_web_data_streams_list_test.py new file mode 100644 index 00000000..e757c685 --- /dev/null +++ b/samples/properties_web_data_streams_list_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 properties_web_data_streams_list + +TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID") + + +def test_properties_web_data_streams_list(capsys): + properties_web_data_streams_list.list_web_data_streams(TEST_PROPERTY_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/samples/properties_web_data_streams_update.py b/samples/properties_web_data_streams_update.py new file mode 100644 index 00000000..a26635d1 --- /dev/null +++ b/samples/properties_web_data_streams_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 web data +stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/update +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import WebDataStream +from google.protobuf.field_mask_pb2 import FieldMask + + +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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + update_web_data_stream(property_id, stream_id) + + +def update_web_data_stream(property_id, stream_id): + """Updates the web data stream.""" + client = AnalyticsAdminServiceClient() + # This call updates the display name of the web data stream, as indicated by + # the value of the `update_mask` field. The web data stream to update is + # specified in the `name` field of the `WebDataStream` instance. + web_data_stream = client.update_web_data_stream( + web_data_stream=WebDataStream( + name=f"properties/{property_id}/webDataStreams/{stream_id}", + display_name="This is an updated test web data stream", + ), + update_mask=FieldMask(paths=["display_name"]), + ) + + print("Result:") + print(web_data_stream) + + +# [END analyticsadmin_properties_web_data_streams_update] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_update_enhanced_measurement_settings.py b/samples/properties_web_data_streams_update_enhanced_measurement_settings.py new file mode 100644 index 00000000..f02e1fb0 --- /dev/null +++ b/samples/properties_web_data_streams_update_enhanced_measurement_settings.py @@ -0,0 +1,73 @@ +#!/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 enhanced +measurement settings for the web stream. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.webDataStreams/updateEnhancedMeasurementSettings +for more information. +""" +# [START analyticsadmin_properties_web_data_streams_update_enhanced_measurement_settings] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import EnhancedMeasurementSettings +from google.protobuf.field_mask_pb2 import FieldMask + + + +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 web data stream ID + # (e.g. "123456") before running the sample. + stream_id = "YOUR-WEB-DATA-STREAM-ID" + + update_enhanced_measurement_settings(property_id, stream_id) + + +def update_enhanced_measurement_settings(property_id, stream_id): + """Updates the enhanced measurement settings for the web stream.""" + client = AnalyticsAdminServiceClient() + # This call updates the `streamEnabled`, `fileDownloadsEnabled` measurement + # settings of the web data stream, as indicated by the value of the + # `update_mask` field. The web data stream to update is specified in the + # `name` field of the `EnhancedMeasurementSettings` instance. + enhanced_measurement_settings = client.update_enhanced_measurement_settings( + enhanced_measurement_settings=EnhancedMeasurementSettings( + name=f"properties/{property_id}/webDataStreams/{stream_id}/enhancedMeasurementSettings", + stream_enabled=False, + file_downloads_enabled=False, + ), + update_mask=FieldMask(paths=["stream_enabled", "file_downloads_enabled"]), + ) + + print("Result:") + print(enhanced_measurement_settings) + + +# [END analyticsadmin_properties_web_data_streams_update_enhanced_measurement_settings] + + +if __name__ == "__main__": + run_sample() diff --git a/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py b/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py new file mode 100644 index 00000000..d692a0eb --- /dev/null +++ b/samples/properties_web_data_streams_update_enhanced_measurement_settings_test.py @@ -0,0 +1,30 @@ +# 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 properties_web_data_streams_update_enhanced_measurement_settings + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_web_data_streams_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_web_data_streams_update_enhanced_measurement_settings.update_enhanced_measurement_settings( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + ) diff --git a/samples/properties_web_data_streams_update_test.py b/samples/properties_web_data_streams_update_test.py new file mode 100644 index 00000000..03f06a71 --- /dev/null +++ b/samples/properties_web_data_streams_update_test.py @@ -0,0 +1,30 @@ +# 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 properties_web_data_streams_update + + +FAKE_PROPERTY_ID = "1" +FAKE_STREAM_ID = "1" + + +def test_properties_web_data_streams_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_web_data_streams_update.update_web_data_stream( + FAKE_PROPERTY_ID, FAKE_STREAM_ID + )