Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions assets/js/src/integration/fluentforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,38 @@

$( document ).on(
'fluentform_submission_success',
function ( event, formDetails ) {
// Extract necessary form details from the event.
const formEl = formDetails.form; // The form element
const formConfig = formDetails.config; // The form configuration (contains formId, etc.)
const formId = formConfig.id;
const formInstanceId = formEl.data( 'form_instance' );
function ( _event, formDetails ) {
// FluentForms fires this event twice per submission:
// 1. First with formDetails (complete data)
// 2. Second without formDetails (undefined)
// We only process the first event with actual data.
if (
! formDetails ||
! formDetails.config ||
! formDetails.config.id
) {
return;
}

const formId = formDetails.config.id;

// FluentForms passes form as jQuery object in formDetails.form.
let formEl = formDetails.form;

// Ensure it's a valid jQuery object with elements.
if ( ! formEl || ! formEl.length ) {
// Fallback: try to find form by ID attribute (FluentForms uses id="fluentform_X").
formEl = $( '#fluentform_' + formId );
}

if ( ! formEl || ! formEl.length ) {
return;
}

const formInstanceId = formEl.data( 'form_instance' ) || null;

// All the magic happens here.
window.PUM.integrations.formSubmission( $( formEl ), {
window.PUM.integrations.formSubmission( formEl, {
formProvider,
formId,
formInstanceId,
Expand Down
10 changes: 9 additions & 1 deletion assets/js/src/site/plugins/pum-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@

return;
} catch ( error ) {
// Fall back to image beacon if sendBeacon fails
// Fall back to image beacon if sendBeacon fails.
// eslint-disable-next-line no-console
console.warn(
'sendBeacon failed, falling back to image beacon:',
error
Expand Down Expand Up @@ -141,6 +142,13 @@
10
) || null,
event: 'conversion',
eventData: {
type: 'form_submission',
formProvider: args.formProvider || null,
formId: args.formId || null,
formKey: args.formKey || null,
formInstanceId: args.formInstanceId || null,
},
};

// Shortcode popups use negative numbers, and single-popup (preview mode) shouldn't be tracked.
Expand Down
33 changes: 31 additions & 2 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ public static function endpoint_absint( $param ) {
return is_numeric( $param );
}

/**
* Sanitize eventData parameter (matches Pro's approach).
*
* Decodes JSON string to array if needed.
*
* @param mixed $value EventData value (JSON string or array).
*
* @return array Decoded eventData as array.
*/
public static function sanitize_event_data( $value ) {
// If already an array, return as-is.
if ( is_array( $value ) ) {
return $value;
}

// Decode JSON string to array (matches Pro's Request::parse_tracking_data).
if ( is_string( $value ) ) {
$decoded = json_decode( $value, true );
return is_array( $decoded ) ? $decoded : [];
}

return [];
}

/**
* Registers the analytics endpoints
*/
Expand All @@ -181,18 +205,23 @@ public static function register_endpoints() {
'callback' => [ __CLASS__, 'analytics_endpoint' ],
'permission_callback' => '__return_true',
'args' => [
'event' => [
'event' => [
'required' => true,
'description' => __( 'Event Type', 'popup-maker' ),
'type' => 'string',
],
'pid' => [
'pid' => [
'required' => true,
'description' => __( 'Popup ID', 'popup-maker' ),
'type' => 'integer',
'validation_callback' => [ __CLASS__, 'endpoint_absint' ],
'sanitize_callback' => 'absint',
],
'eventData' => [
'required' => false,
'description' => __( 'Event metadata (JSON or array)', 'popup-maker' ),
'sanitize_callback' => [ __CLASS__, 'sanitize_event_data' ],
],
],
]
)
Expand Down
16 changes: 16 additions & 0 deletions classes/Plugin/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ function ( $container ) {
}
);

$this->set(
'form_conversion_tracking',
/**
* Get form conversion tracking service.
*
* @return \PopupMaker\Services\FormConversionTracking
*/
function ( $container ) {
return new \PopupMaker\Services\FormConversionTracking( $container );
}
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

do_action( 'popup_maker/register_services', $this );
}

Expand All @@ -318,6 +330,10 @@ protected function init_services() {
// Initialize link click tracking.
$link_click_tracking = $this->get( 'link_click_tracking' );
$link_click_tracking->init();

// Initialize form conversion tracking.
$form_conversion_tracking = $this->get( 'form_conversion_tracking' );
$form_conversion_tracking->init();
}

/**
Expand Down
Loading
Loading