Created
June 29, 2026 15:35
-
-
Save nielsvr/cd7c3115b37ce9a63968e5e9f8fdb09a to your computer and use it in GitHub Desktop.
Simple Analytics tracking with a Shopify Custom Pixel. Add this as JS in the Customer Events custom pixel. Since Simple Analytics is privacy aware, you can enable to fire the pixel always. Replace the hostname with your own, after adding it to Simple Analytics. It uses the Server Side API for the sandboxed events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var SA_HOSTNAME = 'yourhostname.nl'; | |
| var SA_ENDPOINT = 'https://queue.simpleanalyticscdn.com/events'; | |
| // Send a pageview or event to Simple Analytics via the server-side API | |
| function saPost(payload) { | |
| var data = Object.assign({ | |
| hostname: SA_HOSTNAME, | |
| https: true, | |
| ua: navigator.userAgent, | |
| referrer: document.referrer || '', | |
| viewport_width: window.innerWidth, | |
| viewport_height: window.innerHeight, | |
| language: navigator.language | |
| }, payload); | |
| fetch(SA_ENDPOINT, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(data), | |
| keepalive: true | |
| }); | |
| } | |
| // Page view on every page load | |
| analytics.subscribe('page_viewed', function(event) { | |
| saPost({ | |
| type: 'pageview', | |
| event: 'pageview', | |
| path: event.context.document.location.pathname | |
| }); | |
| }); | |
| // Checkout started | |
| analytics.subscribe('checkout_started', function(event) { | |
| saPost({ | |
| type: 'event', | |
| event: 'begin_checkout', | |
| path: event.context.document.location.pathname, | |
| metadata: { | |
| value: event.data.checkout.totalPrice.amount, | |
| currency: event.data.checkout.totalPrice.currencyCode | |
| } | |
| }); | |
| }); | |
| // Purchase completed | |
| analytics.subscribe('checkout_completed', function(event) { | |
| saPost({ | |
| type: 'event', | |
| event: 'purchase', | |
| path: event.context.document.location.pathname, | |
| metadata: { | |
| value: event.data.checkout.totalPrice.amount, | |
| currency: event.data.checkout.totalPrice.currencyCode, | |
| order_id: event.data.checkout.order.id | |
| } | |
| }); | |
| }); | |
| // Product added to cart | |
| analytics.subscribe('product_added_to_cart', function(event) { | |
| var item = event.data.cartLine.merchandise; | |
| saPost({ | |
| type: 'event', | |
| event: 'add_to_cart', | |
| path: event.context.document.location.pathname, | |
| metadata: { | |
| product_id: item.product.id, | |
| product_title: item.product.title, | |
| price: item.price.amount, | |
| currency: item.price.currencyCode | |
| } | |
| }); | |
| }); | |
| // Product page viewed | |
| analytics.subscribe('product_viewed', function(event) { | |
| var product = event.data.productVariant.product; | |
| saPost({ | |
| type: 'event', | |
| event: 'view_item', | |
| path: event.context.document.location.pathname, | |
| metadata: { | |
| product_id: product.id, | |
| product_title: product.title, | |
| price: event.data.productVariant.price.amount, | |
| currency: event.data.productVariant.price.currencyCode | |
| } | |
| }); | |
| }); | |
| // Collection page viewed | |
| analytics.subscribe('collection_viewed', function(event) { | |
| saPost({ | |
| type: 'event', | |
| event: 'view_item_list', | |
| path: event.context.document.location.pathname, | |
| metadata: { | |
| collection_id: event.data.collection.id, | |
| collection_title: event.data.collection.title | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment