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

Commit

Permalink
docs(samples): add samples for Conversion Event management methods (#153
Browse files Browse the repository at this point in the history
)

* 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 Conversion Event management methods

* add samples for Conversion Event management methods

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
ikuleshov and parthea committed Oct 19, 2021
1 parent f6e98eb commit 126f271
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/noxfile_config.py
Expand Up @@ -15,5 +15,6 @@
"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_CONVERSION_EVENT_ID": "2719963095"
},
}
62 changes: 62 additions & 0 deletions samples/properties_conversion_events_create.py
@@ -0,0 +1,62 @@
#!/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 conversion
event for the Google Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/create
for more information.
"""
# [START analyticsadmin_properties_conversion_events_create]
from google.analytics.admin import AnalyticsAdminServiceClient
from google.analytics.admin_v1alpha import ConversionEvent


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_conversion_event(property_id)


def create_conversion_event(property_id):
"""Creates a conversion event for the Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
conversion_event = client.create_conversion_event(
parent=f"properties/{property_id}",
conversion_event=ConversionEvent(event_name="test_purchase"),
)

print("Result:")
print(f"Resource name: {conversion_event.name}")
print(f"Event name: {conversion_event.event_name}")
print(f"Create time: {conversion_event.create_time}")
print(f"Deletable: {conversion_event.deletable}")
print(f"Custom: {conversion_event.custom}")


# [END analyticsadmin_properties_conversion_events_create]

if __name__ == "__main__":
run_sample()
26 changes: 26 additions & 0 deletions samples/properties_conversion_events_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_conversion_events_create

FAKE_PROPERTY_ID = "1"


def test_properties_conversion_events_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_conversion_events_create.create_conversion_event(FAKE_PROPERTY_ID)
60 changes: 60 additions & 0 deletions samples/properties_conversion_events_delete.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 deletes a conversion
event for the Google Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/delete
for more information.
"""
# [START analyticsadmin_properties_conversion_events_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 conversion event ID
# (e.g. "123456") before running the sample.
conversion_event_id = "YOUR-CONVERSION-EVENT-ID"

delete_conversion_event(property_id, conversion_event_id)


def delete_conversion_event(property_id, conversion_event_id):
"""Deletes the conversion event for the Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
client.delete_conversion_event(
name=f"properties/{property_id}/conversionEvents/{conversion_event_id}"
)
print("Conversion event deleted")


# [END analyticsadmin_properties_conversion_events_delete]


if __name__ == "__main__":
run_sample()
29 changes: 29 additions & 0 deletions samples/properties_conversion_events_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_conversion_events_delete

FAKE_PROPERTY_ID = "1"
FAKE_CONVERSION_EVENT_ID = "1"


def test_properties_conversion_events_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_conversion_events_delete.delete_conversion_event(
FAKE_PROPERTY_ID, FAKE_CONVERSION_EVENT_ID
)
59 changes: 59 additions & 0 deletions samples/properties_conversion_events_get.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 prints the conversion
event details.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/get
for more information.
"""
# [START analyticsadmin_properties_conversion_events_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 conversion event ID
# (e.g. "123456") before running the sample.
conversion_event_id = "YOUR-CONVERSION-EVENT-ID"

get_conversion_event(property_id, conversion_event_id)


def get_conversion_event(property_id, conversion_event_id):
"""Retrieves the details for the conversion event."""
client = AnalyticsAdminServiceClient()
conversion_event = client.get_conversion_event(
name=f"properties/{property_id}/conversionEvents/{conversion_event_id}"
)

print("Result:")
print(f"Resource name: {conversion_event.name}")
print(f"Event name: {conversion_event.event_name}")
print(f"Create time: {conversion_event.create_time}")
print(f"Deletable: {conversion_event.deletable}")
print(f"Custom: {conversion_event.custom}")


# [END analyticsadmin_properties_conversion_events_get]


if __name__ == "__main__":
run_sample()
28 changes: 28 additions & 0 deletions samples/properties_conversion_events_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_conversion_events_get

TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
TEST_CONVERSION_EVENT_ID = os.getenv("GA_TEST_CONVERSION_EVENT_ID")


def test_properties_conversion_events_get(capsys):
properties_conversion_events_get.get_conversion_event(
TEST_PROPERTY_ID, TEST_CONVERSION_EVENT_ID
)
out, _ = capsys.readouterr()
assert "Result" in out
55 changes: 55 additions & 0 deletions samples/properties_conversion_events_list.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 lists conversion events
for the Google Analytics 4 property.
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/list
for more information.
"""
# [START analyticsadmin_properties_conversion_events_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_conversion_events(property_id)


def list_conversion_events(property_id):
"""Lists conversion events for the Google Analytics 4 property."""
client = AnalyticsAdminServiceClient()
results = client.list_conversion_events(parent=f"properties/{property_id}")

print("Result:")
for conversion_event in results:
print(f"Resource name: {conversion_event.name}")
print(f"Event name: {conversion_event.event_name}")
print(f"Create time: {conversion_event.create_time}")
print(f"Deletable: {conversion_event.deletable}")
print(f"Custom: {conversion_event.custom}")
print()


# [END analyticsadmin_properties_conversion_events_list]


if __name__ == "__main__":
run_sample()
25 changes: 25 additions & 0 deletions samples/properties_conversion_events_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_conversion_events_list

TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")


def test_properties_conversion_events_list(capsys):
properties_conversion_events_list.list_conversion_events(TEST_PROPERTY_ID)
out, _ = capsys.readouterr()
assert "Result" in out

0 comments on commit 126f271

Please sign in to comment.