How to Add Custom Code Using Code Injection in Squarespace
Code injection allows you to enhance your site by adding custom HTML and scripts to specific areas. Here's a comprehensive guide on how to use this feature effectively:
Key Points:
- Use code injection for adding HTML scripts, analytics, or third-party integrations
- Don't use it for content or styles (use Custom CSS editor instead)
- JavaScript must be wrapped in
- CSS should be added through the Custom CSS editor
Locations for Code Injection:
- Header
- Inserts into tag
- Affects all site pages
- Footer
- Inserts before closing tag
- Applies site-wide
- Lock Page
- Appears above password field
- For password-protected pages
- Order Confirmation Page
- Displays after gift card, membership, or subscription purchases
- Supports customer-specific tags:
- {orderId}
- {orderSubtotal}
- {orderGrandTotal}
- {customerEmailAddress}
- Order Status Page
- Shows after physical goods/digital products purchase
- Includes same tags as confirmation page
- Use {.if firstPageView} for one-time tracking scripts
Adding Code:
- Open Code Injection panel
- Insert valid HTML/scripts in appropriate field
- Click Save
Warning mark in a red circle
Page-Specific Injection:
- Hover over page in Pages panel
- Click Advanced Settings
- Add code in "Code Injection into Page Header"
Important Notes:
- Cookie banner integration available for third-party scripts
- Preview mode may require script disabling for safe editing
- Checkout pages don't support code injection
- Blog post injection varies by template version
- Always test code in preview before publishing
Best Practices:
- Use Custom CSS editor for styling
- Test thoroughly before implementation
- Follow platform-specific guidelines
- Consider mobile responsiveness
- Keep code organized and documented