Skip to content

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

fix(conversion-tracking): contain push errors in the new listeners and fix Squarespace save ownership

Aaron Beashel8b3eafc03eb8
diff --git a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-acuity-scheduling.json b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-acuity-scheduling.json
index 1fc1182..0c839c3 100644
--- a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-acuity-scheduling.json
+++ b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-acuity-scheduling.json
@@ -29,7 +29,7 @@
{
"type": "TEMPLATE",
"key": "html",
- "value": "<script>\n(function () {\n 'use strict';\n if (window.__ctDetectAcuity) return;\n window.__ctDetectAcuity = true;\n window.dataLayer = window.dataLayer || [];\n\n // Acuity's booking form gives the page no booking event of its own. This\n // listener pairs with a short snippet the site owner pastes into Acuity's\n // custom conversion tracking field (acuity-snippet.html). Acuity runs that\n // snippet after each booking inside its conversion tracking frame, on\n // https://conversion-tracking-sandbox.acuityinnovation.com, two frames below\n // this page, and it posts the booking's ID to this page only (it names the\n // page's own address as the target). The frame is a grandchild, so it can't\n // be matched to an iframe on the page; it must come from Acuity's origin and\n // sit inside this page's own frame tree.\n var ORIGIN_PATTERN = /^https:\\/\\/([a-z0-9-]+\\.)+acuityinnovation\\.com$/;\n var ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;\n var KIND_PATTERN = /^[A-Za-z_-]{1,32}$/;\n var PRICE_PATTERN = /^[0-9]{1,9}(\\.[0-9]{1,4})?$/;\n var seenIds = [];\n\n function isPlainObject(value) {\n return !!value && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';\n }\n\n // A frame inside this page has this page as its top window. A popup or a\n // window this page opened does not.\n function insideThisPage(source) {\n try { return source.top === window; } catch (e) { return false; }\n }\n\n function idSeen(id) {\n for (var i = 0; i < seenIds.length; i++) if (seenIds[i] === id) return true;\n seenIds.push(id);\n return false;\n }\n\n function cleanText(value, max) {\n if (typeof value !== 'string') return '';\n value = value.replace(/[\\u0000-\\u001f\\u007f]/g, ' ').replace(/^\\s+|\\s+$/g, '');\n return value.length > max ? value.slice(0, max) : value;\n }\n\n function onMessage(message) {\n if (!message || !message.source || message.source === window) return;\n if (typeof message.origin !== 'string' || !ORIGIN_PATTERN.test(message.origin)) return;\n if (!insideThisPage(message.source)) return;\n var data = message.data;\n if (!isPlainObject(data) || data.source !== 'gtm-acuity-booking' || data.version !== 1) return;\n if (typeof data.appointment_id !== 'string' || !ID_PATTERN.test(data.appointment_id)) return;\n if (idSeen(data.appointment_id)) return;\n var push = { event: 'acuity_appointment_scheduled', appointment_id: data.appointment_id };\n if (typeof data.kind === 'string' && KIND_PATTERN.test(data.kind)) push.acuity_type = data.kind;\n var type = cleanText(data.appointment_type, 200);\n if (type) push.appointment_type = type;\n if (typeof data.price === 'string' && PRICE_PATTERN.test(data.price)) push.price = data.price;\n window.dataLayer.push(push);\n }\n\n window.addEventListener('message', onMessage, false);\n})();\n</script>"
+ "value": "<script>\n(function () {\n 'use strict';\n if (window.__ctDetectAcuity) return;\n window.__ctDetectAcuity = true;\n window.dataLayer = window.dataLayer || [];\n\n // Acuity's booking form gives the page no booking event of its own. This\n // listener pairs with a short snippet the site owner pastes into Acuity's\n // custom conversion tracking field (the field snippet that comes with this\n // listener). Acuity runs that snippet after each booking inside its\n // conversion tracking frame, on\n // https://conversion-tracking-sandbox.acuityinnovation.com, two frames below\n // this page, and it posts the booking's ID to this page only (it names the\n // page's own address as the target). The frame is a grandchild, so it can't\n // be matched to an iframe on the page; it must come from Acuity's origin and\n // sit inside this page's own frame tree.\n var ORIGIN_PATTERN = /^https:\\/\\/([a-z0-9-]+\\.)+acuityinnovation\\.com$/;\n var ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;\n var KIND_PATTERN = /^[A-Za-z_-]{1,32}$/;\n var PRICE_PATTERN = /^[0-9]{1,9}(\\.[0-9]{1,4})?$/;\n var seenIds = [];\n\n function isPlainObject(value) {\n return !!value && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';\n }\n\n // A frame inside this page has this page as its top window. A popup or a\n // window this page opened does not.\n function insideThisPage(source) {\n try { return source.top === window; } catch (e) { return false; }\n }\n\n function idSeen(id) {\n for (var i = 0; i < seenIds.length; i++) if (seenIds[i] === id) return true;\n return false;\n }\n\n function cleanText(value, max) {\n if (typeof value !== 'string') return '';\n value = value.replace(/[\\u0000-\\u001f\\u007f]/g, ' ').replace(/^\\s+|\\s+$/g, '');\n return value.length > max ? value.slice(0, max) : value;\n }\n\n function onMessage(message) {\n if (!message || !message.source || message.source === window) return;\n if (typeof message.origin !== 'string' || !ORIGIN_PATTERN.test(message.origin)) return;\n if (!insideThisPage(message.source)) return;\n var data = message.data;\n if (!isPlainObject(data) || data.source !== 'gtm-acuity-booking' || data.version !== 1) return;\n if (typeof data.appointment_id !== 'string' || !ID_PATTERN.test(data.appointment_id)) return;\n if (idSeen(data.appointment_id)) return;\n var push = { event: 'acuity_appointment_scheduled', appointment_id: data.appointment_id };\n if (typeof data.kind === 'string' && KIND_PATTERN.test(data.kind)) push.acuity_type = data.kind;\n var type = cleanText(data.appointment_type, 200);\n if (type) push.appointment_type = type;\n if (typeof data.price === 'string' && PRICE_PATTERN.test(data.price)) push.price = data.price;\n // The ID is remembered only once the push went through, so a dataLayer\n // that throws (a broken wrapper) never escapes into the page, and a\n // repeat of the same message can still be counted.\n try {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push(push);\n } catch (e) {\n return;\n }\n seenIds.push(data.appointment_id);\n }\n\n window.addEventListener('message', onMessage, false);\n})();\n</script>"
},
{
"type": "BOOLEAN",
diff --git a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-cal-com.json b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-cal-com.json
index c88193f..cc73b91 100644
--- a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-cal-com.json
+++ b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-cal-com.json
@@ -29,7 +29,7 @@
{
"type": "TEMPLATE",
"key": "html",
- "value": "<script>\n(function () {\n 'use strict';\n if (window.__ctDetectCalcom) return;\n window.__ctDetectCalcom = true;\n window.dataLayer = window.dataLayer || [];\n\n // Cal.com's embed code (inline, popup and floating button) renders an iframe\n // from https://app.cal.com; a booking page pasted in as a plain iframe loads\n // from https://cal.com. Each posts from its own origin, so a frame is only\n // trusted for the origin its address starts with. The CSS selectors do that\n // prefix match so this listener never reads an iframe's address: Cal.com\n // prefill parameters can carry a name or email.\n var CAL_FRAMES = [\n { selector: 'iframe[src^=\"https://app.cal.com/\"]', origin: 'https://app.cal.com' },\n { selector: 'iframe[src^=\"https://cal.com/\"]', origin: 'https://cal.com' }\n ];\n var UID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;\n var STATUS_PATTERN = /^[A-Z_]{1,32}$/;\n var trackedFrames = [];\n var seenUids = [];\n\n function trackFrame(frame, origin) {\n if (!frame || !frame.contentWindow) return;\n for (var i = 0; i < trackedFrames.length; i++) {\n if (trackedFrames[i].frame === frame) {\n trackedFrames[i].origin = origin;\n return;\n }\n }\n trackedFrames.push({ frame: frame, origin: origin });\n }\n\n function scan(root) {\n if (!root || typeof root.querySelectorAll !== 'function') return;\n for (var k = 0; k < CAL_FRAMES.length; k++) {\n var frames;\n try { frames = root.querySelectorAll(CAL_FRAMES[k].selector); } catch (e) { continue; }\n for (var i = 0; i < frames.length; i++) trackFrame(frames[i], CAL_FRAMES[k].origin);\n }\n }\n\n // The message must come from a Cal.com frame that is on this page right now,\n // and from the origin that frame was loaded from. Cal.com re-creates popup\n // frames, so a frame seen earlier is re-checked live.\n function sourceIsTracked(message) {\n for (var i = 0; i < trackedFrames.length; i++) {\n var item = trackedFrames[i];\n try {\n if (item.frame.isConnected === true && item.frame.contentWindow === message.source && item.origin === message.origin) return true;\n } catch (e) { /* replaced or inaccessible frames fail closed */ }\n }\n return false;\n }\n\n function isCalOrigin(origin) {\n for (var k = 0; k < CAL_FRAMES.length; k++) if (CAL_FRAMES[k].origin === origin) return true;\n return false;\n }\n\n function isPlainObject(value) {\n return !!value && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';\n }\n\n function uidSeen(uid) {\n for (var i = 0; i < seenUids.length; i++) if (seenUids[i] === uid) return true;\n seenUids.push(uid);\n return false;\n }\n\n function onMessage(message) {\n if (!message || !message.source || message.source === window) return;\n if (!isCalOrigin(message.origin)) return;\n // A frame added since the last scan (a popup that just opened) is picked up here.\n if (!sourceIsTracked(message)) {\n scan(document);\n if (!sourceIsTracked(message)) return;\n }\n var data = message.data;\n if (typeof data === 'string') {\n try { data = JSON.parse(data); } catch (e) { return; }\n }\n // Only a completed new booking. Reschedules (rescheduleBookingSuccessfulV2)\n // and the deprecated V1 events, which carry the attendee's details, never match.\n if (!isPlainObject(data) || data.originator !== 'CAL' || data.type !== 'bookingSuccessfulV2') return;\n var booking = data.data;\n if (!isPlainObject(booking) || typeof booking.uid !== 'string' || !UID_PATTERN.test(booking.uid)) return;\n if (uidSeen(booking.uid)) return;\n // Only these three values are copied. The booking title names the attendee,\n // so it and every other field are left behind.\n var push = { event: 'calcom_booking_successful', booking_uid: booking.uid };\n if (typeof booking.eventTypeId === 'number' && isFinite(booking.eventTypeId)) push.event_type_id = String(booking.eventTypeId);\n if (typeof booking.status === 'string' && STATUS_PATTERN.test(booking.status)) push.booking_status = booking.status;\n window.dataLayer.push(push);\n }\n\n function start() {\n if (typeof document === 'undefined') return;\n scan(document);\n if (typeof MutationObserver === 'undefined' || !document.documentElement) return;\n try {\n var observer = new MutationObserver(function (mutations) {\n for (var i = 0; i < mutations.length; i++) {\n var added = mutations[i].addedNodes || [];\n for (var j = 0; j < added.length; j++) {\n var node = added[j];\n if (node && node.tagName === 'IFRAME') scan(document);\n else scan(node);\n }\n if (mutations[i].type === 'attributes' && mutations[i].attributeName === 'src') scan(document);\n }\n });\n observer.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ['src'] });\n } catch (e) {}\n }\n\n if (typeof document !== 'undefined') {\n if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, false);\n else start();\n }\n window.addEventListener('message', onMessage, false);\n})();\n</script>"
+ "value": "<script>\n(function () {\n 'use strict';\n if (window.__ctDetectCalcom) return;\n window.__ctDetectCalcom = true;\n window.dataLayer = window.dataLayer || [];\n\n // Cal.com's embed code (inline, popup and floating button) renders an iframe\n // from https://app.cal.com; a booking page pasted in as a plain iframe loads\n // from https://cal.com. Each posts from its own origin, so a frame is only\n // trusted for the origin its address starts with. The CSS selectors do that\n // prefix match so this listener never reads an iframe's address, because\n // Cal.com prefill parameters can carry a name or email.\n var CAL_FRAMES = [\n { selector: 'iframe[src^=\"https://app.cal.com/\"]', origin: 'https://app.cal.com' },\n { selector: 'iframe[src^=\"https://cal.com/\"]', origin: 'https://cal.com' }\n ];\n var UID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;\n var STATUS_PATTERN = /^[A-Z_]{1,32}$/;\n var trackedFrames = [];\n var seenUids = [];\n\n function trackFrame(frame, origin) {\n if (!frame || !frame.contentWindow) return;\n for (var i = 0; i < trackedFrames.length; i++) {\n if (trackedFrames[i].frame === frame) {\n trackedFrames[i].origin = origin;\n return;\n }\n }\n trackedFrames.push({ frame: frame, origin: origin });\n }\n\n function scan(root) {\n if (!root || typeof root.querySelectorAll !== 'function') return;\n for (var k = 0; k < CAL_FRAMES.length; k++) {\n var frames;\n try { frames = root.querySelectorAll(CAL_FRAMES[k].selector); } catch (e) { continue; }\n for (var i = 0; i < frames.length; i++) trackFrame(frames[i], CAL_FRAMES[k].origin);\n }\n }\n\n // The message must come from a Cal.com frame that is on this page right now,\n // and from the origin that frame was loaded from. Cal.com re-creates popup\n // frames, so a frame seen earlier is re-checked live.\n function sourceIsTracked(message) {\n for (var i = 0; i < trackedFrames.length; i++) {\n var item = trackedFrames[i];\n try {\n if (item.frame.isConnected === true && item.frame.contentWindow === message.source && item.origin === message.origin) return true;\n } catch (e) { /* replaced or inaccessible frames fail closed */ }\n }\n return false;\n }\n\n function isCalOrigin(origin) {\n for (var k = 0; k < CAL_FRAMES.length; k++) if (CAL_FRAMES[k].origin === origin) return true;\n return false;\n }\n\n function isPlainObject(value) {\n return !!value && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';\n }\n\n function uidSeen(uid) {\n for (var i = 0; i < seenUids.length; i++) if (seenUids[i] === uid) return true;\n return false;\n }\n\n function onMessage(message) {\n if (!message || !message.source || message.source === window) return;\n if (!isCalOrigin(message.origin)) return;\n // A frame added since the last scan (a popup that just opened) is picked up here.\n if (!sourceIsTracked(message)) {\n scan(document);\n if (!sourceIsTracked(message)) return;\n }\n var data = message.data;\n if (typeof data === 'string') {\n try { data = JSON.parse(data); } catch (e) { return; }\n }\n // Only a completed new booking. Reschedules (rescheduleBookingSuccessfulV2)\n // and the deprecated V1 events, which carry the attendee's details, never match.\n if (!isPlainObject(data) || data.originator !== 'CAL' || data.type !== 'bookingSuccessfulV2') return;\n var booking = data.data;\n if (!isPlainObject(booking) || typeof booking.uid !== 'string' || !UID_PATTERN.test(booking.uid)) return;\n if (uidSeen(booking.uid)) return;\n // Only these three values are copied. The booking title names the attendee,\n // so it and every other field are left behind.\n var push = { event: 'calcom_booking_successful', booking_uid: booking.uid };\n if (typeof booking.eventTypeId === 'number' && isFinite(booking.eventTypeId)) push.event_type_id = String(booking.eventTypeId);\n if (typeof booking.status === 'string' && STATUS_PATTERN.test(booking.status)) push.booking_status = booking.status;\n // The ID is remembered only once the push went through, so a dataLayer\n // that throws (a broken wrapper) never escapes into the page, and a\n // repeat of the same message can still be counted.\n try {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push(push);\n } catch (e) {\n return;\n }\n seenUids.push(booking.uid);\n }\n\n function start() {\n if (typeof document === 'undefined') return;\n scan(document);\n if (typeof MutationObserver === 'undefined' || !document.documentElement) return;\n try {\n var observer = new MutationObserver(function (mutations) {\n for (var i = 0; i < mutations.length; i++) {\n var added = mutations[i].addedNodes || [];\n for (var j = 0; j < added.length; j++) {\n var node = added[j];\n if (node && node.tagName === 'IFRAME') scan(document);\n else scan(node);\n }\n if (mutations[i].type === 'attributes' && mutations[i].attributeName === 'src') scan(document);\n }\n });\n observer.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ['src'] });\n } catch (e) {}\n }\n\n if (typeof document !== 'undefined') {\n if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start, false);\n else start();\n }\n window.addEventListener('message', onMessage, false);\n})();\n</script>"
},
{
"type": "BOOLEAN",
diff --git a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-squarespace-forms.json b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-squarespace-forms.json
index bbf1c3e..7cf1239 100644
--- a/skills/conversion-tracking/assets/gtm-recipes/detect/detect-squarespace-forms.json
+++ b/skills/conversion-tracking/assets/gtm-recipes/detect/detect-squarespace-forms.json
@@ -29,7 +29,7 @@
{
"type": "TEMPLATE",
"key": "html",
- "value": "<script>\n(function () {\n 'use strict';\n // Install once, even if GTM injects this tag more than once on the page.\n if (window.__ctDetectSquarespaceForms) return;\n window.__ctDetectSquarespaceForms = true;\n window.dataLayer = window.dataLayer || [];\n\n // Ported from a production module (cdn/v1/modules/\n // squarespace-forms.js, measured against a live Squarespace 7.1 site in\n // October 2026). Event only: it never reads what the visitor typed.\n //\n // What Squarespace does:\n // - The Form block renders a React form in the page (no iframe):\n // .sqs-block-form ... form.react-form-contents[novalidate]. It mounts\n // after DOMContentLoaded, so submits are caught on document.\n // - There is no check in the browser. Every submit posts\n // /api/form/FormSubmissionKey then /api/form/SaveFormSubmission, and the\n // server decides: 204 on success, 400 for a field error, 401 for a failed\n // reCAPTCHA. A submit is never a conversion on its own.\n // - On success it marks the form (react-form-contents--submitted), shows a\n // .form-submission-text message and gives the button the class\n // \"success\". A form set to redirect leaves for its page about 1.3\n // seconds after the 204, before that message renders in Chromium.\n // - The Newsletter block posts to the same SaveFormSubmission address, and\n // that request carries no form identity, so a save only counts for a\n // form that provably owns it.\n //\n // It fires once per form, on the first confirmed success for an attempt:\n // 1. a success marker that was not there when the attempt started, or\n // 2. the SaveFormSubmission request answering 2xx (read from the\n // browser's resource timing, where it exposes responseStatus), when\n // no other form was submitted around it, or\n // 3. as the page leaves (a redirect form), either of the above.\n // It never fires on a 400 or 401, on a filled anti-spam field, or when it\n // cannot tell which form a save belongs to.\n\n var EVENT_NAME = 'squarespace_form_submitted';\n var BLOCK_SELECTOR = '.sqs-block-form, [data-sqsp-block=\"form\"], [data-definition-name=\"website.components.form\"]';\n var SAVE_PATH = '/api/form/SaveFormSubmission';\n // Long enough for a visitor to solve a reCAPTCHA picture challenge.\n var WATCH_MS = 180000;\n // A submit this soon before an attempt, still unanswered, makes the next\n // save ambiguous.\n var LOOKBACK_MS = 5000;\n\n function now() {\n try {\n if (window.performance && typeof window.performance.now === 'function') return window.performance.now();\n } catch (e) {}\n return 0;\n }\n\n function hasToken(el, token) {\n if (!el || !el.getAttribute) return false;\n var cls = el.getAttribute('class') || '';\n return (' ' + cls + ' ').indexOf(' ' + token + ' ') !== -1;\n }\n\n function isConnected(el) {\n try {\n if (typeof el.isConnected === 'boolean') return el.isConnected;\n return !!(el.ownerDocument && el.ownerDocument.documentElement.contains(el));\n } catch (e) {\n return false;\n }\n }\n\n // ---- Which forms are Squarespace forms ----\n\n function blockFor(formEl) {\n try {\n return (formEl && formEl.closest) ? formEl.closest(BLOCK_SELECTOR) : null;\n } catch (e) {\n return null;\n }\n }\n\n function isSquarespaceForm(formEl) {\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return false;\n return !!blockFor(formEl);\n }\n\n // The block's own form definition (the site owner's settings, not the\n // visitor's answers): { formId, formName, ... }.\n function formIdentity(block) {\n var id = { formId: '', formName: '' };\n try {\n var script = block && block.querySelector('script.sqs-form-block-context');\n if (!script) return id;\n var parsed = JSON.parse(script.textContent || '{}');\n if (parsed && typeof parsed.formId === 'string') id.formId = parsed.formId;\n if (parsed && typeof parsed.formName === 'string') id.formName = parsed.formName;\n } catch (e) {}\n return id;\n }\n\n // The honeypot is a text input visitors cannot see or reach (measured id\n // \"message-field\", inside an aria-hidden wrapper). Squarespace answers a\n // filled one with a 204 and its thank-you message, so a filled honeypot\n // means a bot. Only whether it is empty is checked.\n function honeypotFilled(formEl) {\n try {\n var hp = formEl.querySelector('#message-field');\n if (!hp) return false;\n var wrap = hp.closest && hp.closest('[aria-hidden=\"true\"]');\n if (!wrap || !formEl.contains(wrap)) return false;\n return hp.value !== '';\n } catch (e) {\n return false;\n }\n }\n\n function submitButtonOf(formEl) {\n try { return formEl.querySelector('button[type=\"submit\"]'); } catch (e) { return null; }\n }\n\n // ---- Which form a save request belongs to ----\n\n var seenSaves = []; // SaveFormSubmission resource entries seen so far\n var claimed = []; // entries already credited to a form\n var submitLog = []; // every form submit on the page, with its time\n\n function pageOrigin() {\n try {\n var l = window.location;\n return l.origin || (l.protocol + '//' + l.host);\n } catch (e) {\n return '';\n }\n }\n\n // The save request's address is checked for its origin and path only.\n function isSaveEntry(e) {\n var n = (e && e.name) || '';\n var origin = pageOrigin();\n if (!origin || n.indexOf(origin + '/') !== 0) return false;\n return n.slice(origin.length).split(/[?#]/)[0] === SAVE_PATH;\n }\n\n function prune() {\n var cutoff = now() - (WATCH_MS + LOOKBACK_MS);\n var i;\n for (i = submitLog.length - 1; i >= 0; i--) if (submitLog[i].at < cutoff) submitLog.splice(i, 1);\n for (i = seenSaves.length - 1; i >= 0; i--) if (seenSaves[i].startTime < cutoff) seenSaves.splice(i, 1);\n if (seenSaves.length > 100) seenSaves.splice(0, seenSaves.length - 100);\n if (claimed.length > 100) claimed.splice(0, claimed.length - 100);\n }\n\n function allSaveEntries() {\n var all = seenSaves.slice();\n try {\n if (window.performance && typeof window.performance.getEntriesByType === 'function') {\n var buffered = window.performance.getEntriesByType('resource');\n for (var b = 0; b < buffered.length; b++) if (all.indexOf(buffered[b]) === -1) all.push(buffered[b]);\n }\n } catch (err) {}\n var out = [];\n for (var i = 0; i < all.length; i++) if (isSaveEntry(all[i])) out.push(all[i]);\n return out;\n }\n\n function saveEntriesSince(since) {\n var all = allSaveEntries();\n var out = [];\n for (var i = 0; i < all.length; i++) {\n if (all[i].startTime >= since && claimed.indexOf(all[i]) === -1) out.push(all[i]);\n }\n out.sort(function (x, y) { return x.startTime - y.startTime; });\n return out;\n }\n\n // Another form (a Newsletter block included) submitted during this\n // attempt, or just before it without its own save yet, makes a save\n // ambiguous.\n function saveOwnershipAmbiguous(formEl, since, until) {\n var saves = null;\n for (var i = 0; i < submitLog.length; i++) {\n var s = submitLog[i];\n if (s.form === formEl || s.at > until) continue;\n if (s.at >= since) return true;\n if (s.at >= since - LOOKBACK_MS) {\n if (!saves) saves = allSaveEntries();\n var answered = false;\n for (var j = 0; j < saves.length; j++) {\n if (saves[j].startTime >= s.at && saves[j].startTime < since) { answered = true; break; }\n }\n if (!answered) return true;\n }\n }\n return false;\n }\n\n function statusOf(entry) {\n var s = entry && entry.responseStatus;\n return (typeof s === 'number' && s > 0) ? s : null;\n }\n\n // ---- One watcher per form ----\n\n var watchers = [];\n var firedForms = [];\n\n function findWatcher(formEl) {\n for (var i = 0; i < watchers.length; i++) if (watchers[i].form === formEl) return watchers[i];\n return null;\n }\n\n function removeWatcher(w) {\n for (var i = 0; i < watchers.length; i++) if (watchers[i] === w) { watchers.splice(i, 1); return; }\n }\n\n function dropStaleWatchers(formEl, block) {\n for (var i = watchers.length - 1; i >= 0; i--) {\n var w = watchers[i];\n if (w.form === formEl) continue;\n if (!isConnected(w.form) || (block && w.block === block)) w.cleanup();\n }\n }\n\n function createWatcher(formEl, block) {\n var scope = (formEl.closest && formEl.closest('.form-wrapper')) || block;\n function markers() {\n var text = false;\n try { text = !!(scope && scope.querySelector('.form-submission-text')); } catch (e) {}\n return {\n submitted: hasToken(formEl, 'react-form-contents--submitted'),\n button: hasToken(submitButtonOf(formEl), 'success'),\n text: text\n };\n }\n var w = {\n form: formEl,\n block: block,\n identity: formIdentity(block),\n armed: false,\n submitAt: 0,\n answered: true,\n done: false,\n timer: null,\n observer: null,\n pagehideArmed: false,\n staleError: false,\n had: markers()\n };\n\n function redirectConfigured() {\n try { return !!(formEl.getAttribute && formEl.getAttribute('data-success-redirect')); } catch (e) { return false; }\n }\n\n function cleanup() {\n w.done = true;\n if (w.timer) { try { clearTimeout(w.timer); } catch (e) {} w.timer = null; }\n if (w.observer) { try { w.observer.disconnect(); } catch (e) {} w.observer = null; }\n if (w.pagehideArmed) {\n try { window.removeEventListener('pagehide', onPageHide, true); } catch (e) {}\n w.pagehideArmed = false;\n }\n removeWatcher(w);\n }\n\n function fire() {\n if (w.done) return;\n cleanup();\n firedForms.push(formEl);\n var payload = { event: EVENT_NAME, form_id: w.identity.formId };\n if (w.identity.formName) payload.form_name = w.identity.formName;\n window.dataLayer.push(payload);\n }\n\n function domConfirmsSuccess() {\n var m = markers();\n return (m.submitted && !w.had.submitted) || (m.button && !w.had.button) || (m.text && !w.had.text);\n }\n\n function check() {\n if (w.done || !w.armed) return;\n if (!isConnected(formEl)) { cleanup(); return; }\n if (domConfirmsSuccess()) { fire(); return; }\n if (w.answered) return;\n // A fresh error state on the button is Squarespace refusing this\n // attempt; one left over from an earlier attempt is not.\n var errorShowing = hasToken(submitButtonOf(formEl), 'error');\n if (!errorShowing) w.staleError = false;\n else if (!w.staleError) w.answered = true;\n }\n\n function onPageHide() {\n if (w.done || !w.armed) return;\n if (!isConnected(formEl)) { cleanup(); return; }\n if (domConfirmsSuccess()) { fire(); return; }\n if (saveOwnershipAmbiguous(formEl, w.submitAt, now())) { cleanup(); return; }\n var entries = saveEntriesSince(w.submitAt);\n var last = entries[entries.length - 1];\n var status = statusOf(last);\n if (status !== null && status >= 200 && status < 300) {\n claimed.push(last);\n fire();\n return;\n }\n cleanup();\n }\n\n w.refresh = function (submitAt) {\n // A new attempt replaces the old one once the old one was answered\n // (or its save has been seen); a double submit keeps the first.\n if (!w.armed || w.answered || saveEntriesSince(w.submitAt).length > 0) {\n w.submitAt = submitAt;\n w.answered = false;\n w.staleError = hasToken(submitButtonOf(formEl), 'error');\n }\n w.armed = true;\n if (w.timer) { try { clearTimeout(w.timer); } catch (e) {} }\n try { w.timer = setTimeout(cleanup, WATCH_MS); } catch (e) {}\n if (!w.pagehideArmed && redirectConfigured()) {\n try {\n window.addEventListener('pagehide', onPageHide, true);\n w.pagehideArmed = true;\n } catch (e) {}\n }\n };\n w.check = check;\n w.fire = fire;\n w.cleanup = cleanup;\n w.alive = function () { return isConnected(formEl); };\n w.markAnswered = function () { w.answered = true; };\n\n try {\n if (typeof MutationObserver !== 'undefined' && block) {\n w.observer = new MutationObserver(check);\n w.observer.observe(block, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] });\n }\n } catch (e) {}\n watchers.push(w);\n return w;\n }\n\n // ---- The save request's verdict, from resource timing ----\n\n function onResourceEntries(list) {\n var entries;\n try { entries = list.getEntries(); } catch (e) { return; }\n for (var i = 0; i < entries.length; i++) {\n var e = entries[i];\n if (!e || !isSaveEntry(e)) continue;\n if (seenSaves.indexOf(e) === -1) { seenSaves.push(e); prune(); }\n var status = statusOf(e);\n if (status === null || claimed.indexOf(e) !== -1) continue;\n var candidates = [];\n for (var j = 0; j < watchers.length; j++) {\n var w = watchers[j];\n if (!w.done && w.armed && !w.answered && w.alive() && e.startTime >= w.submitAt) candidates.push(w);\n }\n if (candidates.length !== 1) continue;\n var c = candidates[0];\n if (saveOwnershipAmbiguous(c.form, c.submitAt, e.startTime)) continue;\n if (status >= 200 && status < 300) {\n claimed.push(e);\n c.fire();\n } else {\n c.markAnswered();\n }\n }\n }\n\n // ---- Submits ----\n\n function handleSubmit(event) {\n try {\n var formEl = event.target;\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return;\n submitLog.push({ form: formEl, at: now() });\n prune();\n if (!isSquarespaceForm(formEl)) return;\n var block = blockFor(formEl);\n dropStaleWatchers(formEl, block);\n // Squarespace allows one success per page view.\n if (firedForms.indexOf(formEl) !== -1) return;\n if (hasToken(formEl, 'react-form-contents--submitted')) return;\n if (honeypotFilled(formEl)) {\n var old = findWatcher(formEl);\n if (old) old.cleanup();\n return;\n }\n var w = findWatcher(formEl) || createWatcher(formEl, block);\n w.refresh(now());\n } catch (e) {\n // Never throw back into Squarespace's own form handling.\n }\n }\n\n document.addEventListener('submit', handleSubmit, true);\n try {\n if (typeof PerformanceObserver !== 'undefined') {\n new PerformanceObserver(onResourceEntries).observe({ type: 'resource' });\n }\n } catch (e) {}\n})();\n</script>"
+ "value": "<script>\n(function () {\n 'use strict';\n // Install once, even if GTM injects this tag more than once on the page.\n if (window.__ctDetectSquarespaceForms) return;\n window.__ctDetectSquarespaceForms = true;\n window.dataLayer = window.dataLayer || [];\n\n // Ported from a production module (cdn/v1/modules/\n // squarespace-forms.js, measured against a live Squarespace 7.1 site in\n // October 2026). Event only: it never reads what the visitor typed.\n //\n // What Squarespace does:\n // - The Form block renders a React form in the page (no iframe):\n // .sqs-block-form ... form.react-form-contents[novalidate]. It mounts\n // after DOMContentLoaded, so submits are caught on document.\n // - There is no check in the browser. Every submit posts\n // /api/form/FormSubmissionKey then /api/form/SaveFormSubmission, and the\n // server decides: 204 on success, 400 for a field error, 401 for a failed\n // reCAPTCHA. A submit is never a conversion on its own.\n // - On success it marks the form (react-form-contents--submitted), shows a\n // .form-submission-text message and gives the button the class\n // \"success\". A form set to redirect leaves for its page about 1.3\n // seconds after the 204, before that message renders in Chromium.\n // - The Newsletter block posts to the same SaveFormSubmission address, and\n // that request carries no form identity, so a save only counts for a\n // form that provably owns it.\n //\n // It fires once per form, on the first confirmed success for an attempt:\n // 1. a success marker that was not there when the attempt started, or\n // 2. the SaveFormSubmission request answering 2xx (read from the\n // browser's resource timing, where it exposes responseStatus), when\n // no other form's submit could own that save, or\n // 3. as the page leaves (a redirect form), either of the above.\n // It never fires on a 400 or 401, on a filled anti-spam field, or when it\n // cannot tell which form a save belongs to.\n\n var EVENT_NAME = 'squarespace_form_submitted';\n var BLOCK_SELECTOR = '.sqs-block-form, [data-sqsp-block=\"form\"], [data-definition-name=\"website.components.form\"]';\n var SAVE_PATH = '/api/form/SaveFormSubmission';\n // Long enough for a visitor to solve a reCAPTCHA picture challenge.\n var WATCH_MS = 180000;\n\n function now() {\n try {\n if (window.performance && typeof window.performance.now === 'function') return window.performance.now();\n } catch (e) {}\n return 0;\n }\n\n function hasToken(el, token) {\n if (!el || !el.getAttribute) return false;\n var cls = el.getAttribute('class') || '';\n return (' ' + cls + ' ').indexOf(' ' + token + ' ') !== -1;\n }\n\n function isConnected(el) {\n try {\n if (typeof el.isConnected === 'boolean') return el.isConnected;\n return !!(el.ownerDocument && el.ownerDocument.documentElement.contains(el));\n } catch (e) {\n return false;\n }\n }\n\n // ---- Which forms are Squarespace forms ----\n\n function blockFor(formEl) {\n try {\n return (formEl && formEl.closest) ? formEl.closest(BLOCK_SELECTOR) : null;\n } catch (e) {\n return null;\n }\n }\n\n function isSquarespaceForm(formEl) {\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return false;\n return !!blockFor(formEl);\n }\n\n // The block's own form definition (the site owner's settings, not the\n // visitor's answers): { formId, formName, ... }.\n function formIdentity(block) {\n var id = { formId: '', formName: '' };\n try {\n var script = block && block.querySelector('script.sqs-form-block-context');\n if (!script) return id;\n var parsed = JSON.parse(script.textContent || '{}');\n if (parsed && typeof parsed.formId === 'string') id.formId = parsed.formId;\n if (parsed && typeof parsed.formName === 'string') id.formName = parsed.formName;\n } catch (e) {}\n return id;\n }\n\n // The honeypot is a text input visitors cannot see or reach (measured id\n // \"message-field\", inside an aria-hidden wrapper). Squarespace answers a\n // filled one with a 204 and its thank-you message, so a filled honeypot\n // means a bot. Only whether it is empty is checked.\n function honeypotFilled(formEl) {\n try {\n var hp = formEl.querySelector('#message-field');\n if (!hp) return false;\n var wrap = hp.closest && hp.closest('[aria-hidden=\"true\"]');\n if (!wrap || !formEl.contains(wrap)) return false;\n return hp.value !== '';\n } catch (e) {\n return false;\n }\n }\n\n function submitButtonOf(formEl) {\n try { return formEl.querySelector('button[type=\"submit\"]'); } catch (e) { return null; }\n }\n\n // ---- Which form a save request belongs to ----\n\n var seenSaves = []; // SaveFormSubmission resource entries seen so far\n var claimed = []; // entries already credited to a form\n var submitLog = []; // every form submit on the page, with its time\n\n function pageOrigin() {\n try {\n var l = window.location;\n return l.origin || (l.protocol + '//' + l.host);\n } catch (e) {\n return '';\n }\n }\n\n // The save request's address is checked for its origin and path only.\n function isSaveEntry(e) {\n var n = (e && e.name) || '';\n var origin = pageOrigin();\n if (!origin || n.indexOf(origin + '/') !== 0) return false;\n return n.slice(origin.length).split(/[?#]/)[0] === SAVE_PATH;\n }\n\n function prune() {\n // An attempt is watched for WATCH_MS, and submits up to WATCH_MS before\n // it still count when deciding who owns a save.\n var cutoff = now() - 2 * WATCH_MS;\n var i;\n for (i = submitLog.length - 1; i >= 0; i--) if (submitLog[i].at < cutoff) submitLog.splice(i, 1);\n for (i = seenSaves.length - 1; i >= 0; i--) if (seenSaves[i].startTime < cutoff) seenSaves.splice(i, 1);\n if (seenSaves.length > 100) seenSaves.splice(0, seenSaves.length - 100);\n if (claimed.length > 100) claimed.splice(0, claimed.length - 100);\n }\n\n function allSaveEntries() {\n var all = seenSaves.slice();\n try {\n if (window.performance && typeof window.performance.getEntriesByType === 'function') {\n var buffered = window.performance.getEntriesByType('resource');\n for (var b = 0; b < buffered.length; b++) if (all.indexOf(buffered[b]) === -1) all.push(buffered[b]);\n }\n } catch (err) {}\n var out = [];\n for (var i = 0; i < all.length; i++) if (isSaveEntry(all[i])) out.push(all[i]);\n return out;\n }\n\n function saveEntriesSince(since) {\n var all = allSaveEntries();\n var out = [];\n for (var i = 0; i < all.length; i++) {\n if (all[i].startTime >= since && claimed.indexOf(all[i]) === -1) out.push(all[i]);\n }\n out.sort(function (x, y) { return x.startTime - y.startTime; });\n return out;\n }\n\n // A save's owner is ambiguous when another form (a Newsletter block\n // included) was submitted during this attempt. It is also ambiguous when\n // another form was submitted earlier in the watch window and may still be\n // waiting for its save, because a slow reCAPTCHA can hold that save back\n // until after this attempt starts. Each submit gets at most one save, so\n // fewer saves than submits before this attempt means one is outstanding.\n function saveOwnershipAmbiguous(formEl, since, until) {\n var other = false;\n var first = null;\n var submits = 0;\n for (var i = 0; i < submitLog.length; i++) {\n var s = submitLog[i];\n if (s.at > until || s.at < since - WATCH_MS) continue;\n if (s.at >= since) {\n if (s.form !== formEl) return true;\n continue;\n }\n if (s.form !== formEl) other = true;\n submits++;\n if (first === null || s.at < first) first = s.at;\n }\n if (!other) return false;\n var saves = allSaveEntries();\n var answered = 0;\n for (var j = 0; j < saves.length; j++) {\n if (saves[j].startTime >= first && saves[j].startTime < since) answered++;\n }\n return answered < submits;\n }\n\n function statusOf(entry) {\n var s = entry && entry.responseStatus;\n return (typeof s === 'number' && s > 0) ? s : null;\n }\n\n // ---- One watcher per form ----\n\n var watchers = [];\n var firedForms = [];\n\n function findWatcher(formEl) {\n for (var i = 0; i < watchers.length; i++) if (watchers[i].form === formEl) return watchers[i];\n return null;\n }\n\n function removeWatcher(w) {\n for (var i = 0; i < watchers.length; i++) if (watchers[i] === w) { watchers.splice(i, 1); return; }\n }\n\n function dropStaleWatchers(formEl, block) {\n for (var i = watchers.length - 1; i >= 0; i--) {\n var w = watchers[i];\n if (w.form === formEl) continue;\n if (!isConnected(w.form) || (block && w.block === block)) w.cleanup();\n }\n }\n\n function createWatcher(formEl, block) {\n var scope = (formEl.closest && formEl.closest('.form-wrapper')) || block;\n function markers() {\n var text = false;\n try { text = !!(scope && scope.querySelector('.form-submission-text')); } catch (e) {}\n return {\n submitted: hasToken(formEl, 'react-form-contents--submitted'),\n button: hasToken(submitButtonOf(formEl), 'success'),\n text: text\n };\n }\n var w = {\n form: formEl,\n block: block,\n identity: formIdentity(block),\n armed: false,\n submitAt: 0,\n answered: true,\n done: false,\n timer: null,\n observer: null,\n pagehideArmed: false,\n staleError: false,\n had: markers()\n };\n\n function redirectConfigured() {\n try { return !!(formEl.getAttribute && formEl.getAttribute('data-success-redirect')); } catch (e) { return false; }\n }\n\n function cleanup() {\n w.done = true;\n if (w.timer) { try { clearTimeout(w.timer); } catch (e) {} w.timer = null; }\n if (w.observer) { try { w.observer.disconnect(); } catch (e) {} w.observer = null; }\n if (w.pagehideArmed) {\n try { window.removeEventListener('pagehide', onPageHide, true); } catch (e) {}\n w.pagehideArmed = false;\n }\n removeWatcher(w);\n }\n\n // The push comes first. The form is marked done only once the push went\n // through, so a dataLayer that throws (a broken wrapper) loses nothing,\n // and the next confirmation tries again.\n function fire() {\n if (w.done) return false;\n var payload = { event: EVENT_NAME, form_id: w.identity.formId };\n if (w.identity.formName) payload.form_name = w.identity.formName;\n try {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push(payload);\n } catch (e) {\n return false;\n }\n cleanup();\n firedForms.push(formEl);\n return true;\n }\n\n function domConfirmsSuccess() {\n var m = markers();\n return (m.submitted && !w.had.submitted) || (m.button && !w.had.button) || (m.text && !w.had.text);\n }\n\n function check() {\n if (w.done || !w.armed) return;\n if (!isConnected(formEl)) { cleanup(); return; }\n if (domConfirmsSuccess()) { fire(); return; }\n if (w.answered) return;\n // A fresh error state on the button is Squarespace refusing this\n // attempt; one left over from an earlier attempt is not.\n var errorShowing = hasToken(submitButtonOf(formEl), 'error');\n if (!errorShowing) w.staleError = false;\n else if (!w.staleError) w.answered = true;\n }\n\n // A failure in any callback stays inside this listener and never\n // reaches Squarespace's page.\n function onPageHide() {\n try { pageHideCheck(); } catch (e) {}\n }\n\n function pageHideCheck() {\n if (w.done || !w.armed) return;\n if (!isConnected(formEl)) { cleanup(); return; }\n if (domConfirmsSuccess()) { fire(); return; }\n if (saveOwnershipAmbiguous(formEl, w.submitAt, now())) { cleanup(); return; }\n var entries = saveEntriesSince(w.submitAt);\n var last = entries[entries.length - 1];\n var status = statusOf(last);\n if (status !== null && status >= 200 && status < 300) {\n if (fire()) claimed.push(last);\n else cleanup();\n return;\n }\n cleanup();\n }\n\n w.refresh = function (submitAt) {\n // A new attempt replaces the old one once the old one was answered\n // (or its save has been seen); a double submit keeps the first.\n if (!w.armed || w.answered || saveEntriesSince(w.submitAt).length > 0) {\n w.submitAt = submitAt;\n w.answered = false;\n w.staleError = hasToken(submitButtonOf(formEl), 'error');\n }\n w.armed = true;\n if (w.timer) { try { clearTimeout(w.timer); } catch (e) {} }\n try { w.timer = setTimeout(cleanup, WATCH_MS); } catch (e) {}\n if (!w.pagehideArmed && redirectConfigured()) {\n try {\n window.addEventListener('pagehide', onPageHide, true);\n w.pagehideArmed = true;\n } catch (e) {}\n }\n };\n w.check = check;\n w.fire = fire;\n w.cleanup = cleanup;\n w.alive = function () { return isConnected(formEl); };\n w.markAnswered = function () { w.answered = true; };\n\n try {\n if (typeof MutationObserver !== 'undefined' && block) {\n w.observer = new MutationObserver(function () {\n try { check(); } catch (e) {}\n });\n w.observer.observe(block, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] });\n }\n } catch (e) {}\n watchers.push(w);\n return w;\n }\n\n // ---- The save request's verdict, from resource timing ----\n\n function onResourceEntries(list) {\n var entries;\n try { entries = list.getEntries(); } catch (e) { return; }\n for (var i = 0; i < entries.length; i++) {\n var e = entries[i];\n if (!e || !isSaveEntry(e)) continue;\n if (seenSaves.indexOf(e) === -1) { seenSaves.push(e); prune(); }\n var status = statusOf(e);\n if (status === null || claimed.indexOf(e) !== -1) continue;\n var candidates = [];\n for (var j = 0; j < watchers.length; j++) {\n var w = watchers[j];\n if (!w.done && w.armed && !w.answered && w.alive() && e.startTime >= w.submitAt) candidates.push(w);\n }\n if (candidates.length !== 1) continue;\n var c = candidates[0];\n if (saveOwnershipAmbiguous(c.form, c.submitAt, e.startTime)) continue;\n if (status >= 200 && status < 300) {\n if (c.fire()) claimed.push(e);\n } else {\n c.markAnswered();\n }\n }\n }\n\n // ---- Submits ----\n\n function handleSubmit(event) {\n try {\n var formEl = event.target;\n if (!formEl || (formEl.tagName || '').toLowerCase() !== 'form') return;\n submitLog.push({ form: formEl, at: now() });\n prune();\n if (!isSquarespaceForm(formEl)) return;\n var block = blockFor(formEl);\n dropStaleWatchers(formEl, block);\n // Squarespace allows one success per page view.\n if (firedForms.indexOf(formEl) !== -1) return;\n if (hasToken(formEl, 'react-form-contents--submitted')) return;\n if (honeypotFilled(formEl)) {\n var old = findWatcher(formEl);\n if (old) old.cleanup();\n return;\n }\n var w = findWatcher(formEl) || createWatcher(formEl, block);\n w.refresh(now());\n } catch (e) {\n // Never throw back into Squarespace's own form handling.\n }\n }\n\n document.addEventListener('submit', handleSubmit, true);\n try {\n if (typeof PerformanceObserver !== 'undefined') {\n new PerformanceObserver(function (list) {\n try { onResourceEntries(list); } catch (e) {}\n }).observe({ type: 'resource' });\n }\n } catch (e) {}\n})();\n</script>"
},
{
"type": "BOOLEAN",
diff --git a/skills/conversion-tracking/assets/snippets/acuity-scheduling.js b/skills/conversion-tracking/assets/snippets/acuity-scheduling.js
index 06c3121..2eed339 100644
--- a/skills/conversion-tracking/assets/snippets/acuity-scheduling.js
+++ b/skills/conversion-tracking/assets/snippets/acuity-scheduling.js
@@ -9,8 +9,9 @@
// Acuity's booking form gives the page no booking event of its own. This
// listener pairs with a short snippet the site owner pastes into Acuity's
- // custom conversion tracking field (acuity-snippet.html). Acuity runs that
- // snippet after each booking inside its conversion tracking frame, on
+ // custom conversion tracking field (the field snippet that comes with this
+ // listener). Acuity runs that snippet after each booking inside its
+ // conversion tracking frame, on
// https://conversion-tracking-sandbox.acuityinnovation.com, two frames below
// this page, and it posts the booking's ID to this page only (it names the
// page's own address as the target). The frame is a grandchild, so it can't
@@ -34,7 +35,6 @@
function idSeen(id) {
for (var i = 0; i < seenIds.length; i++) if (seenIds[i] === id) return true;
- seenIds.push(id);
return false;
}
@@ -57,7 +57,16 @@
var type = cleanText(data.appointment_type, 200);
if (type) push.appointment_type = type;
if (typeof data.price === 'string' && PRICE_PATTERN.test(data.price)) push.price = data.price;
- window.dataLayer.push(push);
+ // The ID is remembered only once the push went through, so a dataLayer
+ // that throws (a broken wrapper) never escapes into the page, and a
+ // repeat of the same message can still be counted.
+ try {
+ window.dataLayer = window.dataLayer || [];
+ window.dataLayer.push(push);
+ } catch (e) {
+ return;
+ }
+ seenIds.push(data.appointment_id);
}
window.addEventListener('message', onMessage, false);
diff --git a/skills/conversion-tracking/assets/snippets/cal-com.js b/skills/conversion-tracking/assets/snippets/cal-com.js
index 9748f31..8f27754 100644
--- a/skills/conversion-tracking/assets/snippets/cal-com.js
+++ b/skills/conversion-tracking/assets/snippets/cal-com.js
@@ -11,8 +11,8 @@
// from https://app.cal.com; a booking page pasted in as a plain iframe loads
// from https://cal.com. Each posts from its own origin, so a frame is only
// trusted for the origin its address starts with. The CSS selectors do that
- // prefix match so this listener never reads an iframe's address: Cal.com
- // prefill parameters can carry a name or email.
+ // prefix match so this listener never reads an iframe's address, because
+ // Cal.com prefill parameters can carry a name or email.
var CAL_FRAMES = [
{ selector: 'iframe[src^="https://app.cal.com/"]', origin: 'https://app.cal.com' },
{ selector: 'iframe[src^="https://cal.com/"]', origin: 'https://cal.com' }
@@ -66,7 +66,6 @@
function uidSeen(uid) {
for (var i = 0; i < seenUids.length; i++) if (seenUids[i] === uid) return true;
- seenUids.push(uid);
return false;
}
@@ -93,7 +92,16 @@
var push = { event: 'calcom_booking_successful', booking_uid: booking.uid };
if (typeof booking.eventTypeId === 'number' && isFinite(booking.eventTypeId)) push.event_type_id = String(booking.eventTypeId);
if (typeof booking.status === 'string' && STATUS_PATTERN.test(booking.status)) push.booking_status = booking.status;
- window.dataLayer.push(push);
+ // The ID is remembered only once the push went through, so a dataLayer
+ // that throws (a broken wrapper) never escapes into the page, and a
+ // repeat of the same message can still be counted.
+ try {
+ window.dataLayer = window.dataLayer || [];
+ window.dataLayer.push(push);
+ } catch (e) {
+ return;
+ }
+ seenUids.push(booking.uid);
}
function start() {
diff --git a/skills/conversion-tracking/assets/snippets/squarespace-forms.js b/skills/conversion-tracking/assets/snippets/squarespace-forms.js
index f66a132..65e7513 100644
--- a/skills/conversion-tracking/assets/snippets/squarespace-forms.js
+++ b/skills/conversion-tracking/assets/snippets/squarespace-forms.js
@@ -32,7 +32,7 @@
// 1. a success marker that was not there when the attempt started, or
// 2. the SaveFormSubmission request answering 2xx (read from the
// browser's resource timing, where it exposes responseStatus), when
- // no other form was submitted around it, or
+ // no other form's submit could own that save, or
// 3. as the page leaves (a redirect form), either of the above.
// It never fires on a 400 or 401, on a filled anti-spam field, or when it
// cannot tell which form a save belongs to.
@@ -42,9 +42,6 @@
var SAVE_PATH = '/api/form/SaveFormSubmission';
// Long enough for a visitor to solve a reCAPTCHA picture challenge.
var WATCH_MS = 180000;
- // A submit this soon before an attempt, still unanswered, makes the next
- // save ambiguous.
- var LOOKBACK_MS = 5000;
function now() {
try {
@@ -141,7 +138,9 @@
}
function prune() {
- var cutoff = now() - (WATCH_MS + LOOKBACK_MS);
+ // An attempt is watched for WATCH_MS, and submits up to WATCH_MS before
+ // it still count when deciding who owns a save.
+ var cutoff = now() - 2 * WATCH_MS;
var i;
for (i = submitLog.length - 1; i >= 0; i--) if (submitLog[i].at < cutoff) submitLog.splice(i, 1);
for (i = seenSaves.length - 1; i >= 0; i--) if (seenSaves[i].startTime < cutoff) seenSaves.splice(i, 1);
@@ -172,25 +171,34 @@
return out;
}
- // Another form (a Newsletter block included) submitted during this
- // attempt, or just before it without its own save yet, makes a save
- // ambiguous.
+ // A save's owner is ambiguous when another form (a Newsletter block
+ // included) was submitted during this attempt. It is also ambiguous when
+ // another form was submitted earlier in the watch window and may still be
+ // waiting for its save, because a slow reCAPTCHA can hold that save back
+ // until after this attempt starts. Each submit gets at most one save, so
+ // fewer saves than submits before this attempt means one is outstanding.
function saveOwnershipAmbiguous(formEl, since, until) {
- var saves = null;
+ var other = false;
+ var first = null;
+ var submits = 0;
for (var i = 0; i < submitLog.length; i++) {
var s = submitLog[i];
- if (s.form === formEl || s.at > until) continue;
- if (s.at >= since) return true;
- if (s.at >= since - LOOKBACK_MS) {
- if (!saves) saves = allSaveEntries();
- var answered = false;
- for (var j = 0; j < saves.length; j++) {
- if (saves[j].startTime >= s.at && saves[j].startTime < since) { answered = true; break; }
- }
- if (!answered) return true;
+ if (s.at > until || s.at < since - WATCH_MS) continue;
+ if (s.at >= since) {
+ if (s.form !== formEl) return true;
+ continue;
}
+ if (s.form !== formEl) other = true;
+ submits++;
+ if (first === null || s.at < first) first = s.at;
}
- return false;
+ if (!other) return false;
+ var saves = allSaveEntries();
+ var answered = 0;
+ for (var j = 0; j < saves.length; j++) {
+ if (saves[j].startTime >= first && saves[j].startTime < since) answered++;
+ }
+ return answered < submits;
}
function statusOf(entry) {
@@ -261,13 +269,22 @@
removeWatcher(w);
}
+ // The push comes first. The form is marked done only once the push went
+ // through, so a dataLayer that throws (a broken wrapper) loses nothing,
+ // and the next confirmation tries again.
function fire() {
- if (w.done) return;
- cleanup();
- firedForms.push(formEl);
+ if (w.done) return false;
var payload = { event: EVENT_NAME, form_id: w.identity.formId };
if (w.identity.formName) payload.form_name = w.identity.formName;
- window.dataLayer.push(payload);
+ try {
+ window.dataLayer = window.dataLayer || [];
+ window.dataLayer.push(payload);
+ } catch (e) {
+ return false;
+ }
+ cleanup();
+ firedForms.push(formEl);
+ return true;
}
function domConfirmsSuccess() {
@@ -287,7 +304,13 @@
else if (!w.staleError) w.answered = true;
}
+ // A failure in any callback stays inside this listener and never
+ // reaches Squarespace's page.
function onPageHide() {
+ try { pageHideCheck(); } catch (e) {}
+ }
+
+ function pageHideCheck() {
if (w.done || !w.armed) return;
if (!isConnected(formEl)) { cleanup(); return; }
if (domConfirmsSuccess()) { fire(); return; }
@@ -296,8 +319,8 @@
var last = entries[entries.length - 1];
var status = statusOf(last);
if (status !== null && status >= 200 && status < 300) {
- claimed.push(last);
- fire();
+ if (fire()) claimed.push(last);
+ else cleanup();
return;
}
cleanup();
@@ -329,7 +352,9 @@
try {
if (typeof MutationObserver !== 'undefined' && block) {
- w.observer = new MutationObserver(check);
+ w.observer = new MutationObserver(function () {
+ try { check(); } catch (e) {}
+ });
w.observer.observe(block, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] });
}
} catch (e) {}
@@ -357,8 +382,7 @@
var c = candidates[0];
if (saveOwnershipAmbiguous(c.form, c.submitAt, e.startTime)) continue;
if (status >= 200 && status < 300) {
- claimed.push(e);
- c.fire();
+ if (c.fire()) claimed.push(e);
} else {
c.markAnswered();
}
@@ -394,7 +418,9 @@
document.addEventListener('submit', handleSubmit, true);
try {
if (typeof PerformanceObserver !== 'undefined') {
- new PerformanceObserver(onResourceEntries).observe({ type: 'resource' });
+ new PerformanceObserver(function (list) {
+ try { onResourceEntries(list); } catch (e) {}
+ }).observe({ type: 'resource' });
}
} catch (e) {}
})();