This website uses cookies to ensure you get the best experience on our website.
/**
* Generates a shared event ID for Meta Pixel + Conversions API deduplication
* on Elementor Pro forms, and fires the client-side "Lead" Pixel event with
* that same ID after a successful submission.
*
* Pairs with meta-capi-lead-functions.php, which reads the same event_id
* back out of the submitted form data and sends the server-side event.
*
* Add this via Elementor Pro's Custom Code (Templates - Custom Code, set to
* load in the footer on the relevant pages), or enqueue it from your child
* theme.
*
* Requires: a Hidden field with ID "event_id" added to the form in the
* Elementor form editor (leave its default value blank).
*/
(function ($) {
'use strict';
function generateEventId() {
if (window.crypto && window.crypto.randomUUID) {
return window.crypto.randomUUID();
}
// Fallback for older browsers without crypto.randomUUID.
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
$(function () {
// Populate every Elementor form's hidden event_id field on page load,
// so it's already set by the time the visitor submits.
$('form.elementor-form').each(function () {
var $eventIdField = $(this).find('input[name="form_fields[event_id]"]');
if ($eventIdField.length) {
$eventIdField.val(generateEventId());
}
});
});
// Elementor Pro fires "submit_success" on the form wrapper after a
// successful AJAX submission — this is when we fire the client Pixel
// event, using the same ID the server just received.
$(document).on('submit_success', function (event, response) {
var $form = $(event.target);
var eventId = $form.find('input[name="form_fields[event_id]"]').val();
if (typeof fbq === 'function' && eventId) {
fbq('track', 'Lead', {}, { eventID: eventId });
}
// Refresh the ID in case the same form gets submitted again on the
// same page load.
$form.find('input[name="form_fields[event_id]"]').val(generateEventId());
});
})(jQuery);