JotForm leads are not being captured on the initial load page.

gerard_reyes Posted by: Gerard Reyes Date Posted: January 20, 2025
Back to Case Studies

Issue

JotForm leads are not being captured on the initial load page.

Solution

Created a custom Javascript to retry capturing until a lead is captured.

<script>
// Function to get all query string parameters
function getAllQueryParameters() {
     var params = {};
     var queryString = window.location.search;
     var urlParams = new URLSearchParams(queryString);

     // Iterate over each parameter in the URL
     urlParams.forEach(function(value, key) {
     // Decode the value and store it in the params object
     params[key] = decodeURIComponent(value.replace(/\+/g, " "));
     });

     return params;
}

var form_id = 'fx-quote';
var gtm_tag = 'cHTML - MCFX request quote';

var attempts = 0; // Initialize the attempts counter
var maxAttempts = 5; // Set the maximum number of attempts

var interval = setInterval(function() {
     console.log('undefined' !== typeof mcfx);

     if ('undefined' !== typeof mcfx) {
         // Get all query parameters dynamically
         var fields = getAllQueryParameters();

         var wrapper = document.createElement('div');
         var form = '<form id="' + form_id + '">'; // Create a fake form

         Object.keys(fields).map(function(field) {
             var label = field;
             var val = fields[field];
             form += '<input type="hidden" value="' + val + '" name="' + label + '">';
         });
         form += '</form>';

         wrapper.innerHTML = form;

         mcfx(function(tracker) {
             tracker.capture(wrapper.firstChild);
             console.log('MCFX capture success - ' + gtm_tag);
             //clearInterval(interval); // Stop the interval after capturing
         });

         console.log(fields);

         attempts++; // Increment the attempts counter

         // Check if the maximum number of attempts has been reached
         if (attempts >= maxAttempts) {
             console.log('Maximum attempts reached. Stopping the interval.');
             clearInterval(interval); // Stop the interval after max attempts
         }
     }
}, 1000); // 1000 milliseconds = 1 second
</script>

Achievement

Lead is captured.

Snippet/Screenshot/Link

Task: Link

Code: Screenshot