Custom Tracking same form using tabbing option for specific lead capture

Posted by: Nico Cagalitan Date Posted: April 29, 2025
Back to Case Studies

Issue

The client/IM wants to track the form separately because the two options/choices have different form fields, but due to its configuration, it uses the same form with the same attributes. So, the form will capture the same lead, which captures unreliable or incorrect data values.

 

Solution

Constructed a custom tracking that will trigger a virtual form based on the option a user selects between Residential and Commercial.

(function() {

    var currentTab = '';

 

    function getCurrentTab() {

        var activeTab = document.querySelector('.jet-tabs__control.active-tab');

        if (activeTab) {

            var tabText = activeTab.querySelector('.jet-tabs__label-text').textContent;

            console.log('Current active tab found:', tabText);

            return tabText;

        }

        console.log('No active tab found');

        return 'Residential';

    }

 

    function captureForm(formId) {

        currentTab = getCurrentTab();

        console.log('Attempting capture for tab:', currentTab, 'with formId:', formId);

 

        var fxform = document.querySelector('#' + formId + ' form');

        console.log('Form element found:', fxform);

 

        if (fxform) {

            // Create a new virtual form element

            var virtualForm = document.createElement('form');

            virtualForm.setAttribute('id', currentTab === 'Residential' ? 'fx-residential-form' : 'fx-commercial-form');

            console.log('Created virtual form with ID:', virtualForm.id);

 

            // Clone and append each input/textarea from the original div to the virtual form

            var fields = fxform.querySelectorAll('input, textarea, select');

            console.log('Found fields to clone:', fields.length);

           

            for (var i = 0; i < fields.length; i++) {

                var fxfield = fields[i].cloneNode(true);

               

                if (!fxfield.getAttribute('name')) {

                    fxfield.setAttribute('name', 'fx-' + i);

                }

               

                virtualForm.appendChild(fxfield);

            }

 

            // Add a hidden field to indicate which tab was used

            var tabIndicator = document.createElement('input');

            tabIndicator.setAttribute('type', 'hidden');

            tabIndicator.setAttribute('name', 'form_type');

            tabIndicator.setAttribute('value', currentTab);

            virtualForm.appendChild(tabIndicator);

           

            console.log('About to capture form for tab:', currentTab);

            mcfx('capture', virtualForm);

            console.log('Virtual form captured:', virtualForm);

        }

    }

 

    function setupSubmitButton(formId) {

        var fxform = document.querySelector('#' + formId + ' form');

        if (!fxform) {

            console.log('Form not found for:', formId);

            return;

        }

 

        var submitButton = fxform.querySelector('button[type="submit"]');

        if (!submitButton) {

            console.log('Submit button not found for:', formId);

            return;

        }

 

        // Remove existing button and its listeners

        var newButton = submitButton.cloneNode(true);

        submitButton.parentNode.replaceChild(newButton, submitButton);

 

        // Add new click listener

        newButton.addEventListener('click', function() {

            console.log('Submit clicked for:', formId);

            captureForm(formId);

        });

       

        console.log('Submit button set up for:', formId);

    }

 

    // Set up tab click listeners

    var tabControls = document.querySelectorAll('.jet-tabs__control');

    console.log('Found tab controls:', tabControls.length);

 

    for (var i = 0; i < tabControls.length; i++) {

        tabControls[i].addEventListener('click', function() {

            setTimeout(function() {

                currentTab = getCurrentTab();

                console.log('Tab switched to:', currentTab);

               

                // Setup the appropriate form based on tab

                if (currentTab === 'Residential') {

                    setupSubmitButton('jet-tabs-content-2171');

                } else if (currentTab === 'Commercial') {

                    setupSubmitButton('jet-tabs-content-2172');

                }

            }, 100);

        });

    }

 

    // Initialize

    currentTab = getCurrentTab();

    console.log('Initial tab state:', currentTab);

   

    // Setup initial form based on active tab

    if (currentTab === 'Residential') {

        setupSubmitButton('jet-tabs-content-2171');

    } else if (currentTab === 'Commercial') {

        setupSubmitButton('jet-tabs-content-2172');

    }

 

    console.log('Script initialized with tab:', currentTab);

})();

Achievement

Constructed a custom tracking that will capture the forms separately between 2 options: Residential or Commercial.

It is now tracking correctly.

Snippet/Screenshot/Link

Ticket: link

Code: screenshot