coreyhaines31/marketingskills / skills/conversion-tracking
conversion-tracking
VerifiedWhen the user wants to set up, audit, or fix conversion tracking to Google Ads, Meta, GA4, LinkedIn, and TikTok, or asks why conversions are missing or doubled: GTM, pixels, CAPI, Enhanced Conversions, offline imports. Fire on confirmed success, not the submit click; verify a test conversion arrives; diagnose before changing; offer free paths and disclose partner tools. For tracking plans, see analytics. For attribution, see attribution. For bidding, see ads.
assets/snippets/ninja-forms.js
View on GitHub/*! Ninja Forms conversion detection (MIT)
* Detects: form submission. Pushes dataLayer event: ninja_forms_submitted
* From the conversion-tracking skill in Marketing Skills */
(function () {
'use strict';
var runtime = window.__ctNinjaFormsRuntime;
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.__ctDetectNinjaForms) return;
if (!runtime) {
runtime = { seenEntryIds: Object.create(null), installed: false };
window.__ctNinjaFormsRuntime = runtime;
}
window.__ctDetectNinjaForms = 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.__ctNinjaMethod2AfterResponse === 'function') {
window.__ctNinjaMethod2AfterResponse(callbackFormId, false, false);
}
return;
}
var key = '$' + accepted.entryId;
if (runtime.seenEntryIds[key]) {
if (typeof window.__ctNinjaMethod2AfterResponse === 'function') {
window.__ctNinjaMethod2AfterResponse(accepted.formId, false, true);
}
return;
}
runtime.seenEntryIds[key] = true;
var enriched = false;
if (typeof window.__ctNinjaMethod2Emit === 'function') {
try {
enriched = window.__ctNinjaMethod2Emit(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.__ctNinjaMethod2AfterResponse === 'function') {
window.__ctNinjaMethod2AfterResponse(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);
}
})();
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |