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

docs: add samples for Measurement Protocol Secrets management methods #152

Merged
merged 31 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f787a04
update test configuration
ikuleshov Sep 2, 2021
79093d1
remove custom noxfile configs for now
parthea Sep 2, 2021
8f1f957
remove MaximumUserAccess rom sample
parthea Sep 2, 2021
b674e0d
add comment in noxfile_config.py
parthea Sep 2, 2021
de94a95
run blacken session
parthea Sep 2, 2021
761d8aa
lint
parthea Sep 2, 2021
ee91693
add pytest
parthea Sep 2, 2021
6cfcf4f
Merge branch 'main' into master
parthea Oct 4, 2021
f12b2b1
Merge branch 'main' into master
ikuleshov Oct 4, 2021
7a7c6d2
Update noxfile_config.py
ikuleshov Oct 5, 2021
9366fdc
Update noxfile_config.py
ikuleshov Oct 5, 2021
a3e4f17
Merge remote-tracking branch 'upstream/main'
ikuleshov Oct 5, 2021
7de1885
Merge remote-tracking branch 'origin/master'
ikuleshov Oct 5, 2021
a88d15e
delete enhanced measurement settings samples as this functionality is…
ikuleshov Oct 5, 2021
e76ba96
fix the samples tests
ikuleshov Oct 5, 2021
3836b72
do not use the `maximum_user_access` field and `update` operation in …
ikuleshov Oct 5, 2021
d235358
use `creator_email_address` instead of `email_address` field in prope…
ikuleshov Oct 5, 2021
d5b923a
fix the samples test
ikuleshov Oct 5, 2021
f3d6fa7
Merge remote-tracking branch 'upstream/main'
ikuleshov Oct 13, 2021
f7c0cd6
add samples for Measurement Protocol Secrets management methods
ikuleshov Oct 13, 2021
cd55ddf
add samples for Measurement Protocol Secrets management methods
ikuleshov Oct 13, 2021
2de4f79
Merge remote-tracking branch 'origin/mp_secrets' into mp_secrets
ikuleshov Oct 13, 2021
bf19bbb
add samples for Measurement Protocol Secrets management methods
ikuleshov Oct 13, 2021
4937c3f
Merge branch 'main' into mp_secrets
ikuleshov Oct 19, 2021
5b10eef
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
267033a
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
e30b7ca
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
9d674e0
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
505fdc5
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
53ff7fe
Update samples/properties_web_data_streams_measurement_protocol_secre…
ikuleshov Oct 19, 2021
af12a56
Merge branch 'main' into mp_secrets
ikuleshov Oct 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samples/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"GA_TEST_ANDROID_APP_DATA_STREAM_ID": "2828100949",
"GA_TEST_IOS_APP_DATA_STREAM_ID": "2828089289",
"GA_TEST_WEB_DATA_STREAM_ID": "2828068992",
"GA_TEST_ANDROID_APP_DATA_SECRET_ID": "2994941777",
"GA_TEST_IOS_APP_DATA_SECRET_ID": "2994979281",
"GA_TEST_WEB_DATA_SECRET_ID": "2994983412"
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/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 creates a measurement
protocol secret for the Android app data stream.

See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/create
for more information.
"""
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create]
from google.analytics.admin import AnalyticsAdminServiceClient
from google.analytics.admin_v1alpha import MeasurementProtocolSecret


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"

create_measurement_protocol_secret(property_id, stream_id)


def create_measurement_protocol_secret(property_id, stream_id):
"""Creates a measurement protocol secret for the Android app data stream."""
client = AnalyticsAdminServiceClient()
measurement_protocol_secret = client.create_measurement_protocol_secret(
parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}",
measurement_protocol_secret=MeasurementProtocolSecret(
display_name="New secret"
),
)

print("Result:")
print(f"Resource name: {measurement_protocol_secret.name}")
print(f"Secret value: {measurement_protocol_secret.secret_value}")
print(f"Display name: {measurement_protocol_secret.display_name}")


# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_create]


if __name__ == "__main__":
run_sample()
Original file line number Diff line number Diff line change
@@ -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_measurement_protocol_secrets_create


FAKE_PROPERTY_ID = "1"
FAKE_STREAM_ID = "1"


def test_properties_android_app_data_streams_measurement_protocol_secrets_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_android_app_data_streams_measurement_protocol_secrets_create.create_measurement_protocol_secret(
FAKE_PROPERTY_ID, FAKE_STREAM_ID
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/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 measurement
protocol secret for the Android app data stream.

See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/delete
for more information.
"""
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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"

# TODO(developer): Replace this variable with your measurement protocol
# secret ID (e.g. "123456") before running the sample.
secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID"

delete_measurement_protocol_secret(property_id, stream_id, secret_id)


def delete_measurement_protocol_secret(property_id, stream_id, secret_id):
"""Deletes the measurement protocol secret for the Android app data
stream."""
client = AnalyticsAdminServiceClient()
client.delete_measurement_protocol_secret(
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}"
)
print("Measurement protocol secret deleted")


# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_delete]


if __name__ == "__main__":
run_sample()
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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_measurement_protocol_secrets_delete


FAKE_PROPERTY_ID = "1"
FAKE_STREAM_ID = "1"
FAKE_SECRET_ID = "1"


def test_properties_android_app_data_streams_measurement_protocol_secrets_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_measurement_protocol_secrets_delete.delete_measurement_protocol_secret(
FAKE_PROPERTY_ID, FAKE_STREAM_ID, FAKE_SECRET_ID
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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 retrieves the details
for the measurement protocol secret.

See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/get
for more information.
"""
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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"

# TODO(developer): Replace this variable with your measurement protocol
# secret ID (e.g. "123456") before running the sample.
secret_id = "YOUR-MEASUREMENT-PROTOCOL-SECRET-ID"

get_measurement_protocol_secret(property_id, stream_id, secret_id)


def get_measurement_protocol_secret(property_id, stream_id, secret_id):
"""Retrieves the details for the measurement protocol secret."""
client = AnalyticsAdminServiceClient()
measurement_protocol_secret = client.get_measurement_protocol_secret(
name=f"properties/{property_id}/androidAppDataStreams/{stream_id}/measurementProtocolSecrets/{secret_id}"
)

print("Result:")
print(f"Resource name: {measurement_protocol_secret.name}")
print(f"Secret value: {measurement_protocol_secret.secret_value}")
print(f"Display name: {measurement_protocol_secret.display_name}")


# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_get]


if __name__ == "__main__":
run_sample()
Original file line number Diff line number Diff line change
@@ -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_measurement_protocol_secrets_get

TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID")
TEST_SECRET_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_SECRET_ID")


def test_properties_android_app_data_streams_measurement_protocol_secrets_get(capsys):
properties_android_app_data_streams_measurement_protocol_secrets_get.get_measurement_protocol_secret(
TEST_PROPERTY_ID, TEST_STREAM_ID, TEST_SECRET_ID
)
out, _ = capsys.readouterr()
assert "Result" in out
Original file line number Diff line number Diff line change
@@ -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 lists measurement
protocol secrets for the Android app data stream.

See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.androidAppDataStreams.measurementProtocolSecrets/list
for more information.
"""
# [START analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_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"

# 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"
list_measurement_protocol_secrets(property_id, stream_id)


def list_measurement_protocol_secrets(property_id, stream_id):
"""Lists measurement protocol secrets for the Android app data stream."""
client = AnalyticsAdminServiceClient()
results = client.list_measurement_protocol_secrets(
parent=f"properties/{property_id}/androidAppDataStreams/{stream_id}"
)

print("Result:")
for measurement_protocol_secret in results:
print("Result:")
print(f"Resource name: {measurement_protocol_secret.name}")
print(f"Secret value: {measurement_protocol_secret.secret_value}")
print(f"Display name: {measurement_protocol_secret.display_name}")
print()


# [END analyticsadmin_properties_android_app_data_streams_measurement_protocol_secrets_list]


if __name__ == "__main__":
run_sample()
Original file line number Diff line number Diff line change
@@ -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_measurement_protocol_secrets_list


TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
TEST_STREAM_ID = os.getenv("GA_TEST_ANDROID_APP_DATA_STREAM_ID")


def test_properties_android_app_data_streams_measurement_protocol_secrets_list(capsys):
properties_android_app_data_streams_measurement_protocol_secrets_list.list_measurement_protocol_secrets(
TEST_PROPERTY_ID, TEST_STREAM_ID
)
out, _ = capsys.readouterr()
assert "Result" in out
Loading