Filter: wpd_ai_event_data_before_insertion

Adjust or enrich Alpha Insights event payloads immediately before they are written to the events database table.

Description

This filter fires after Alpha Insights finishes normalizing and sanitizing an event payload but before the final insert statement runs. It gives developers one last chance to tweak values, append metadata, or enforce store-specific policies on tracked events. Typical use cases include injecting derived metrics, mapping product identifiers, or attaching contextual data gathered elsewhere in your stack.

Location

File: includes/classes/WPDAI_WooCommerce_Event_Tracking.php

Method: WPDAI_WooCommerce_Event_Tracking::insert_event()

Line: ~354

Parameters

Parameter Type Description
$data array Fully prepared event payload that will be inserted into wpd_ai_events. Keys include session_id, ip_address, user_id, page_href, object_type, object_id, event_type, event_quantity, event_value, product_id, variation_id, date_created_gmt, and additional_data.

Return

Type: array

Description: The payload to persist. Always return the complete array, even if you only modify one value.

Common Use Cases

Example Usage

Add Campaign Metadata

add_filter( 'wpd_ai_event_data_before_insertion', 'ai_attach_campaign_metadata' );
function ai_attach_campaign_metadata( $data )
{
    $metadata = array(
        'campaign' => isset( $_COOKIE['ai_campaign'] ) ? sanitize_text_field( $_COOKIE['ai_campaign'] ) : '',
        'utm_source' => isset( $_GET['utm_source'] ) ? sanitize_text_field( $_GET['utm_source'] ) : '',
        'utm_medium' => isset( $_GET['utm_medium'] ) ? sanitize_text_field( $_GET['utm_medium'] ) : '',
    );
    if ( ! empty( $metadata['campaign'] ) || ! empty( $metadata['utm_source'] ) )
    {
        $existing = isset( $data['additional_data'] ) && ! empty( $data['additional_data'] )
            ? json_decode( $data['additional_data'], true )
            : array();
        $data['additional_data'] = wp_json_encode( array_merge( $existing, $metadata ) );
    }
    return $data;
}

Enforce Minimum Event Value

add_filter( 'wpd_ai_event_data_before_insertion', 'ai_floor_event_value' );
function ai_floor_event_value( $data )
{
    if ( isset( $data['event_value'] ) && $data['event_value'] < 0 )
    {
        $data['event_value'] = 0.00;
    }
    return $data;
}

Skip Unsupported Events

add_filter( 'wpd_ai_event_data_before_insertion', 'ai_skip_untrusted_events' );
function ai_skip_untrusted_events( $data )
{
    if ( empty( $data['page_href'] ) || false === strpos( $data['page_href'], 'yourstore.com' ) )
    {
        // Return early by throwing a WP_Error or logging & returning untouched data.
        // To fully halt insertion, hook into WPDAI_WooCommerce_Event_Tracking earlier and stop the process.
        $data['additional_data'] = wp_json_encode( array( 'discarded_event' => true ) );
    }
    return $data;
}

Best Practices

Security Considerations

Related Filters

Related Classes