| [ Web Proxy ] |
| Viewing: https://usefathom.com/docs/integrations/shopify | [Back] [Original] |
Adding the Fathom script to your Shopify website is a quick and easy process. Heres how you set up your Shopify site to work with our analytics:
Sales channels > Online Store > Themes.Edit code.
[Editing a theme's code]Layout section, select the theme.liquid file.</head> tag.
[Editing a theme's code]Save.If you would like to capture data for events like purchases, add to cart, checkouts, etc, you can do so using our events within Shopifys web pixel. Heres how:
Customer events.Add custom pixel.Save in the top right.Connect.Below are individual event tracking snippets that you can add to your Web Pixel, depending on which events you want to track. At the bottom, youll find the full script with all events combined.
Adding the Fathom script
This snippet of code ensures Fathom Analytics is loaded on your Shopify store. Its important to add this first.
Note: On the third line in the code below, replace YOUR-SITE-ID inside the single quote marks with your actual Fathom Site ID.
const script = document.createElement('script');
script.id = 'fathom';
script.dataset.site = 'YOUR-SITE-ID';
script.src = "https://cdn.usefathom.com/script.js";
script.setAttribute('defer', '');
script.setAttribute('data-auto', 'false');
document.getElementsByTagName('head')[0].appendChild(script);
Tracking Product Additions to Cart
Tracks when a customer adds a product to their cart and adds the product name and price to the event.
analytics.subscribe("product_added_to_cart", (event) => {
const productName = event.data.cartLine.merchandise.product.title;
const productPrice = event.data.cartLine.cost.totalAmount.amount * 100;
fathom.trackEvent(`Add to cart: ${productName}`, {
_value: productPrice
});
});
Tracking Cart Views
Tracks when a customer views their cart and adds the total cart price to the event.
analytics.subscribe("cart_viewed", (event) => {
const totalCartPrice = event.data.cart.cost.totalAmount.amount * 100;
fathom.trackEvent('Cart Viewed', {
_value: totalCartPrice
});
});
Tracking Checkout Initiation
Tracks when a customer starts the checkout process and adds the total checkout price to the event.
analytics.subscribe("checkout_started", (event) => {
const checkoutTotalPrice = event.data.checkout.totalPrice.amount * 100;
fathom.trackEvent('Checkout Started', {
_value: checkoutTotalPrice
});
});
Tracking Completed Checkouts (Orders Placed)
Tracks when an order is successfully placed and adds the total order price to the event.
analytics.subscribe("checkout_completed", (event) => {
const checkoutTotalPrice = event.data.checkout.totalPrice.amount * 100;
fathom.trackEvent('Order Placed', {
_value: checkoutTotalPrice
});
});
Tracking Product Sales
Tracks when an order is successfully placed and adds each line items product name and price to the event.
analytics.subscribe("checkout_completed", (event) => {
const checkoutTotalPrice = event.data.checkout.totalPrice.amount * 100;
event.data.checkout.lineItems.forEach((item) => {
fathom.trackEvent(`Product Sold: ${item.title}`, {
_value: item.variant.price.amount * 100
});
});
});
Tracking Search Queries
Tracks when a customer submits a search query on your store.
analytics.subscribe("search_submitted", (event) => {
fathom.trackEvent(`Search Query: ${event.data.searchResult.query}`);
});
Full Web Pixel script (All events combined):
If you want to track all of the above events, use this complete script:
Note: On the third line in the code below, remember to replace YOUR-SITE-ID inside the single quote marks with your actual Fathom Site ID.
const script = document.createElement('script');
script.id = 'fathom';
script.dataset.site = 'YOUR-SITE-ID';
script.src = "https://cdn.usefathom.com/script.js";
script.setAttribute('defer', '');
script.setAttribute('data-auto', 'false');
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = () => {
analytics.subscribe("product_added_to_cart", (event) => {
const productName = event.data.cartLine.merchandise.product.title;
const productPrice = event.data.cartLine.cost.totalAmount.amount * 100;
fathom.trackEvent(`Add to cart: ${productName}`, { _value: productPrice });
});
analytics.subscribe("cart_viewed", (event) => {
const totalCartPrice = event.data.cart.cost.totalAmount.amount * 100;
fathom.trackEvent('Cart Viewed', { _value: totalCartPrice });
});
analytics.subscribe("checkout_started", (event) => {
const checkoutTotalPrice = event.data.checkout.totalPrice.amount * 100;
fathom.trackEvent('Checkout Started', { _value: checkoutTotalPrice });
});
analytics.subscribe("checkout_completed", (event) => {
const checkoutTotalPrice = event.data.checkout.totalPrice.amount * 100;
fathom.trackEvent('Order Placed', { _value: checkoutTotalPrice });
event.data.checkout.lineItems.forEach((item) => {
fathom.trackEvent(`Product Sold: ${item.title}`, { _value: item.variant.price.amount * 100 });
});
});
analytics.subscribe("search_submitted", (event) => {
fathom.trackEvent(`Search Query: ${event.data.searchResult.query}`);
});
};
Shopify provide an API for subscribing to customer events using their analytics.subscribe function. There are other customer events available that we havent covered above that you may want to track, or you may want to customise the code examples weve provided above to include additional data in your Fathom events.
You can find more information about Shopifys customer events here. Click on the event name to see more information about the event, how to subscribe to it, and what data is available.
We also have a detailed guide on how our events work here.
If you still have questions or require help with anything, please reach out to us and we'll happily get things sorted out for you.
We've counted billions of pageviews for thousands of customers, all without ever compromising anyone's digital privacy. We hope you'll join us.
2018-2026 Fathom Analytics.| Web Proxy Viewer | New URL | Original Page |