Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.
JavaScript agent-skillsai-agentsai-seoclaudeclaude-codeclaude-code-pluginclaude-skillscodexcontent-marketingconversion-rate-optimizationcopywritingcursoremail-marketinggenerative-engine-optimizationgrowth-marketingmarketingmarketing-automationsaas-marketingseoskill-md
fix(conversion-tracking): let HubSpot retry a conversion whose push fails
Aaron Beashele43fe6f03cef
diff --git a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-hubspot-forms.json b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-hubspot-forms.jsonindex a69aec5..7d0007a 100644--- a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-hubspot-forms.json+++ b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-hubspot-forms.json@@ -29,7 +29,7 @@{"type": "TEMPLATE","key": "html",- "value": "<script>\n(function () {\n if (window.__ctDetectHubspotForms) return;\n window.__ctDetectHubspotForms = true;\n window.dataLayer = window.dataLayer || [];\n\n // Updated-editor conversions are keyed by HubSpot's documented conversion\n // ID. Legacy messages use event-object identity because their documented\n // callback payload has no conversion ID.\n var seenConversions = {};\n var seenLegacyEvents = typeof WeakSet === 'function' ? new WeakSet() : [];\n\n function fireConversion(formId, conversionId, acceptedContext) {\n var id = formId ? String(formId) : '';\n if (typeof conversionId !== 'string' || !conversionId) return false;\n var key = '$' + conversionId;\n if (seenConversions[key]) return false;\n seenConversions[key] = true;\n var handled = false;\n var adapter = window.__ctHubspotMethod2Handler;\n if (typeof adapter === 'function') {\n try {\n handled = adapter({\n formId: id,\n instanceId: acceptedContext && acceptedContext.instanceId || '',\n conversionId: conversionId,\n form: acceptedContext && acceptedContext.form || null\n }) === true;\n } catch (e) {\n handled = false;\n }\n }\n if (!handled) window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: id });\n return true;\n }\n\n function claimLegacyEvent(event) {\n if (!event || typeof event !== 'object') return false;\n if (seenLegacyEvents && typeof seenLegacyEvents.has === 'function') {\n if (seenLegacyEvents.has(event)) return false;\n seenLegacyEvents.add(event);\n return true;\n }\n if (seenLegacyEvents.indexOf(event) !== -1) return false;\n seenLegacyEvents.push(event);\n return true;\n }\n\n // ------------------------------------------------------------\n // Path 1: current forms editor (live-verified)\n // ------------------------------------------------------------\n // HubSpot dispatches this on the .hs-form-frame container and lets it bubble\n // (or on document itself when it cannot find the container), with\n // detail = { formId, instanceId }. It only ever follows a submission\n // HubSpot's server accepted. Listening on document in the capture phase\n // catches both dispatch points and cannot be silenced by page code that\n // stops the event's propagation further down.\n function hubspotFormsApi() {\n return window.HubSpotFormsV4 || window.HubspotFormsV4 || null;\n }\n\n function handleSubmissionSuccess(event) {\n try {\n var detail = event && event.detail;\n if (!detail || typeof detail.formId !== 'string' || !detail.formId) return;\n if (typeof detail.instanceId !== 'string' || !detail.instanceId) return;\n // When HubSpot's forms API is on the page, the event must name a form\n // instance HubSpot actually rendered here. A stray or malformed event\n // with a made-up instance id is ignored.\n var api = hubspotFormsApi();\n if (!api || typeof api.getFormFromEvent !== 'function') return;\n var form = api.getFormFromEvent(event);\n if (!form || typeof form.getFormId !== 'function' ||\n typeof form.getInstanceId !== 'function' ||\n typeof form.getConversionId !== 'function') return;\n if (form.getFormId() !== detail.formId ||\n form.getInstanceId() !== detail.instanceId) return;\n // HubSpot documents getConversionId as valid after successful\n // submission. It is the stable key for event replay and repeated submits.\n fireConversion(detail.formId, form.getConversionId(), { form: form, instanceId: detail.instanceId });\n } catch (e) {\n // Never throw back into HubSpot's own event dispatch.\n }\n }\n\n // ------------------------------------------------------------\n // Path 2: legacy forms editor (ported from the production module)\n // ------------------------------------------------------------\n // Legacy forms announce each lifecycle step with a postMessage of\n // { type: 'hsFormCallback', eventName, id, data }. Only 'onFormSubmitted'\n // means the submission was accepted; 'onFormSubmit' also fires for\n // submissions that then fail validation, so it is ignored.\n //\n // The legacy callback payload documents its form ID but not a safe mapping\n // from a cross-origin source WindowProxy to that ID. Accept only the inline\n // same-window callback; cross-origin iframe messages fail closed until a\n // vendor-supported source/form binding can be verified.\n function isTrustedOrigin(event) {\n return !!(event && event.source === window &&\n event.origin === window.location.origin);\n }\n\n function handleMessage(event) {\n try {\n // Reject an unbound source before even reading its structured-clone data.\n if (!isTrustedOrigin(event)) return;\n var data = event && event.data;\n if (!data || typeof data !== 'object') return;\n if (data.type !== 'hsFormCallback') return;\n if (data.eventName !== 'onFormSubmitted') return;\n var formId = data.id;\n if (typeof formId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(formId)) return;\n if (!claimLegacyEvent(event)) return;\n // The documented legacy callback runs after persistence, but has no\n // stable conversion ID. The local guard only recognizes replay of this\n // exact MessageEvent object; a distinct event object may be a replay or a\n // new conversion, so cross-event deduplication is not claimed.\n var handled = false;\n var adapter = window.__ctHubspotMethod2Handler;\n if (typeof adapter === 'function') {\n try {\n handled = adapter({ formId: formId, legacyEvent: event }) === true;\n } catch (e) { handled = false; }\n }\n if (!handled) window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: formId });\n } catch (e) {\n // Never throw back into the page's own message handling.\n }\n }\n\n document.addEventListener('hs-form-event:on-submission:success', handleSubmissionSuccess, true);\n window.addEventListener('message', handleMessage, false);\n})();\n</script>"+ "value": "<script>\n(function () {\n if (window.__ctDetectHubspotForms) return;\n window.__ctDetectHubspotForms = true;\n window.dataLayer = window.dataLayer || [];\n\n // Updated-editor conversions are keyed by HubSpot's documented conversion\n // ID. Legacy messages use event-object identity because their documented\n // callback payload has no conversion ID.\n var seenConversions = {};\n var seenLegacyEvents = typeof WeakSet === 'function' ? new WeakSet() : [];\n\n function fireConversion(formId, conversionId, acceptedContext) {\n var id = formId ? String(formId) : '';\n if (typeof conversionId !== 'string' || !conversionId) return false;\n var key = '$' + conversionId;\n if (seenConversions[key]) return false;\n seenConversions[key] = true;\n var handled = false;\n var adapter = window.__ctHubspotMethod2Handler;\n if (typeof adapter === 'function') {\n try {\n handled = adapter({\n formId: id,\n instanceId: acceptedContext && acceptedContext.instanceId || '',\n conversionId: conversionId,\n form: acceptedContext && acceptedContext.form || null\n }) === true;\n } catch (e) {\n handled = false;\n }\n }\n // The push is contained, and a conversion whose push failed is\n // forgotten, so a repeat of HubSpot's event can still count it.\n if (!handled) {\n try {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: id });\n } catch (e) {\n delete seenConversions[key];\n return false;\n }\n }\n return true;\n }\n\n function claimLegacyEvent(event) {\n if (!event || typeof event !== 'object') return false;\n if (seenLegacyEvents && typeof seenLegacyEvents.has === 'function') {\n if (seenLegacyEvents.has(event)) return false;\n seenLegacyEvents.add(event);\n return true;\n }\n if (seenLegacyEvents.indexOf(event) !== -1) return false;\n seenLegacyEvents.push(event);\n return true;\n }\n\n function releaseLegacyEvent(event) {\n try {\n if (typeof seenLegacyEvents.delete === 'function') { seenLegacyEvents.delete(event); return; }\n var i = seenLegacyEvents.indexOf(event);\n if (i !== -1) seenLegacyEvents.splice(i, 1);\n } catch (e) {}\n }\n\n // ------------------------------------------------------------\n // Path 1: current forms editor (live-verified)\n // ------------------------------------------------------------\n // HubSpot dispatches this on the .hs-form-frame container and lets it bubble\n // (or on document itself when it cannot find the container), with\n // detail = { formId, instanceId }. It only ever follows a submission\n // HubSpot's server accepted. Listening on document in the capture phase\n // catches both dispatch points and cannot be silenced by page code that\n // stops the event's propagation further down.\n function hubspotFormsApi() {\n return window.HubSpotFormsV4 || window.HubspotFormsV4 || null;\n }\n\n function handleSubmissionSuccess(event) {\n try {\n var detail = event && event.detail;\n if (!detail || typeof detail.formId !== 'string' || !detail.formId) return;\n if (typeof detail.instanceId !== 'string' || !detail.instanceId) return;\n // When HubSpot's forms API is on the page, the event must name a form\n // instance HubSpot actually rendered here. A stray or malformed event\n // with a made-up instance id is ignored.\n var api = hubspotFormsApi();\n if (!api || typeof api.getFormFromEvent !== 'function') return;\n var form = api.getFormFromEvent(event);\n if (!form || typeof form.getFormId !== 'function' ||\n typeof form.getInstanceId !== 'function' ||\n typeof form.getConversionId !== 'function') return;\n if (form.getFormId() !== detail.formId ||\n form.getInstanceId() !== detail.instanceId) return;\n // HubSpot documents getConversionId as valid after successful\n // submission. It is the stable key for event replay and repeated submits.\n fireConversion(detail.formId, form.getConversionId(), { form: form, instanceId: detail.instanceId });\n } catch (e) {\n // Never throw back into HubSpot's own event dispatch.\n }\n }\n\n // ------------------------------------------------------------\n // Path 2: legacy forms editor (ported from the production module)\n // ------------------------------------------------------------\n // Legacy forms announce each lifecycle step with a postMessage of\n // { type: 'hsFormCallback', eventName, id, data }. Only 'onFormSubmitted'\n // means the submission was accepted; 'onFormSubmit' also fires for\n // submissions that then fail validation, so it is ignored.\n //\n // The legacy callback payload documents its form ID but not a safe mapping\n // from a cross-origin source WindowProxy to that ID. Accept only the inline\n // same-window callback; cross-origin iframe messages fail closed until a\n // vendor-supported source/form binding can be verified.\n function isTrustedOrigin(event) {\n return !!(event && event.source === window &&\n event.origin === window.location.origin);\n }\n\n function handleMessage(event) {\n try {\n // Reject an unbound source before even reading its structured-clone data.\n if (!isTrustedOrigin(event)) return;\n var data = event && event.data;\n if (!data || typeof data !== 'object') return;\n if (data.type !== 'hsFormCallback') return;\n if (data.eventName !== 'onFormSubmitted') return;\n var formId = data.id;\n if (typeof formId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(formId)) return;\n if (!claimLegacyEvent(event)) return;\n // The documented legacy callback runs after persistence, but has no\n // stable conversion ID. The local guard only recognizes replay of this\n // exact MessageEvent object; a distinct event object may be a replay or a\n // new conversion, so cross-event deduplication is not claimed.\n var handled = false;\n var adapter = window.__ctHubspotMethod2Handler;\n if (typeof adapter === 'function') {\n try {\n handled = adapter({ formId: formId, legacyEvent: event }) === true;\n } catch (e) { handled = false; }\n }\n if (!handled) {\n try {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: formId });\n } catch (e) {\n releaseLegacyEvent(event);\n }\n }\n } catch (e) {\n // Never throw back into the page's own message handling.\n }\n }\n\n document.addEventListener('hs-form-event:on-submission:success', handleSubmissionSuccess, true);\n window.addEventListener('message', handleMessage, false);\n})();\n</script>"},{"type": "BOOLEAN",diff --git a/skills/conversion-tracking/assets/snippets/hubspot-forms.js b/skills/conversion-tracking/assets/snippets/hubspot-forms.jsindex 269f9ea..671b737 100644--- a/skills/conversion-tracking/assets/snippets/hubspot-forms.js+++ b/skills/conversion-tracking/assets/snippets/hubspot-forms.js@@ -32,7 +32,17 @@handled = false;}}- if (!handled) window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: id });+ // The push is contained, and a conversion whose push failed is+ // forgotten, so a repeat of HubSpot's event can still count it.+ if (!handled) {+ try {+ window.dataLayer = window.dataLayer || [];+ window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: id });+ } catch (e) {+ delete seenConversions[key];+ return false;+ }+ }return true;}@@ -48,6 +58,14 @@return true;}+ function releaseLegacyEvent(event) {+ try {+ if (typeof seenLegacyEvents.delete === 'function') { seenLegacyEvents.delete(event); return; }+ var i = seenLegacyEvents.indexOf(event);+ if (i !== -1) seenLegacyEvents.splice(i, 1);+ } catch (e) {}+ }+// ------------------------------------------------------------// Path 1: current forms editor (live-verified)// ------------------------------------------------------------@@ -124,7 +142,14 @@handled = adapter({ formId: formId, legacyEvent: event }) === true;} catch (e) { handled = false; }}- if (!handled) window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: formId });+ if (!handled) {+ try {+ window.dataLayer = window.dataLayer || [];+ window.dataLayer.push({ event: 'hubspot_form_submitted', form_id: formId });+ } catch (e) {+ releaseLegacyEvent(event);+ }+ }} catch (e) {// Never throw back into the page's own message handling.}diff --git a/skills/conversion-tracking/references/conversion-moments.md b/skills/conversion-tracking/references/conversion-moments.mdindex 888ea10..f3f24c5 100644--- a/skills/conversion-tracking/references/conversion-moments.md+++ b/skills/conversion-tracking/references/conversion-moments.md@@ -33,7 +33,7 @@ Stacking complications: multi-step forms (only final step converts; step events- Legacy v2: `js.hsforms.net/forms/embed/v2.js` + `hbspt.forms.create({portalId, formId})`; renders INLINE mostly. Window message events: `event.data.type === 'hsFormCallback'`, `event.data.eventName` ∈ onFormReady | onFormSubmit | **onFormSubmitted** (use this, post-success) | onFormFailedValidation; `event.data.id` = form GUID.- 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.-- `hubspot-forms.js` covers both and pushes `hubspot_form_submitted` with `form_id` only, never the lead's details. On a current-editor form with the current embed code it counts `hs-form-event:on-submission:success` only when `HubSpotFormsV4.getFormFromEvent` confirms the form and instance, once per conversion ID. On a legacy-editor form with the v2.js embed it counts only an `onFormSubmitted` message posted from the page's own window, never `onFormSubmit`, which also fires for submissions that then fail. Legacy forms in a cross-origin iframe aren't counted, because their message can't be tied to a form. A current-editor form placed with the older `hbspt.forms.create()` code sent the same success event in an earlier probe but wasn't in the 15 Oct checks, and multi-step forms, several forms on one page, redirect after submit and pop-up forms weren't tested. Submissions HubSpot's bot filter files under Spam still count, because HubSpot fires its success event for them too. It was live-checked 15 Oct 2026 on a current-editor form and a legacy-editor form, and HubSpot's Submissions matched the events one for one.+- `hubspot-forms.js` covers both and pushes `hubspot_form_submitted` with `form_id` only, never the lead's details. On a current-editor form with the current embed code it counts `hs-form-event:on-submission:success` only when `HubSpotFormsV4.getFormFromEvent` confirms the form and instance, once per conversion ID. On a legacy-editor form with the v2.js embed it counts only an `onFormSubmitted` message posted from the page's own window, never `onFormSubmit`, which also fires for submissions that then fail. Legacy forms in a cross-origin iframe aren't counted, because their message can't be tied to a form. A current-editor form placed with the older `hbspt.forms.create()` code sent the same success event in an earlier probe but wasn't in the 15 Oct checks, and multi-step forms, several forms on one page, redirect after submit and pop-up forms weren't tested. Submissions HubSpot's bot filter files under Spam still count, because HubSpot fires its success event for them too. It was live-checked 15 Oct 2026 on a current-editor form and a legacy-editor form, and again on 16 Oct after a retry fix, and HubSpot's Submissions matched the events one for one.**Jotform** - iframe (`form.jotform.com/<id>` or script `form.jotform.com/jsform/<id>`). After a successful submission, Jotform's iframe and script embeds send the parent page a `{action: 'submission-completed', formID}` message (live-checked on both, Oct 2026; Jotform support describes it but doesn't formally document it), which `jotform.js` listens for. The full source-code embed sends 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).