|
| 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() |
0 commit comments