FreeGoogle Tag Manager recipes

Track Ninja Forms Submissions in Google Tag Manager with this Free GTM Recipe

A free, tested Google Tag Manager recipe that captures Ninja Forms submissions and turns them into a trigger you can use to fire conversions to Google Ads, Google Analytics and more.

Aaron Beashel
Aaron BeashelUpdated July 20267 min read
The Ninja Forms recipe tag inside Google Tag Manager

Google Tag Manager is a common way to send conversions to Google Ads, Google Analytics, etc when someone submits a Ninja Forms form on your website.

The problem is, it's quite difficult to set up. Ninja Forms uses AJAX to save form submissions, which means Google Tag Manager's built-in form submission trigger doesn't work, so you're left having to write custom code to detect the form submission, send an event to the dataLayer, and build a trigger on top of that.

And unless you're a developer or very experienced with Google Tag Manager, you're going to struggle.

Fortunately though, there is a simple solution.

You can simply install this Ninja Forms Conversion Tracking Recipe in your Google Tag Manager account and you'll have everything you need to get basic conversion tracking set up.

What Is a Google Tag Manager Recipe and What's in It?

A recipe is a pre-built bundle of Google Tag Manager assets (a tag, a trigger and a variable) that you import in one go instead of building each piece yourself. You can see all our recipes and how they work in the GTM recipe library.

This recipe detects successful Ninja Forms submissions, sends a 'Form Submitted' event to Google Tag Manager, and gives you a Trigger you can use to send conversions to Google Analytics, Google Ads, etc. There are three parts to it, and nothing inside is hidden. Here's exactly what each piece contains, so you can see what you're importing before you import it.

Custom HTML Tag

Listens for successful Ninja Forms submissions and pushes a 'ninja_forms_submitted' event to the dataLayer. It fires on All Pages, once per event, and this is the code inside it.

The Tag's CodeJavaScript · 149 lines
(function () {
  'use strict';
  var runtime = window.__converlyNinjaFormsRuntime;
  if (runtime && runtime.installed) return;

  // Preserve the recipe's public duplicate-install guard. If a superseded
  // snippet already set it without this runtime, fail closed: don't layer a
  // second listener over an unknown old detector.
  if (!runtime && window.__converlyRecipeNinjaForms) return;

  if (!runtime) {
    runtime = { seenEntryIds: Object.create(null), installed: false };
    window.__converlyNinjaFormsRuntime = runtime;
  }
  window.__converlyRecipeNinjaForms = true;
  window.dataLayer = window.dataLayer || [];

  var MISSING = {};
  var MAX_SAFE_INTEGER = 9007199254740991;

  // Read only own data properties. Callback payloads are normally parsed JSON,
  // but a filter or another page script can supply accessor-backed objects.
  // Do not execute getters while deciding whether a response proves a save.
  function ownDataValue(object, key) {
    if (!object || typeof object !== 'object' || !Object.prototype.hasOwnProperty.call(object, key)) return MISSING;
    if (typeof Object.getOwnPropertyDescriptor !== 'function') return MISSING;
    var descriptor = Object.getOwnPropertyDescriptor(object, key);
    if (!descriptor || !Object.prototype.hasOwnProperty.call(descriptor, 'value')) return MISSING;
    return descriptor.value;
  }

  function emptyErrors(value) {
    if (Object.prototype.toString.call(value) === '[object Array]') return value.length === 0;
    if (!value || typeof value !== 'object') return false;
    for (var key in value) {
      if (Object.prototype.hasOwnProperty.call(value, key)) return false;
    }
    return true;
  }

  // Accept only primitive safe integers or canonical decimal strings. Never
  // coerce booleans, arrays, objects, boxed numbers, or accessor-backed IDs.
  function positiveIntegerId(value) {
    if (typeof value === 'number') {
      if (!isFinite(value) || value <= 0 || Math.floor(value) !== value || value > MAX_SAFE_INTEGER) return null;
      return String(value);
    }
    if (typeof value !== 'string' || !/^[1-9]\d*$/.test(value)) return null;
    var number = Number(value);
    if (!isFinite(number) || number > MAX_SAFE_INTEGER || String(number) !== value) return null;
    return value;
  }

  function canonicalFormId(value) {
    var formId;
    if (typeof value === 'number') {
      if (!isFinite(value) || value <= 0 || Math.floor(value) !== value || value > MAX_SAFE_INTEGER) return null;
      formId = String(value);
    } else if (typeof value === 'string') {
      formId = value;
    } else {
      return null;
    }
    if (!/^[1-9]\d*(?:_(?:0|[1-9]\d*))*$/.test(formId)) return null;
    var parts = formId.split('_');
    for (var i = 0; i < parts.length; i++) {
      var part = Number(parts[i]);
      if (!isFinite(part) || part > MAX_SAFE_INTEGER || String(part) !== parts[i]) return null;
    }
    return formId;
  }

  function inspectResponse(response, callbackFormId) {
    var errors = ownDataValue(response, 'errors');
    if (errors === MISSING || !emptyErrors(errors)) return null;
    var data = ownDataValue(response, 'data');
    if (!data || typeof data !== 'object') return null;
    var halt = ownDataValue(data, 'halt');
    if (halt !== MISSING && halt) return null;
    var nestedErrors = ownDataValue(data, 'errors');
    if (nestedErrors !== MISSING && !emptyErrors(nestedErrors)) return null;
    var formValue = ownDataValue(data, 'form_id');
    var formId = formValue === MISSING ? null : canonicalFormId(formValue);
    if (!formId || canonicalFormId(callbackFormId) !== formId) return null;
    var actions = ownDataValue(data, 'actions');
    if (!actions || typeof actions !== 'object') return null;
    var save = ownDataValue(actions, 'save');
    if (!save || typeof save !== 'object') return null;
    var saveValue = ownDataValue(save, 'sub_id');
    var entryId = saveValue === MISSING ? null : positiveIntegerId(saveValue);
    if (!entryId) return null;
    return { formId: formId, entryId: entryId };
  }

  runtime.inspectResponse = inspectResponse;

  function handleSubmitResponse(response, textStatus, jqXHR, callbackFormId) {
      var accepted = inspectResponse(response, callbackFormId);
      if (!accepted) {
        if (typeof window.__converlyNinjaMethod2AfterResponse === 'function') {
          window.__converlyNinjaMethod2AfterResponse(callbackFormId, false, false);
        }
        return;
      }

      var key = '$' + accepted.entryId;
      if (runtime.seenEntryIds[key]) {
        if (typeof window.__converlyNinjaMethod2AfterResponse === 'function') {
          window.__converlyNinjaMethod2AfterResponse(accepted.formId, false, true);
        }
        return;
      }
      runtime.seenEntryIds[key] = true;

      var enriched = false;
      if (typeof window.__converlyNinjaMethod2Emit === 'function') {
        try {
          enriched = window.__converlyNinjaMethod2Emit(accepted.formId, accepted.entryId) === true;
        } catch (e) {
          enriched = false;
        }
      }
      if (!enriched) {
        window.dataLayer.push({ event: 'ninja_forms_submitted', form_id: accepted.formId });
      }
      if (typeof window.__converlyNinjaMethod2AfterResponse === 'function') {
        window.__converlyNinjaMethod2AfterResponse(accepted.formId, true, false);
      }
  }

  function hookNfRadio() {
    if (runtime.installed) return true;
    var radio = typeof nfRadio !== 'undefined' && nfRadio.channel ? nfRadio :
      (typeof Backbone !== 'undefined' && Backbone.Radio ? Backbone.Radio : null);
    if (!radio) return false;
    try {
      radio.channel('forms').on('submit:response', handleSubmitResponse);
      runtime.installed = true;
      return true;
    } catch (e) {
      return false;
    }
  }

  if (!hookNfRadio()) {
    document.addEventListener('DOMContentLoaded', hookNfRadio);
    window.addEventListener('load', hookNfRadio);
  }
})();

Variable

Captures the ID of the form that was submitted, ready to reference in any tag. These are its exact settings.

Name

DLV - Ninja Forms Form ID

Variable Type

Data Layer Variable

Data Layer Variable Name

form_id

Trigger

Fires when the 'ninja_forms_submitted' event is received. Use it as the trigger for any conversion tag. These are its exact settings.

Name

CE - Ninja Forms Submitted

Trigger Type

Custom Event

Event Name

ninja_forms_submitted

Download This Recipe

Get the ready-to-import file and upload it into Google Tag Manager. Everything shown above (the tag, the variable and the trigger) comes pre-built inside.

Built from Converly's own Ninja Forms detection code

Why It's the Best Recipe Out There

It is built from the same detection code we use in our own conversion tracking product, Converly, just adapted to run inside Google Tag Manager.

Checks the result before firing. Ninja Forms sends out the same signal whether a submission succeeds or fails, so most scripts count every failed submission as a conversion. This recipe checks whether Ninja Forms actually accepted it and only fires on a genuine success.
Only counts a submission once Ninja Forms has saved it as an entry, so the form needs its Record Submission action turned on. A form without that action fires nothing.
Has been extensively tested and used to detect thousands of Ninja Forms submissions by our customers.

How to Use the Ninja Forms GTM Recipe

1

Install the Recipe

Download the recipe using the form above, then import it into your Google Tag Manager container. In GTM, head to Admin, choose Import Container, select the downloaded file, and pick the Merge option so it doesn't overwrite anything already in your container. The listener, variable and trigger are added for you automatically.

Importing the recipe container in Google Tag Manager
2

Configure Your Tags

Now that the recipe is installed, you can use it as a Trigger for your conversion tags. Here are two common examples.

Example 1: Google Analytics

Create a GA4 event tag and set the recipe's trigger (called CE - Ninja Forms Submitted) as its firing trigger. Every Ninja Forms submission now sends an event to Google Analytics.

A GA4 event tag using the recipe's trigger

Example 2: Google Ads

Add your Google Ads conversion tag and set it to fire on the Trigger this recipe gives you (called CE - Ninja Forms Submitted). This will send conversions to Google Ads whenever someone successfully submits a Ninja Form on your site.

A Google Ads conversion tag using the recipe's trigger

What This Enables You to Do (and What It Doesn't)

This recipe fires an event in Google Tag Manager whenever a successful Ninja Forms submission is detected. You can use that as a trigger to send conversions to Google Ads, Google Analytics and other tags.

What it does

Sends an Event to GTM

Detects successful Ninja Forms submissions and pushes an event called 'ninja_forms_submitted' into your dataLayer automatically.

Tells You Which Form Fired

Captures the ID of the form that was submitted, so a site with several forms can fire different tags for different forms.

Creates a Trigger in GTM

Gives you a 'Trigger' in Google Tag Manager which you can use as the trigger for any of your conversion tags.

What it doesn't do

Doesn't send the conversion server-side

Everything happens in the visitor's browser, so ad blockers can stop the tag from loading, and browser privacy restrictions can limit tracking to as little as a day.

No Click IDs or Identifiers

It does not capture the Google click ID, Facebook click ID, Meta pixel cookies, IP address or user agent, which ad platforms need for Enhanced Conversions (Google) or a proper Event Match Quality score (Meta).

No Personal Details

The script only fires an event. It does not capture the lead's name, email or phone number, which Google Ads, Meta Ads, etc require for enhanced conversions and a high match score.

No Logging or Notifications

You can test it with Tag Assistant on install, but unless you check every week you have no way of knowing if it breaks (until 2 weeks later when you see 0 conversions in your reports).

The Proper Way to Send Conversions to Google Ads, Meta Ads & More

This recipe is fine if you only send events to Google Analytics, which does not accept personal information or IP addresses. But to get a good Event Match Quality (EMQ) score in Meta, or use Enhanced Conversions in Google, you need to send the data server-side, together with the lead's name and email. That is what Converly does.

A Converly workflow sending Ninja Forms conversions to ad platforms

Here's why Converly is the best way to send conversions to Google Ads, Meta Ads, Microsoft Ads, LinkedIn Ads, etc.

Easy to set up

Converly gives you a simple drag-and-drop builder you can use to build a Conversion Flow. Simply select a trigger (like a Ninja form being submitted) and then select your actions (like sending a conversion to Google Ads). No custom code, no complex configuration.

Sends Data Server-Side

Converly sends conversions directly from its servers to Google, Meta, ChatGPT, etc., so ad blockers and privacy restrictions can't get in the way.

Sends More Data

Converly captures the lead's name, email, and phone, plus the Google and Facebook click IDs, IP address, and user agent, and sends them with every conversion. That enables Enhanced Conversions in Google Ads, or gives you an Event Match Quality (EMQ) score of 9 or 10 in Meta.

Full Conversion Log & Email Notifications

See every conversion, what was captured, what was sent to each platform and whether it was received. Get email alerts so tracking can't break without you knowing.

Supports Multiple Tools & Destinations

Detects submissions across 100+ form, scheduling and chat tools and sends to 20+ destinations including Google Ads, Meta, ChatGPT, LinkedIn, Microsoft and TikTok. So if you ever add a new tool or start advertising on a new platform, it's just a few clicks.

Provides Free Support

Our team has 20 years of experience in conversion tracking and analytics and is here to help whenever you need it. With Google Tag Manager, you're on your own.

Start Your Free Trial

Start a 14-day free trial and track your conversions the proper way. No code, no complicated configuration.

About the author

Aaron Beashel
Aaron Beashel

Aaron is the founder of Converly. With over 15 years of experience in digital marketing and SaaS, he's passionate about helping businesses track and optimise their ad conversions.

Start your free trial

Easily send conversions to your ad platforms and analytics tools.
No code required.