Event Object

The event object in a transformation carries the properties and metadata of the Event as provided by the Source.

There are two main components of the object:

  • event_name: The name of the Event Type. This value is visible in the Schema Mapper for you to map it to a Destination table.

  • properties: The properties of the Event in the form of key-value pairs (dict).

For example:

{
    "event_name": "users",
    "properties": {
	     "id":56712434,
	     "first_name": "John",
	     "last_name": "Doe",
	     "age": 42,
	     "is_active": true,
	     "last_logged_in": 1507893200464
    }
}

Properties of a given Event can be manipulated using the Transformations code to add, edit, or delete fields in any way you want.

The following is the complete API Reference to the event object.

Event(string: eventname, dict: properties) - Event

Description: Constructs a new event object from an existing Event with given name and properties.

Example:

The following transformation creates two separate Events, electronics and phones with the same properties as the original Event.

from io.hevo.api import Event

def transform(event):

    # Get properties from the event #
    properties = event.getProperties()

    # Create two events (electronics, phones) from the original event
    del properties['type']
    electronics = Event('electronics', properties)
    phones = Event('phones', properties)

    # return an array of new events
    return [electronics, phones]

Sample output:

Two separate Events electronics and phones are created.

Two Events are created from one Event

getEventName() - string

Description: Returns the name of the Event.

Example:

The following transformation retrieves the Event name, and if it is discontinued_products, filters the Event.

from io.hevo.api import Event

def transform(event):
    # Get event name from the event
    eventName = event.getEventName()

    if eventName == 'activity':
        return None

    return event

Sample output:

The Event is filtered as Event name is activity.

The Event is filtered

getProperties() - dict

Description: Returns a dictionary (key-valyue pairs) of all the properties of the Event.

Example:

The following transformation retrieves the dictionary of the properties and modifies the value of a specific property.

from io.hevo.api import Event

def transform(event):
    # Get properties from the event
    properties = event.getProperties()
    # Add a discount
    properties['price'] = properties['price'] * 0.50

    return event

Sample output:

The value of the property price is updated.

Value of property is updated

setProperties(dict: properties)

Description: Sets new properties for the Event.

Example:

The following transformation fetches the existing properties of an Event and sets new properties for it by modifying and adding to the existing ones.

from io.hevo.api import Event

def transform(event):
    # Get properties from the event
    properties = event.getProperties()

    new_properties = {}
    new_properties['id'] = 'product_' + properties['_id']
    new_properties['name'] = properties['name']
    new_properties['price'] = properties['price'] * 0.9
    new_properties['region'] = 'eu'

    # Set a dictionary as the properties
    event.setProperties(new_properties)

    return event

Sample output:

A new set of properties is assigned to the Event, and some of the earlier properties are retained with modified values.

New properties are added to the Event

rename(string: new_event_name)

Description: Renames an Event with a new name of choice.

Example:

The following transformation picks Events with name as products and renames them to upcoming_products.

from io.hevo.api import Event

def transform(event):
    # Get event name from the event #
    eventName = event.getEventName()

    # Update the eventName
    # (This is how it will appear in the destination)
    if eventName == 'products':
        event.rename('upcoming_' + eventName)

    return event

Sample output:

The Event products is renamed.

Event is renamed

isString(string: fieldname) - boolean

Description: Returns True if the value of the specified property is a string.

Example:

The following transformation code checks if the value of the property price is of type String and converts it into Number.

from io.hevo.api import Event

def transform(event):
    properties = event.getProperties()

    # Check if a field 'price' is String
    if event.isString('price'):
        properties['price'] = int(properties['price'])

    return event

Sample output:

The data type of the property price is changed from String to Number.

The property's data type is changed from String to Number.

isNumber(string: fieldname) - boolean

Description: Returns True if the value of the specified property is present and is a number.

Example:

The following transformation checks if the value of the property price is of type Number and updates it to a new value, else deletes the property.

def transform(event):
    properties = event.getProperties()

    # Check if a field 'price' is Numeric
    if event.isNumber('price'):
        properties['price'] = properties['price'] * 0.80
    else:
        del properties['price']

    return event

Sample output:

  • If the value of the property price is numeric, it is modified:

    Numeric value of a property is updated

  • If the value of the property price is a string, it is dropped from the Event.

    String value of a property is dropped

isBoolean(string: fieldname) - boolean

Description: Returns True if the value of the specified property is present and is boolean.

Example:

The following transformation checks if the property available is boolean and updates its value.

from io.hevo.api import Event

def transform(event):
    properties = event.getProperties()

    # Check if a field 'available' is Boolean
    if event.isBoolean('available'):
        properties['available'] = 'Yes'

    return event

Sample output:

Boolean value is changed

isTimestamp(string: fieldname) - boolean

Description: Returns True if the value of the specified property is present and is a timestamp/datetime.

Example:

The following transformation updates the value of the property has_valid_report as True if the property report_at is of type Timestamp/Datetime.

from io.hevo.api import Event

def transform(event):
    properties = event.getProperties()

    # Check if a field 'report_at' is a Timestamp/Datetime
    if event.isTimestamp('report_at'):
        properties['has_valid_report'] = True
    else:
        properties['has_valid_report'] = False

    return event

Sample output:

If the property report_at is of type Timestamp, a new property has_valid_report with the value True is added to the Event.

isDate(string: fieldname) - boolean

Description: Returns True if the value of the specified field name is present and is a date. Note that for datetime values, the function returns False.

Example:

The following transformation adds a property has_valid_report and assigns it the value True if the report_at property is of type Date.

from io.hevo.api import Event

def transform(event):
    properties = event.getProperties()

    # Check if a field 'report_at' is a Date
    if event.isDate('report_at'):
        properties['has_valid_report'] = True
    else:
        properties['has_valid_report'] = False

    return event

Sample output:

If the property report_at is of type Date, a new property has_valid_report with the value True is added to the Event.

isTime(string: fieldname) - boolean

Description: Returns True if the value of the specified field name is present and is a time value. Please note that for datetime values it will return False.

Example:

The following transformation adds a property has_valid_updated_ts and assigns it the value True if the updated_ts property is of type Time, else assigns the value False.

from io.hevo.api import Event

def transform(event):
    properties = event.getProperties()

    # Check if a field 'updated_ts' is a Time field
    if event.isTime('updated_ts'):
        properties['has_valid_updated_ts'] = True
    else:
        properties['has_valid_updated_ts'] = False

    return event

Sample output:

If the property updated_ts of type Time, a new property has_valid_updated_ts with the value True is added to the Event.

getType( string: fieldname ) - string

Description: Returns the Type of the value of specified field name.

Example:

The following transformation checks for the data type of the property revenue and adds that as a new property to the Event.

from io.hevo.api import Event

def transform(event):
    eventName = event.getEventName()
    properties = event.getProperties()

    Type = event.getType('revenue')
    properties['type'] = Type

    return event

Sample output:

A new property type is added to the Event to indicate the data type of the property revenue.

getPrimaryKeyFields() - string[]

Description: Returns the list of fields which are primary keys in the Event.

Example:

The following transformation fetches the list of properties that are primary keys in the Event.

from io.hevo.api import Event

def transform(event):

    # Fetches the current set of primary keys for a given record
    pk_fields = event.getPrimaryKeyFields()

    properties = event.getProperties()
    properties['existing_pk_fields'] = pk_fields

    return event

Sample output:

The list of primary keys for the Event are added as a new property called existing_pk_fields.

setPrimaryKeyFields(string[]: fieldnames)

Description: Sets the specified list of field names as the primary key of the Event.

Example:

The following transformation sets the specified properties of an Event as the primary keys.

from io.hevo.api import Event

def transform(event):

    # Set a field (or a list of fields) as the primary key
    event.setPrimaryKeyFields(['journey_id', 'key'])

    properties = event.getProperties()
    properties['new_pk_fields'] = event.getPrimaryKeyFields()

    return event

Sample output:

The properties journey_id and key are marked as primary keys for the Event.

Last updated on 29 Mar 2021