Squarespace: OpenAI Ads Pixel and CAPI for Squarespace Lead Generation Sites: Complete Setup
Step-by-step technical guide to setting up OpenAI Ads Measurement Pixel and CAPI on Squarespace lead generation websites using Code Injection and webhooks.
Squarespace provides hosted website templates with managed infrastructure, but restricts server customization.
While Business and Commerce plans include Code Injection, custom server-side scripts cannot run directly on Squarespace hosts. If a visitor submits an inquiry while using an ad-blocking browser, client-side tracking scripts are prevented from firing.
Capturing all campaign leads requires working with Squarespace's external integration points.
The recommended approach combines Header Code Injection for client-side engagement with external form storage webhooks to transmit Conversions API events directly to OpenAI.
Here is how to set up both client tracking and webhook relays for Squarespace.
Adding the base pixel in Squarespace Code Injection
Deploy the measurement script in your site settings:
- In the Squarespace admin menu, open Website, then Custom Code (or Settings, Developer Tools, Code Injection).
- In the Header box, paste the base pixel snippet:
<script>
!function(w,d,e,u,t,s){if(w.openaiAds)return;t=w.openaiAds=function(){
t.callMethod?t.callMethod.apply(t,arguments):t.queue.push(arguments)};
if(!w._openaiAds)w._openaiAds=t;t.push=t;t.loaded=!0;t.version='1.0';
t.queue=[];s=d.createElement(e);s.async=!0;s.src=u;
var f=d.getElementsByTagName(e)[0];f.parentNode.insertBefore(s,f);
}(window,document,'script','https://bzrcdn.openai.com/pixel.js');
openaiAds('init', 'YOUR_PIXEL_ID');
openaiAds('track', 'PageView');
</script>
<noscript>
<img height="1" width="1" style="display:none"
src="https://bzr.openai.com/v1/events?pixel_id=YOUR_PIXEL_ID&event=PageView&noscript=1" />
</noscript>
Client-side lead tracking via DOM MutationObserver
Squarespace updates the form DOM with a success confirmation (.form-submission-text) once a submission clears. Monitoring the DOM with a MutationObserver ensures the Lead event fires only when submissions are verified:
In the Footer box under Custom Code, add:
<script>
document.addEventListener('DOMContentLoaded', function () {
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) {
if (node.nodeType === 1) {
var isConfirmation = node.classList.contains('form-submission-text') ||
node.querySelector('.form-submission-text');
if (isConfirmation && !node.getAttribute('data-openai-tracked')) {
node.setAttribute('data-openai-tracked', 'true');
var dedupeToken = 'sqsp_lead_' + Date.now();
if (window.openaiAds) {
window.openaiAds('track', 'Lead', {
content_name: 'Squarespace Form Submission'
}, {
event_id: dedupeToken
});
}
}
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
});
</script>
Server-side CAPI via form storage webhooks
To capture submissions blocked by client privacy tools, configure form storage automation:
- In your Squarespace Form Block, click Edit, then Storage.
- Select Zapier or Webhook storage.
- Direct the automation to an edge worker function that delivers CAPI payloads:
export default {
async fetch(request, env) {
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
try {
const data = await request.json();
const dedupeId = data.openai_event_id || `sqsp_lead_${Date.now()}`;
const hashSha256 = async (str) => {
if (!str) return null;
const buf = new TextEncoder().encode(str.trim().toLowerCase());
const hash = await crypto.subtle.digest('SHA-256', buf);
return Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, '0')).join('');
};
const email = data.email || data['Email Address'] || data.Email;
const hashedEmail = await hashSha256(email);
const phone = data.phone || data['Phone Number'] || data.Phone;
const hashedPhone = phone ? await hashSha256(phone.replace(/[^0-9]/g, '')) : null;
const payload = {
data: [
{
event_name: 'Lead',
event_time: Math.floor(Date.now() / 1000),
event_id: dedupeId,
user_data: {
...(hashedEmail && { em: hashedEmail }),
...(hashedPhone && { ph: hashedPhone })
},
custom_data: {
content_name: data.form_name || 'Squarespace Form Submission'
},
action_source: 'system_generated'
}
]
};
const res = await fetch('https://bzr.openai.com/v1/events', {
method: 'POST',
headers: {
'Authorization': `Bearer ${env.OPENAI_ADS_ACCESS_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
return new Response('Success', { status: res.status });
} catch (err) {
return new Response('Error: ' + err.message, { status: 500 });
}
}
};
Verification checklist
Inspect the HTML head of your Squarespace site to confirm the tracking script is present.
Submit a test lead through your form and verify that the MutationObserver triggers the front-end event.
Check OpenAI Ads Event Manager to confirm that lead conversions register with proper parameter mapping.
Frequently asked questions
Where is custom code inserted in Squarespace?
Navigate to Website, select Custom Code (or Settings, Developer Tools, Code Injection), and paste the code into the Header section.
How do I track form leads on Squarespace?
Use a MutationObserver in Footer code injection to detect the appearance of .form-submission-text, triggering openaiAds('track', 'Lead', {...}).
Can Squarespace forms send server CAPI events?
Yes. Connect form storage to a webhook relay or Zapier integration that hashes customer data and posts to https://bzr.openai.com/v1/events.
How do I avoid duplicate lead records?
Format the event_id consistently across client observer scripts and server relays so OpenAI deduplicates matching tokens.
Need ChatGPT tracking installed?
Configured, tested, documented.