GTM Listener for Multi-Page NinjaForm

Black and white photo of Ron Geronimo Posted by: Ronel Geronimo Date Posted: August 3, 2026
Back to Case Studies

Issue

While working on an RCFX setup task, I needed to set up form tracking for a Ninja Form on a client site. The MCFX capture listener I had worked fine on single-page forms, but this form was broken into multiple sections. When I tested it, only the fields from the last section (where the Submit button is) came through — none of the info from the earlier sections made it into LeadManagerFX.

Solution

I found out that Ninja Forms removes the earlier sections from the page as you move through them, so by the time the form submits, the listener can only “see” the last section. To get around this, I updated the listener so it takes a snapshot of each section’s fields as you move between them (Next, Previous, or the breadcrumb steps), then adds those saved fields back in right before the form submits. It’s built so it doesn’t cause any issues on single-section forms either — it just runs like normal.

Achievement

One listener that captures the full form submission in LeadManagerFX for both single- and multi-section Ninja Forms

Snippet/Screenshot/Link

 

<script>

jQuery(document).ready(function(){
var nfCaptured = {};

function isField(el){
var c = el.className;
return typeof c === 'string' && (' ' + c + ' ').indexOf(' ninja-forms-field ') > -1;
}

function snapshot(cont){
var m = cont.id.match(/nf-form-(\d+)-cont/);
if (!m) return;
var store = nfCaptured[m[1]] || (nfCaptured[m[1]] = {});
var els = cont.getElementsByTagName('*');
var seen = {};
for (var i = 0; i < els.length; i++){
var el = els[i];
if (!isField(el)) continue;
var name = el.getAttribute('name');
if (!name || name === 'nf-field-hp') continue;
var tag = el.tagName.toLowerCase();
var type = (el.getAttribute('type') || '').toLowerCase();
if (tag === 'button' || type === 'submit') continue;
if (!seen[name]){ store[name] = []; seen[name] = true; }
if (type === 'checkbox' || type === 'radio'){
if (el.checked) store[name].push(el.value);
} else {
store[name] = [el.value];
}
}
}

function presentNames(cont){
var set = {};
var els = cont.getElementsByTagName('*');
for (var i = 0; i < els.length; i++){
if (!isField(els[i])) continue;
var n = els[i].getAttribute('name');
if (n) set[n] = true;
}
return set;
}

function navAncestor(el){
var depth = 0;
while (el && el.nodeType === 1 && depth < 4){
var c = el.className;
if (typeof c === 'string'){
var s = ' ' + c + ' ';
if (s.indexOf(' nf-next ') > -1 || s.indexOf(' nf-previous ') > -1 || s.indexOf(' nf-breadcrumb ') > -1) return el;
}
el = el.parentNode; depth++;
}
return null;
}

function contAncestor(el){
while (el && el.nodeType === 1){
var c = el.className;
if (typeof c === 'string' && (' ' + c + ' ').indexOf(' nf-form-cont ') > -1) return el;
el = el.parentNode;
}
return null;
}

document.addEventListener('click', function(e){
if (!navAncestor(e.target)) return;
var cont = contAncestor(e.target);
if (cont) snapshot(cont);
}, true);

var mcfxSubmitController = Marionette.Object.extend({
initialize: function(){
this.listenTo(Backbone.Radio.channel('forms'), 'before:submit', this.actionSubmit);
},
actionSubmit: function(response){
var cont = document.getElementById('nf-form-' + response.id + '-cont');
if (!cont) return;
var form = cont.querySelector('form');
if (!form || typeof mcfx === 'undefined') return;
snapshot(cont);
var store = nfCaptured[response.id] || {};
var here = presentNames(cont);
var holder = document.createElement('div');
holder.style.display = 'none';
for (var name in store){
if (!store.hasOwnProperty(name) || here[name]) continue;
var vals = store[name];
for (var j = 0; j < vals.length; j++){
var input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = vals[j];
holder.appendChild(input);
}
}
form.appendChild(holder);
mcfx('capture', form);
form.removeChild(holder);
}
});
new mcfxSubmitController();
});

</script>