Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(conversion-tracking): detect Jotform source-code embeds once the submission is built

Aaron Beashelc352309fb369
diff --git a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-jotform.json b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-jotform.json
index 5b2d6c0..1f3238c 100644
--- a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-jotform.json
+++ b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-jotform.json
@@ -29,7 +29,7 @@
{
"type": "TEMPLATE",
"key": "html",
- "value": "<script>\n(function () {\n if (window.__ctDetectJotform) return;\n window.__ctDetectJotform = true;\n window.dataLayer = window.dataLayer || [];\n\n // Two embed styles, two signals of different strength:\n //\n // 1. IFRAME embed (the default Jotform embed code). The form iframe posts\n // { action: 'submission-completed', formID: '<id>' } to the parent page\n // once Jotform has accepted the submission. This is a real success\n // signal: spam blocks and server-side rejections never send it. Jotform\n // support has confirmed the message exists but says it is not a public\n // API and may change, so re-verify it when Jotform ships embed changes.\n //\n // 2. SOURCE-CODE embed (the form's raw HTML pasted into the page, a\n // <form class=\"jotform-form\"> posting to submit.jotform.com). The page\n // navigates away to Jotform on submit, so there is no success signal\n // this page can observe. This path is SUBMIT-BASED BEST EFFORT: it fires\n // on a submit that passed the browser's own validation and that no other\n // handler cancelled, so a server-side rejection, CAPTCHA failure or spam\n // block still counts. Prefer the iframe embed, or a thank-you page\n // redirect plus thank-you-page.js, when accuracy matters.\n\n var JOTFORM_FORM_ID_RE = /^\\d{10,}$/;\n var JOTFORM_SUBMIT_ACTION_RE = /^https?:\\/\\/submit\\.jotform\\.com\\//i;\n var JOTFORM_ORIGIN_RE = /(^|\\.)jotform(eu)?\\.com$/;\n\n function fireConversion(formId) {\n window.dataLayer.push({\n event: 'jotform_form_submitted',\n form_id: formId || ''\n });\n }\n\n // ---- Path 1: iframe embed, native success message ----\n\n function handleMessage(event) {\n try {\n if (!event || !event.source || event.source === window) return;\n if (!event.origin || typeof event.origin !== 'string') return;\n var hostMatch = /^https:\\/\\/([^/]+)$/.exec(event.origin);\n if (!hostMatch || !JOTFORM_ORIGIN_RE.test(hostMatch[1])) return;\n\n var data = event.data;\n if (typeof data === 'string') {\n if (data.indexOf('submission-completed') === -1) return;\n try { data = JSON.parse(data); } catch (e) { return; }\n }\n if (!data || typeof data !== 'object') return;\n if (data.action !== 'submission-completed') return;\n\n var formId = String(data.formID || data.formId || '');\n if (!JOTFORM_FORM_ID_RE.test(formId)) return;\n\n fireConversion(formId);\n } catch (e) {\n // Never throw back into the page's own message handling.\n }\n }\n\n window.addEventListener('message', handleMessage, false);\n\n // ---- Path 2: source-code embed, submit-based best effort ----\n\n function isJotformInlineForm(formEl) {\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return false;\n var hasClass = formEl.classList && formEl.classList.contains('jotform-form');\n var action = formEl.getAttribute && formEl.getAttribute('action');\n var hasAction = action && JOTFORM_SUBMIT_ACTION_RE.test(action);\n if (!hasClass && !hasAction) return false;\n var id = formEl.id || '';\n if (!JOTFORM_FORM_ID_RE.test(id)) return false;\n return true;\n }\n\n // Bubble phase deliberately. A capture-phase listener runs BEFORE the form's\n // own handlers, so it would count submissions Jotform's validation cancels.\n document.addEventListener('submit', function (event) {\n var formEl = event.target;\n if (!isJotformInlineForm(formEl)) return;\n\n // A submission another handler already cancelled never happened.\n if (event.defaultPrevented) return;\n\n // Constraint validation gate: an invalid form cannot have submitted.\n try {\n if (typeof formEl.checkValidity === 'function' && formEl.checkValidity() === false) return;\n } catch (e) {\n // constraint validation unavailable — fall through\n }\n\n fireConversion(formEl.id);\n }, false);\n})();\n</script>"
+ "value": "<script>\n(function () {\n if (window.__ctDetectJotform) return;\n window.__ctDetectJotform = true;\n window.dataLayer = window.dataLayer || [];\n\n // Two embed styles, two signals of different strength:\n //\n // 1. IFRAME embed (the default Jotform embed code). Jotform doesn't\n // document any submission message from its embed. In testing a classic\n // form, nothing reached the host page after submit, and Jotform's own\n // embed script handles that message only for Card-layout forms. A form\n // showing an on-page thank-you message wasn't tested. So for iframe\n // embeds, set the form's thank-you to redirect to a page on this site\n // and use thank-you-page.js there. Use one method or the other for a\n // given form, never both. The message listener below is kept only as a\n // fallback. It fires only for a message from a Jotform iframe on this\n // page that shows the same form ID, and at most once per form.\n //\n // 2. SOURCE-CODE embed (the form's raw HTML pasted into the page, a\n // <form class=\"jotform-form\"> posting to submit.jotform.com). Jotform\n // validates in its own handler, cancels every native submit and then\n // submits the form itself, so a listener that skips cancelled submits\n // never fires. This path marks the form on submit (capture phase) and\n // fires when the browser builds the form's data for the real send (the\n // formdata event), once per form. In the tested flow (a classic form\n // with Jotform's own validation on), a submit Jotform rejects builds no\n // data and doesn't count. A server-side rejection or spam block after\n // the send still counts.\n // Known limits. If another script builds the form's data after a\n // rejected attempt, that counts. Payment, multi-page, save-and-continue\n // and captcha forms haven't been tested, so prefer the thank-you\n // redirect plus thank-you-page.js there.\n\n var JOTFORM_FORM_ID_RE = /^\\d{10,}$/;\n var JOTFORM_SUBMIT_ACTION_RE = /^https?:\\/\\/submit\\.jotform\\.com\\//i;\n var JOTFORM_ORIGIN_RE = /(^|\\.)jotform(eu)?\\.com$/;\n\n // Form IDs already counted on this page, shared by both paths.\n var fired = {};\n\n function fireConversion(formId) {\n window.dataLayer.push({\n event: 'jotform_form_submitted',\n form_id: formId || ''\n });\n }\n\n // ---- Path 1: iframe embed, message listener kept as a fallback ----\n\n // True when the iframe's src is a Jotform form host and one of its path\n // segments is exactly this form ID.\n function frameShowsForm(frame, formId) {\n var src = (frame.getAttribute && frame.getAttribute('src')) || '';\n if (!src) return false;\n var url;\n try {\n url = new URL(src, window.location.href);\n } catch (e) {\n return false;\n }\n if (!JOTFORM_ORIGIN_RE.test(url.hostname.toLowerCase())) return false;\n var parts = url.pathname.split('/');\n for (var i = 0; i < parts.length; i++) {\n if (parts[i] === formId) return true;\n }\n return false;\n }\n\n // Frames are looked up when the message arrives, so an iframe inserted\n // after this snippet loaded still binds.\n function sourceIsFormFrame(source, formId) {\n var frames = document.getElementsByTagName('iframe');\n for (var i = 0; i < frames.length; i++) {\n var win = null;\n try { win = frames[i].contentWindow; } catch (e) { win = null; }\n if (win && win === source && frameShowsForm(frames[i], formId)) return true;\n }\n return false;\n }\n\n function handleMessage(event) {\n try {\n if (!event || !event.source || event.source === window) return;\n if (!event.origin || typeof event.origin !== 'string') return;\n var hostMatch = /^https:\\/\\/([^/]+)$/.exec(event.origin);\n if (!hostMatch || !JOTFORM_ORIGIN_RE.test(hostMatch[1])) return;\n\n var data = event.data;\n if (typeof data === 'string') {\n if (data.indexOf('submission-completed') === -1) return;\n try { data = JSON.parse(data); } catch (e) { return; }\n }\n if (!data || typeof data !== 'object') return;\n if (data.action !== 'submission-completed') return;\n\n var formId = String(data.formID || data.formId || '');\n if (!JOTFORM_FORM_ID_RE.test(formId)) return;\n if (fired[formId]) return;\n if (!sourceIsFormFrame(event.source, formId)) return;\n\n fireConversion(formId);\n fired[formId] = true;\n } catch (e) {\n // Never throw back into the page's own message handling.\n }\n }\n\n window.addEventListener('message', handleMessage, false);\n\n // ---- Path 2: source-code embed, submission built ----\n\n function isJotformInlineForm(formEl) {\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return false;\n var hasClass = formEl.classList && formEl.classList.contains('jotform-form');\n var action = formEl.getAttribute && formEl.getAttribute('action');\n var hasAction = action && JOTFORM_SUBMIT_ACTION_RE.test(action);\n if (!hasClass && !hasAction) return false;\n var id = formEl.id || '';\n if (!JOTFORM_FORM_ID_RE.test(id)) return false;\n return true;\n }\n\n var attempted = {};\n\n // Capture phase, so this sees the submit before Jotform cancels it.\n document.addEventListener('submit', function (event) {\n var formEl = event.target;\n if (!isJotformInlineForm(formEl)) return;\n attempted[formEl.id] = true;\n }, true);\n\n // Chromium can raise formdata twice for one submission, so the fired\n // check is what keeps this to one conversion per form. The form is marked\n // fired only after the push succeeds.\n document.addEventListener('formdata', function (event) {\n try {\n var formEl = event.target;\n if (!isJotformInlineForm(formEl)) return;\n if (!attempted[formEl.id] || fired[formEl.id]) return;\n fireConversion(formEl.id);\n fired[formEl.id] = true;\n } catch (e) {\n // Never throw back into Jotform's own submit handling.\n }\n }, true);\n})();\n</script>"
},
{
"type": "BOOLEAN",
diff --git a/skills/conversion-tracking/assets/snippets/README.md b/skills/conversion-tracking/assets/snippets/README.md
index f386447..189a9c7 100644
--- a/skills/conversion-tracking/assets/snippets/README.md
+++ b/skills/conversion-tracking/assets/snippets/README.md
@@ -68,7 +68,7 @@ Not every snippet fires on a confirmed success. Some tools expose no success sig
| `forminator.js` | Native success event | jQuery `forminator:form:submit:success` |
| `framer-forms.js` | Submit/click-based best effort | `submit` on a Framer form, not `defaultPrevented`, passing `checkValidity()` |
| `gravity-forms.js` | Native success event | jQuery `gform_confirmation_loaded`, with a MutationObserver fallback on the confirmation message |
-| `jotform.js` | Native success event (iframe embed); submit/click-based best effort (source-code embed) | iframe: `{action: 'submission-completed', formID}` postMessage from jotform.com (works, but Jotform calls it unofficial); source embed: `submit` passing `checkValidity()` |
+| `jotform.js` | Submission built (`submit` then `formdata`, once per form) for the source-code embed; for the iframe embed, thank-you redirect recommended; message listener kept as fallback | source embed: a capture-phase `submit` marks the form, then the `formdata` event fires once per form ID; iframe: `{action: 'submission-completed', formID}` postMessage, which Jotform doesn't document, counted only from a Jotform iframe on the page showing that form ID, once per form ID |
| `ninja-forms.js` | Native success event | Radio `forms` channel `submit:response` with no errors, with a MutationObserver fallback |
| `tally.js` | Native success event | `Tally.FormSubmitted` postMessage from tally.so |
| `typeform.js` | Native success event | `form-submit` postMessage from typeform.com carrying `formId` and `responseId` |
@@ -82,6 +82,8 @@ Not every snippet fires on a confirmed success. Some tools expose no success sig
| `thank-you-page.js` | DOM-inferred after submit | page path starts with a configured confirmation path |
| `generic-ajax-form.js` | DOM-inferred after submit | a success message appears within 7s of submit with no visible validation error |
+**Jotform note.** Use one method per form, either the thank-you redirect with `thank-you-page.js` or this snippet, never both. The source-code path was tested on a classic form, where a submit Jotform's validation rejects doesn't fire. Payment, multi-page, save-and-continue and captcha forms are untested, so prefer the redirect for those.
+
**PII note for `phone-click.js`.** It pushes the clicked number (`phone_number`) and the link text (`link_text`) to the dataLayer, and any tag can forward those. That is fine for a business's own published numbers. If a page shows visitor-specific numbers (an account page, a "your rep" number pulled from a CRM), remove those two fields from the push before installing.
## Verifying a snippet works
diff --git a/skills/conversion-tracking/assets/snippets/jotform.js b/skills/conversion-tracking/assets/snippets/jotform.js
index 7b31a7b..1ab2ced 100644
--- a/skills/conversion-tracking/assets/snippets/jotform.js
+++ b/skills/conversion-tracking/assets/snippets/jotform.js
@@ -8,26 +8,39 @@
// Two embed styles, two signals of different strength:
//
- // 1. IFRAME embed (the default Jotform embed code). The form iframe posts
- // { action: 'submission-completed', formID: '<id>' } to the parent page
- // once Jotform has accepted the submission. This is a real success
- // signal: spam blocks and server-side rejections never send it. Jotform
- // support has confirmed the message exists but says it is not a public
- // API and may change, so re-verify it when Jotform ships embed changes.
+ // 1. IFRAME embed (the default Jotform embed code). Jotform doesn't
+ // document any submission message from its embed. In testing a classic
+ // form, nothing reached the host page after submit, and Jotform's own
+ // embed script handles that message only for Card-layout forms. A form
+ // showing an on-page thank-you message wasn't tested. So for iframe
+ // embeds, set the form's thank-you to redirect to a page on this site
+ // and use thank-you-page.js there. Use one method or the other for a
+ // given form, never both. The message listener below is kept only as a
+ // fallback. It fires only for a message from a Jotform iframe on this
+ // page that shows the same form ID, and at most once per form.
//
// 2. SOURCE-CODE embed (the form's raw HTML pasted into the page, a
- // <form class="jotform-form"> posting to submit.jotform.com). The page
- // navigates away to Jotform on submit, so there is no success signal
- // this page can observe. This path is SUBMIT-BASED BEST EFFORT: it fires
- // on a submit that passed the browser's own validation and that no other
- // handler cancelled, so a server-side rejection, CAPTCHA failure or spam
- // block still counts. Prefer the iframe embed, or a thank-you page
- // redirect plus thank-you-page.js, when accuracy matters.
+ // <form class="jotform-form"> posting to submit.jotform.com). Jotform
+ // validates in its own handler, cancels every native submit and then
+ // submits the form itself, so a listener that skips cancelled submits
+ // never fires. This path marks the form on submit (capture phase) and
+ // fires when the browser builds the form's data for the real send (the
+ // formdata event), once per form. In the tested flow (a classic form
+ // with Jotform's own validation on), a submit Jotform rejects builds no
+ // data and doesn't count. A server-side rejection or spam block after
+ // the send still counts.
+ // Known limits. If another script builds the form's data after a
+ // rejected attempt, that counts. Payment, multi-page, save-and-continue
+ // and captcha forms haven't been tested, so prefer the thank-you
+ // redirect plus thank-you-page.js there.
var JOTFORM_FORM_ID_RE = /^\d{10,}$/;
var JOTFORM_SUBMIT_ACTION_RE = /^https?:\/\/submit\.jotform\.com\//i;
var JOTFORM_ORIGIN_RE = /(^|\.)jotform(eu)?\.com$/;
+ // Form IDs already counted on this page, shared by both paths.
+ var fired = {};
+
function fireConversion(formId) {
window.dataLayer.push({
event: 'jotform_form_submitted',
@@ -35,7 +48,38 @@
});
}
- // ---- Path 1: iframe embed, native success message ----
+ // ---- Path 1: iframe embed, message listener kept as a fallback ----
+
+ // True when the iframe's src is a Jotform form host and one of its path
+ // segments is exactly this form ID.
+ function frameShowsForm(frame, formId) {
+ var src = (frame.getAttribute && frame.getAttribute('src')) || '';
+ if (!src) return false;
+ var url;
+ try {
+ url = new URL(src, window.location.href);
+ } catch (e) {
+ return false;
+ }
+ if (!JOTFORM_ORIGIN_RE.test(url.hostname.toLowerCase())) return false;
+ var parts = url.pathname.split('/');
+ for (var i = 0; i < parts.length; i++) {
+ if (parts[i] === formId) return true;
+ }
+ return false;
+ }
+
+ // Frames are looked up when the message arrives, so an iframe inserted
+ // after this snippet loaded still binds.
+ function sourceIsFormFrame(source, formId) {
+ var frames = document.getElementsByTagName('iframe');
+ for (var i = 0; i < frames.length; i++) {
+ var win = null;
+ try { win = frames[i].contentWindow; } catch (e) { win = null; }
+ if (win && win === source && frameShowsForm(frames[i], formId)) return true;
+ }
+ return false;
+ }
function handleMessage(event) {
try {
@@ -54,8 +98,11 @@
var formId = String(data.formID || data.formId || '');
if (!JOTFORM_FORM_ID_RE.test(formId)) return;
+ if (fired[formId]) return;
+ if (!sourceIsFormFrame(event.source, formId)) return;
fireConversion(formId);
+ fired[formId] = true;
} catch (e) {
// Never throw back into the page's own message handling.
}
@@ -63,7 +110,7 @@
window.addEventListener('message', handleMessage, false);
- // ---- Path 2: source-code embed, submit-based best effort ----
+ // ---- Path 2: source-code embed, submission built ----
function isJotformInlineForm(formEl) {
if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return false;
@@ -76,22 +123,27 @@
return true;
}
- // Bubble phase deliberately. A capture-phase listener runs BEFORE the form's
- // own handlers, so it would count submissions Jotform's validation cancels.
+ var attempted = {};
+
+ // Capture phase, so this sees the submit before Jotform cancels it.
document.addEventListener('submit', function (event) {
var formEl = event.target;
if (!isJotformInlineForm(formEl)) return;
+ attempted[formEl.id] = true;
+ }, true);
- // A submission another handler already cancelled never happened.
- if (event.defaultPrevented) return;
-
- // Constraint validation gate: an invalid form cannot have submitted.
+ // Chromium can raise formdata twice for one submission, so the fired
+ // check is what keeps this to one conversion per form. The form is marked
+ // fired only after the push succeeds.
+ document.addEventListener('formdata', function (event) {
try {
- if (typeof formEl.checkValidity === 'function' && formEl.checkValidity() === false) return;
+ var formEl = event.target;
+ if (!isJotformInlineForm(formEl)) return;
+ if (!attempted[formEl.id] || fired[formEl.id]) return;
+ fireConversion(formEl.id);
+ fired[formEl.id] = true;
} catch (e) {
- // constraint validation unavailable — fall through
+ // Never throw back into Jotform's own submit handling.
}
-
- fireConversion(formEl.id);
- }, false);
+ }, true);
})();
diff --git a/skills/conversion-tracking/references/conversion-moments.md b/skills/conversion-tracking/references/conversion-moments.md
index d818ada..8666d5a 100644
--- a/skills/conversion-tracking/references/conversion-moments.md
+++ b/skills/conversion-tracking/references/conversion-moments.md
@@ -30,7 +30,7 @@ Stacking complications: multi-step forms (only final step converts; step events
- New v4: `js.hsforms.net/forms/embed/<portalId>.js` + `<div class="hs-form-frame" data-form-id=...>`; renders in IFRAME. Real DOM events on window: `hs-form-event:on-ready`, **`hs-form-event:on-submission:success`**, `:failed`, `:on-interaction:navigate`; `event.detail.formId`; helper `window.HubSpotFormsV4` (has getConversionId()).
- Note: `js.hs-scripts.com/<portalId>.js` is the general HubSpot tracking script - presence ≠ ad conversion tracking.
-**Jotform** - iframe (`form.jotform.com/<id>` or script `form.jotform.com/jsform/<id>`). Posts message to parent on submit; the Thank You → redirect setting breaks out of the iframe, navigates TOP window to your URL, and can append field values + unique {id} param - redirect is the most robust method. Tags pasted inside the Jotform builder fire on jotform.com's domain (wrong context).
+**Jotform** - iframe (`form.jotform.com/<id>` or script `form.jotform.com/jsform/<id>`). Jotform documents no submit message to the parent page, and in testing a classic form sent none; the Thank You → redirect setting breaks out of the iframe, navigates TOP window to your URL, and can append field values + unique {id} param - redirect is the most robust method. Tags pasted inside the Jotform builder fire on jotform.com's domain (wrong context).
**Contact Form 7 (WP)** - always AJAX. Bubbling DOM events on document (plain addEventListener works): **`wpcf7mailsent`** (success - use this), wpcf7submit, wpcf7invalid, wpcf7spam, wpcf7mailfailed. `event.detail` includes contactFormId, inputs (name/value array - usable for enhanced conversions).