Skip to content

Commit 065e5e5

Browse files
authored
docs: add Admin API samples for property stream management methods (#68)
* docs: add Admin API samples for property stream management methods * fix the copyright string, avoid importing functions from other samples
1 parent bf0b9b9 commit 065e5e5

File tree

32 files changed

+1461
-0
lines changed

32 files changed

+1461
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which deletes the Android app
18+
data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/delete
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_delete]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
30+
# !!! ATTENTION !!!
31+
# Running this sample may change/delete your Google Analytics account
32+
# configuration. Make sure to not use the Google Analytics property ID from
33+
# your production environment below.
34+
35+
# TODO(developer): Replace this variable with your Google Analytics 4
36+
# property ID (e.g. "123456") before running the sample.
37+
property_id = "YOUR-GA4-PROPERTY-ID"
38+
39+
# TODO(developer): Replace this variable with your Android app data stream ID
40+
# (e.g. "123456") before running the sample.
41+
stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID"
42+
43+
delete_android_app_data_stream(property_id, stream_id)
44+
45+
46+
def delete_android_app_data_stream(property_id, stream_id):
47+
"""Deletes the Android app data stream."""
48+
client = AnalyticsAdminServiceClient()
49+
client.delete_android_app_data_stream(
50+
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}"
51+
)
52+
print("Android app data stream deleted")
53+
54+
55+
# [END analyticsadmin_properties_android_app_data_streams_delete]
56+
57+
58+
if __name__ == "__main__":
59+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import properties_android_app_data_streams_delete
18+
19+
20+
FAKE_PROPERTY_ID = "1"
21+
FAKE_STREAM_ID = "1"
22+
23+
24+
def test_properties_android_app_data_streams_delete():
25+
# This test ensures that the call is valid and reaches the server, even
26+
# though the operation does not succeed due to permission error.
27+
with pytest.raises(Exception, match="403 The caller does not have permission"):
28+
properties_android_app_data_streams_delete.delete_android_app_data_stream(
29+
FAKE_PROPERTY_ID, FAKE_STREAM_ID
30+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which prints the details for
18+
an Android app data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/get
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_get]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
# TODO(developer): Replace this variable with your Google Analytics 4
30+
# property ID (e.g. "123456") before running the sample.
31+
property_id = "YOUR-GA4-PROPERTY-ID"
32+
33+
# TODO(developer): Replace this variable with your Android app data stream ID
34+
# (e.g. "123456") before running the sample.
35+
stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID"
36+
37+
get_android_app_data_stream(property_id, stream_id)
38+
39+
40+
def get_android_app_data_stream(property_id, stream_id):
41+
"""Retrieves the details for an Android app data stream."""
42+
client = AnalyticsAdminServiceClient()
43+
android_app_data_stream = client.get_android_app_data_stream(
44+
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}"
45+
)
46+
47+
print("Result:")
48+
print_android_app_data_stream(android_app_data_stream)
49+
50+
51+
def print_android_app_data_stream(android_app_data_stream):
52+
"""Prints the Android app data stream details."""
53+
print(f"Resource name: {android_app_data_stream.name}")
54+
print(f"Display name: {android_app_data_stream.display_name}")
55+
print(f"Firebase app ID: {android_app_data_stream.firebase_app_id}")
56+
print(f"Package name: {android_app_data_stream.package_name}")
57+
print(f"Create time: {android_app_data_stream.create_time}")
58+
print(f"Update time: {android_app_data_stream.update_time}")
59+
60+
61+
# [END analyticsadmin_properties_android_app_data_streams_get]
62+
63+
64+
if __name__ == "__main__":
65+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import properties_android_app_data_streams_get
18+
19+
20+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
21+
TEST_ANDROID_APP_DATA_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID")
22+
23+
24+
def test_properties_android_app_data_streams_get(capsys):
25+
properties_android_app_data_streams_get.get_android_app_data_stream(
26+
TEST_PROPERTY_ID, TEST_ANDROID_APP_DATA_STREAM_ID
27+
)
28+
out, _ = capsys.readouterr()
29+
assert "Result" in out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which prints Android app data
18+
streams for a Google Analytics 4 property.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/list
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_list]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
26+
27+
def run_sample():
28+
"""Runs the sample."""
29+
# TODO(developer): Replace this variable with your Google Analytics 4
30+
# property ID (e.g. "123456") before running the sample.
31+
property_id = "YOUR-GA4-PROPERTY-ID"
32+
33+
list_android_app_data_streams(property_id)
34+
35+
36+
def list_android_app_data_streams(property_id):
37+
"""Lists Android app data streams for a Google Analytics 4 property."""
38+
client = AnalyticsAdminServiceClient()
39+
results = client.list_android_app_data_streams(parent=f"properties/{property_id}")
40+
41+
print("Result:")
42+
for android_app_data_stream in results:
43+
print(android_app_data_stream)
44+
print()
45+
46+
47+
# [END analyticsadmin_properties_android_app_data_streams_list]
48+
49+
50+
if __name__ == "__main__":
51+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import properties_android_app_data_streams_list
18+
19+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
20+
21+
22+
def test_properties_android_app_data_streams_list(capsys):
23+
properties_android_app_data_streams_list.list_android_app_data_streams(
24+
TEST_PROPERTY_ID
25+
)
26+
out, _ = capsys.readouterr()
27+
assert "Result" in out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which updates the Android app
18+
data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams/update
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_update]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha.types import AndroidAppDataStream
26+
from google.protobuf.field_mask_pb2 import FieldMask
27+
28+
29+
def run_sample():
30+
"""Runs the sample."""
31+
32+
# !!! ATTENTION !!!
33+
# Running this sample may change/delete your Google Analytics account
34+
# configuration. Make sure to not use the Google Analytics property ID from
35+
# your production environment below.
36+
37+
# TODO(developer): Replace this variable with your Google Analytics 4
38+
# property ID (e.g. "123456") before running the sample.
39+
property_id = "YOUR-GA4-PROPERTY-ID"
40+
41+
# TODO(developer): Replace this variable with your Android app data stream ID
42+
# (e.g. "123456") before running the sample.
43+
stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID"
44+
45+
update_android_app_data_stream(property_id, stream_id)
46+
47+
48+
def update_android_app_data_stream(property_id, stream_id):
49+
"""Updates the Android app data stream."""
50+
client = AnalyticsAdminServiceClient()
51+
# This call updates the display name of the Android app data stream, as
52+
# indicated by the value of the `update_mask` field. The Android app data
53+
# stream to update is specified in the `name` field of the
54+
# `AndroidAppDataStream` instance.
55+
android_app_data_stream = client.update_android_app_data_stream(
56+
android_app_data_stream=AndroidAppDataStream(
57+
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}",
58+
display_name="This is an updated test Android app data stream",
59+
),
60+
update_mask=FieldMask(paths=["display_name"]),
61+
)
62+
63+
print("Result:")
64+
print(android_app_data_stream)
65+
66+
67+
# [END analyticsadmin_properties_android_app_data_streams_update]
68+
69+
70+
if __name__ == "__main__":
71+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import properties_android_app_data_streams_update
18+
19+
20+
FAKE_PROPERTY_ID = "1"
21+
FAKE_STREAM_ID = "1"
22+
23+
24+
def test_properties_android_app_data_streams_update():
25+
# This test ensures that the call is valid and reaches the server, even
26+
# though the operation does not succeed due to permission error.
27+
with pytest.raises(Exception, match="403 The caller does not have permission"):
28+
properties_android_app_data_streams_update.update_android_app_data_stream(
29+
FAKE_PROPERTY_ID, FAKE_STREAM_ID
30+
)

0 commit comments

Comments
 (0)