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

Commit e264571

Browse files
ikuleshovparthea
andauthored
docs: add samples for Measurement Protocol Secrets management methods (#152)
* update test configuration * remove custom noxfile configs for now * remove MaximumUserAccess rom sample * add comment in noxfile_config.py * run blacken session * lint * add pytest * Update noxfile_config.py * Update noxfile_config.py * delete enhanced measurement settings samples as this functionality is no longer supported in v1alpha * fix the samples tests * do not use the `maximum_user_access` field and `update` operation in properties.firebase_links tests as this is no longer supported in v1alpha * use `creator_email_address` instead of `email_address` field in properties.google_ads_links_list() test as the field has been renamed in v1alpha * fix the samples test * add samples for Measurement Protocol Secrets management methods * add samples for Measurement Protocol Secrets management methods * add samples for Measurement Protocol Secrets management methods * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> * Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py Co-authored-by: Anthonios Partheniou <partheniou@google.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 126f271 commit e264571

31 files changed

Lines changed: 1435 additions & 0 deletions

File tree

samples/noxfile_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949",
1616
"GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289",
1717
"GA_TEST_WEB_DATA_STREAM_ID": "2828068992",
18+
"GA_TEST_ANDROID_APP_DATA_SECRET_ID": "2994941777",
19+
"GA_TEST_IOS_APP_DATA_SECRET_ID": "2994979281",
20+
"GA_TEST_WEB_DATA_SECRET_ID": "2994983412",
1821
"GA_TEST_CONVERSION_EVENT_ID": "2719963095"
1922
},
2023
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 creates a measurement
18+
protocol secret for the Android app data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/create
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha import MeasurementProtocolSecret
26+
27+
28+
def run_sample():
29+
"""Runs the sample."""
30+
31+
# !!! ATTENTION !!!
32+
# Running this sample may change/delete your Google Analytics account
33+
# configuration. Make sure to not use the Google Analytics property ID from
34+
# your production environment below.
35+
36+
# TODO(developer): Replace this variable with your Google Analytics 4
37+
# property ID (e.g. "123456") before running the sample.
38+
property_id = "YOUR-GA4-PROPERTY-ID"
39+
40+
# TODO(developer): Replace this variable with your Android app data stream ID
41+
# (e.g. "123456") before running the sample.
42+
stream_id = "YOUR-ANDROID-APP-DATA-STREAM-ID"
43+
44+
create_measurement_protocol_secret(property_id, stream_id)
45+
46+
47+
def create_measurement_protocol_secret(property_id, stream_id):
48+
"""Creates a measurement protocol secret for the Android app data stream."""
49+
client = AnalyticsAdminServiceClient()
50+
measurement_protocol_secret = client.create_measurement_protocol_secret(
51+
parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}",
52+
measurement_protocol_secret=MeasurementProtocolSecret(
53+
display_name="New secret"
54+
),
55+
)
56+
57+
print("Result:")
58+
print(f"Resource name: {measurement_protocol_secret.name}")
59+
print(f"Secret value: {measurement_protocol_secret.secret_value}")
60+
print(f"Display name: {measurement_protocol_secret.display_name}")
61+
62+
63+
# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create]
64+
65+
66+
if __name__ == "__main__":
67+
run_sample()
Lines changed: 30 additions & 0 deletions
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_measurement_protocol_secrets_create
18+
19+
20+
FAKE_PROPERTY_ID = "1"
21+
FAKE_STREAM_ID = "1"
22+
23+
24+
def test_properties_android_app_data_streams_measurement_protocol_secrets_create():
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_measurement_protocol_secrets_create.create_measurement_protocol_secret(
29+
FAKE_PROPERTY_ID, FAKE_STREAM_ID
30+
)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 measurement
18+
protocol secret for the Android app data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/delete
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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+
# TODO(developer): Replace this variable with your measurement protocol
44+
# secret ID (e.g. "123456") before running the sample.
45+
secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID"
46+
47+
delete_measurement_protocol_secret(property_id, stream_id, secret_id)
48+
49+
50+
def delete_measurement_protocol_secret(property_id, stream_id, secret_id):
51+
"""Deletes the measurement protocol secret for the Android app data
52+
stream."""
53+
client = AnalyticsAdminServiceClient()
54+
client.delete_measurement_protocol_secret(
55+
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}"
56+
)
57+
print("Measurement protocol secret deleted")
58+
59+
60+
# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete]
61+
62+
63+
if __name__ == "__main__":
64+
run_sample()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_measurement_protocol_secrets_delete
18+
19+
20+
FAKE_PROPERTY_ID = "1"
21+
FAKE_STREAM_ID = "1"
22+
FAKE_SECRET_ID = "1"
23+
24+
25+
def test_properties_android_app_data_streams_measurement_protocol_secrets_delete():
26+
# This test ensures that the call is valid and reaches the server, even
27+
# though the operation does not succeed due to permission error.
28+
with pytest.raises(Exception, match="403 The caller does not have permission"):
29+
properties_android_app_data_streams_measurement_protocol_secrets_delete.delete_measurement_protocol_secret(
30+
FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID
31+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 retrieves the details
18+
for the measurement protocol secret.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/get
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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+
# TODO(developer): Replace this variable with your measurement protocol
38+
# secret ID (e.g. "123456") before running the sample.
39+
secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID"
40+
41+
get_measurement_protocol_secret(property_id, stream_id, secret_id)
42+
43+
44+
def get_measurement_protocol_secret(property_id, stream_id, secret_id):
45+
"""Retrieves the details for the measurement protocol secret."""
46+
client = AnalyticsAdminServiceClient()
47+
measurement_protocol_secret = client.get_measurement_protocol_secret(
48+
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}"
49+
)
50+
51+
print("Result:")
52+
print(f"Resource name: {measurement_protocol_secret.name}")
53+
print(f"Secret value: {measurement_protocol_secret.secret_value}")
54+
print(f"Display name: {measurement_protocol_secret.display_name}")
55+
56+
57+
# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get]
58+
59+
60+
if __name__ == "__main__":
61+
run_sample()
Lines changed: 29 additions & 0 deletions
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_measurement_protocol_secrets_get
18+
19+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
20+
TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID")
21+
TEST_SECRET_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_SECRET_ID")
22+
23+
24+
def test_properties_android_app_data_streams_measurement_protocol_secrets_get(capsys):
25+
properties_android_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret(
26+
TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID
27+
)
28+
out, _ = capsys.readouterr()
29+
assert "Result" in out
Lines changed: 59 additions & 0 deletions
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 lists measurement
18+
protocol secrets for the Android app data stream.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/list
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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+
# 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+
list_measurement_protocol_secrets(property_id, stream_id)
37+
38+
39+
def list_measurement_protocol_secrets(property_id, stream_id):
40+
"""Lists measurement protocol secrets for the Android app data stream."""
41+
client = AnalyticsAdminServiceClient()
42+
results = client.list_measurement_protocol_secrets(
43+
parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}"
44+
)
45+
46+
print("Result:")
47+
for measurement_protocol_secret in results:
48+
print("Result:")
49+
print(f"Resource name: {measurement_protocol_secret.name}")
50+
print(f"Secret value: {measurement_protocol_secret.secret_value}")
51+
print(f"Display name: {measurement_protocol_secret.display_name}")
52+
print()
53+
54+
55+
# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list]
56+
57+
58+
if __name__ == "__main__":
59+
run_sample()
Lines changed: 29 additions & 0 deletions
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_measurement_protocol_secrets_list
18+
19+
20+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
21+
TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID")
22+
23+
24+
def test_properties_android_app_data_streams_measurement_protocol_secrets_list(capsys):
25+
properties_android_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets(
26+
TEST_PROPERTY_ID, TEST_STREAM_ID
27+
)
28+
out, _ = capsys.readouterr()
29+
assert "Result" in out

0 commit comments

Comments
 (0)