def create_custom_event()

in python/ga4_setup/setup.py [0:0]


def create_custom_event(configuration: map, event_name: str):
  """
  Creates a custom event in Google Analytics 4 based on the provided event name.

  Args:
    configuration: A dictionary containing the Google Analytics 4 property ID and data stream ID.
    event_name: The name of the custom event to be created.

  Raises:
    GoogleAnalyticsAdminError: If an error occurs while creating the custom event.
  """
  client = admin_v1alpha.AnalyticsAdminServiceClient()
  event_create_rule = admin_v1alpha.EventCreateRule()
  condition = admin_v1alpha.MatchingCondition()

  condition.field = "event_name"
  condition.comparison_type = "EQUALS"
  condition.value = event_name

  event_create_rule.destination_event = event_name
  event_create_rule.event_conditions.append(condition)

  request = admin_v1alpha.CreateEventCreateRuleRequest(
    parent=f"properties/{configuration['property_id']}/dataStreams/{configuration['stream_id']}",
    event_create_rule=event_create_rule,
  )

  response = client.create_event_create_rule(request=request)