The Snippets API allows you to add, update, retrieve, and delete custom HTML and JavaScript code snippets on Square Online sites. Snippets are injected into the <head> element of all pages on the site, except checkout pages.
Overview
The Snippets client provides methods to:
Retrieve snippets from a site
Add or update snippets on a site
Delete snippets from a site
Client Initialization
import (
" context "
" github.com/square/square-go-sdk/v3 "
" github.com/square/square-go-sdk/v3/core "
)
client := square . NewClient (
& core . RequestOptions {
Token : "YOUR_ACCESS_TOKEN" ,
},
)
Methods
Get Snippet
Retrieves your snippet from a Square Online site. A site can contain snippets from multiple applications, but you can only retrieve the snippet added by your application.
request := & square . GetSnippetsRequest {
SiteID : "SITE_ID" ,
}
response , err := client . Snippets . Get ( context . TODO (), request )
if err != nil {
// Handle error
}
if response . Snippet != nil {
fmt . Printf ( "Snippet ID: %s \n " , * response . Snippet . ID )
fmt . Printf ( "Content: %s \n " , response . Snippet . Content )
fmt . Printf ( "Created: %s \n " , * response . Snippet . CreatedAt )
}
The ID of the site that contains the snippet. You can get site IDs using the ListSites endpoint.
The retrieved snippet object The Square-assigned ID for the snippet
The ID of the site that contains the snippet
The snippet code, which can contain valid HTML, JavaScript, or both
The timestamp when the snippet was initially added to the site, in RFC 3339 format
The timestamp when the snippet was last updated on the site, in RFC 3339 format
Upsert Snippet
Adds a snippet to a Square Online site or updates the existing snippet. The snippet code is appended to the end of the <head> element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site.
request := & square . UpsertSnippetRequest {
SiteID : "SITE_ID" ,
Snippet : & square . Snippet {
Content : `<script>
// Google Analytics tracking code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>` ,
},
}
response , err := client . Snippets . Upsert ( context . TODO (), request )
if err != nil {
// Handle error
}
fmt . Printf ( "Snippet ID: %s \n " , * response . Snippet . ID )
fmt . Printf ( "Updated: %s \n " , * response . Snippet . UpdatedAt )
The ID of the site where you want to add or update the snippet
The snippet to add or update The snippet code. Can contain valid HTML, JavaScript, or both. This code will be injected into the <head> element of all site pages except checkout.
The new or updated snippet
Delete Snippet
Removes your snippet from a Square Online site.
request := & square . DeleteSnippetsRequest {
SiteID : "SITE_ID" ,
}
response , err := client . Snippets . Delete ( context . TODO (), request )
if err != nil {
// Handle error
}
fmt . Println ( "Snippet deleted successfully" )
The ID of the site that contains the snippet to delete
Response Types
Snippet
Represents a code snippet that is added to a Square Online site.
The Square-assigned ID for the snippet
The ID of the site that contains the snippet
The snippet code, which can contain valid HTML, JavaScript, or both. This content is injected into the <head> element of all pages on the site, except checkout pages.
The timestamp when the snippet was initially added to the site, in RFC 3339 format
The timestamp when the snippet was last updated on the site, in RFC 3339 format
Use Cases
Add Google Analytics Tracking
// First, get the site ID
sitesResponse , err := client . Sites . List ( context . TODO ())
if err != nil {
log . Fatal ( err )
}
if len ( sitesResponse . Sites ) > 0 {
siteID := * sitesResponse . Sites [ 0 ]. ID
// Add Google Analytics snippet
request := & square . UpsertSnippetRequest {
SiteID : siteID ,
Snippet : & square . Snippet {
Content : `<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
</script>` ,
},
}
response , err := client . Snippets . Upsert ( context . TODO (), request )
if err != nil {
log . Fatal ( err )
}
fmt . Println ( "Analytics tracking added!" )
}
request := & square . UpsertSnippetRequest {
SiteID : "SITE_ID" ,
Snippet : & square . Snippet {
Content : `<!-- SEO Meta Tags -->
<meta name="description" content="Your store description here">
<meta name="keywords" content="keyword1, keyword2, keyword3">
<meta property="og:title" content="Your Store Name">
<meta property="og:description" content="Your store description">
<meta property="og:image" content="https://yoursite.com/image.jpg">
<meta name="twitter:card" content="summary_large_image">` ,
},
}
response , err := client . Snippets . Upsert ( context . TODO (), request )
if err != nil {
log . Fatal ( err )
}
Add Facebook Pixel
request := & square . UpsertSnippetRequest {
SiteID : "SITE_ID" ,
Snippet : & square . Snippet {
Content : `<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'YOUR_PIXEL_ID');
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=YOUR_PIXEL_ID&ev=PageView&noscript=1"/>
</noscript>` ,
},
}
response , err := client . Snippets . Upsert ( context . TODO (), request )
if err != nil {
log . Fatal ( err )
}
Update an Existing Snippet
// Retrieve the current snippet
getRequest := & square . GetSnippetsRequest {
SiteID : "SITE_ID" ,
}
getResponse , err := client . Snippets . Get ( context . TODO (), getRequest )
if err != nil {
log . Fatal ( err )
}
if getResponse . Snippet != nil {
// Update the snippet content
updateRequest := & square . UpsertSnippetRequest {
SiteID : "SITE_ID" ,
Snippet : & square . Snippet {
Content : getResponse . Snippet . Content + `\n<!-- Additional tracking code -->` ,
},
}
updateResponse , err := client . Snippets . Upsert ( context . TODO (), updateRequest )
if err != nil {
log . Fatal ( err )
}
fmt . Println ( "Snippet updated successfully!" )
}
Manage Snippets Across Multiple Sites
// Get all sites
sitesResponse , err := client . Sites . List ( context . TODO ())
if err != nil {
log . Fatal ( err )
}
// Add the same snippet to all published sites
snippetContent := `<script>console.log('Custom tracking');</script>`
for _ , site := range sitesResponse . Sites {
if * site . IsPublished {
request := & square . UpsertSnippetRequest {
SiteID : * site . ID ,
Snippet : & square . Snippet {
Content : snippetContent ,
},
}
_ , err := client . Snippets . Upsert ( context . TODO (), request )
if err != nil {
fmt . Printf ( "Failed to add snippet to site %s : %v \n " , * site . SiteTitle , err )
continue
}
fmt . Printf ( "Snippet added to %s \n " , * site . SiteTitle )
}
}
Error Handling
response , err := client . Snippets . Get ( context . TODO (), request )
if err != nil {
// Check for API errors
if response != nil && len ( response . Errors ) > 0 {
for _ , e := range response . Errors {
fmt . Printf ( "Error: %s - %s \n " , e . Category , e . Detail )
}
}
return err
}
Important Notes
Each application can only add one snippet per site. Calling Upsert multiple times will update the existing snippet rather than creating multiple snippets.
Snippet code is injected into the <head> element of all pages except checkout pages for security reasons.
Use the Sites API to get site IDs before working with snippets.
The Snippets API is part of an early access program and may be subject to changes.