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

Commit 126f271

Browse files
ikuleshovparthea
andauthored
docs(samples): add samples for Conversion Event management methods (#153)
* 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>
1 parent f6e98eb commit 126f271

9 files changed

Lines changed: 345 additions & 0 deletions

samples/noxfile_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
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_CONVERSION_EVENT_ID": "2719963095"
1819
},
1920
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC 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 conversion
18+
event for the Google Analytics 4 property.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/create
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_conversion_events_create]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha import ConversionEvent
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 account 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+
create_conversion_event(property_id)
41+
42+
43+
def create_conversion_event(property_id):
44+
"""Creates a conversion event for the Google Analytics 4 property."""
45+
client = AnalyticsAdminServiceClient()
46+
conversion_event = client.create_conversion_event(
47+
parent=f"properties/{property_id}",
48+
conversion_event=ConversionEvent(event_name="test_purchase"),
49+
)
50+
51+
print("Result:")
52+
print(f"Resource name: {conversion_event.name}")
53+
print(f"Event name: {conversion_event.event_name}")
54+
print(f"Create time: {conversion_event.create_time}")
55+
print(f"Deletable: {conversion_event.deletable}")
56+
print(f"Custom: {conversion_event.custom}")
57+
58+
59+
# [END analyticsadmin_properties_conversion_events_create]
60+
61+
if __name__ == "__main__":
62+
run_sample()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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_conversion_events_create
18+
19+
FAKE_PROPERTY_ID = "1"
20+
21+
22+
def test_properties_conversion_events_create():
23+
# This test ensures that the call is valid and reaches the server, even
24+
# though the operation does not succeed due to permission error.
25+
with pytest.raises(Exception, match="403 The caller does not have permission"):
26+
properties_conversion_events_create.create_conversion_event(FAKE_PROPERTY_ID)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC 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 a conversion
18+
event for the Google Analytics 4 property.
19+
20+
21+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/delete
22+
for more information.
23+
"""
24+
# [START analyticsadmin_properties_conversion_events_delete]
25+
from google.analytics.admin import AnalyticsAdminServiceClient
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 conversion event ID
41+
# (e.g. "123456") before running the sample.
42+
conversion_event_id = "YOUR-CONVERSION-EVENT-ID"
43+
44+
delete_conversion_event(property_id, conversion_event_id)
45+
46+
47+
def delete_conversion_event(property_id, conversion_event_id):
48+
"""Deletes the conversion event for the Google Analytics 4 property."""
49+
client = AnalyticsAdminServiceClient()
50+
client.delete_conversion_event(
51+
name=f"properties/{property_id}/conversionEvents/{conversion_event_id}"
52+
)
53+
print("Conversion event deleted")
54+
55+
56+
# [END analyticsadmin_properties_conversion_events_delete]
57+
58+
59+
if __name__ == "__main__":
60+
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 pytest
16+
17+
import properties_conversion_events_delete
18+
19+
FAKE_PROPERTY_ID = "1"
20+
FAKE_CONVERSION_EVENT_ID = "1"
21+
22+
23+
def test_properties_conversion_events_delete():
24+
# This test ensures that the call is valid and reaches the server, even
25+
# though the operation does not succeed due to permission error.
26+
with pytest.raises(Exception, match="403 The caller does not have permission"):
27+
properties_conversion_events_delete.delete_conversion_event(
28+
FAKE_PROPERTY_ID, FAKE_CONVERSION_EVENT_ID
29+
)
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 LLC 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 conversion
18+
event details.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/get
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_conversion_events_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 conversion event ID
34+
# (e.g. "123456") before running the sample.
35+
conversion_event_id = "YOUR-CONVERSION-EVENT-ID"
36+
37+
get_conversion_event(property_id, conversion_event_id)
38+
39+
40+
def get_conversion_event(property_id, conversion_event_id):
41+
"""Retrieves the details for the conversion event."""
42+
client = AnalyticsAdminServiceClient()
43+
conversion_event = client.get_conversion_event(
44+
name=f"properties/{property_id}/conversionEvents/{conversion_event_id}"
45+
)
46+
47+
print("Result:")
48+
print(f"Resource name: {conversion_event.name}")
49+
print(f"Event name: {conversion_event.event_name}")
50+
print(f"Create time: {conversion_event.create_time}")
51+
print(f"Deletable: {conversion_event.deletable}")
52+
print(f"Custom: {conversion_event.custom}")
53+
54+
55+
# [END analyticsadmin_properties_conversion_events_get]
56+
57+
58+
if __name__ == "__main__":
59+
run_sample()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_conversion_events_get
18+
19+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
20+
TEST_CONVERSION_EVENT_ID = os.getenv("GA_TEST_CONVERSION_EVENT_ID")
21+
22+
23+
def test_properties_conversion_events_get(capsys):
24+
properties_conversion_events_get.get_conversion_event(
25+
TEST_PROPERTY_ID, TEST_CONVERSION_EVENT_ID
26+
)
27+
out, _ = capsys.readouterr()
28+
assert "Result" in out
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC 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 conversion events
18+
for the Google Analytics 4 property.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties.conversionEvents/list
21+
for more information.
22+
"""
23+
# [START analyticsadmin_properties_conversion_events_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_conversion_events(property_id)
34+
35+
36+
def list_conversion_events(property_id):
37+
"""Lists conversion events for the Google Analytics 4 property."""
38+
client = AnalyticsAdminServiceClient()
39+
results = client.list_conversion_events(parent=f"properties/{property_id}")
40+
41+
print("Result:")
42+
for conversion_event in results:
43+
print(f"Resource name: {conversion_event.name}")
44+
print(f"Event name: {conversion_event.event_name}")
45+
print(f"Create time: {conversion_event.create_time}")
46+
print(f"Deletable: {conversion_event.deletable}")
47+
print(f"Custom: {conversion_event.custom}")
48+
print()
49+
50+
51+
# [END analyticsadmin_properties_conversion_events_list]
52+
53+
54+
if __name__ == "__main__":
55+
run_sample()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_conversion_events_list
18+
19+
TEST_PROPERTY_ID = os.getenv("GA_TEST_PROPERTY_ID")
20+
21+
22+
def test_properties_conversion_events_list(capsys):
23+
properties_conversion_events_list.list_conversion_events(TEST_PROPERTY_ID)
24+
out, _ = capsys.readouterr()
25+
assert "Result" in out

0 commit comments

Comments
 (0)