Skip to content

Commit

Permalink
docs: add samples for Measurement Protocol Secrets management methods (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>

* Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py

Co-authored-by: Anthonios Partheniou <[email protected]>

* Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py

Co-authored-by: Anthonios Partheniou <[email protected]>

* Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py

Co-authored-by: Anthonios Partheniou <[email protected]>

* Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py

Co-authored-by: Anthonios Partheniou <[email protected]>

* Update samples/properties_web_data_streams_measurement_protocol_secrets_delete.py

Co-authored-by: Anthonios Partheniou <[email protected]>

Co-authored-by: Anthonios Partheniou <[email protected]>
  • Loading branch information
ikuleshov and parthea authored Oct 19, 2021
1 parent 276fa22 commit 57237cc
Show file tree
Hide file tree
Showing 31 changed files with 1,435 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/google-analytics-admin/samples/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"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",
"GA_TEST_CONVERSION_EVENT_ID": "2719963095"
},
}
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

0 comments on commit 57237cc

Please sign in to comment.