GTM + Customer Events API
Shopify GTM & Web Pixel Code Generator
Deploy Google Tag Manager and GA4 e-commerce tracking in minutes using Shopify's modern API.
11. Configuration Settings
22. E-commerce Events to Track
33. Generated Code Scripts
Paste this script in Shopify Admin -> Settings -> Customer Events -> Add Custom Pixel. This sandbox-safe pixel catches all user and checkout activities, forwarding them to GTM's dataLayer.
// Shopify Custom Web Pixel (Checkout and Sandbox Compatible)
// Generated by Shopify Marketing & Analytics Hub
(function() {
// Initialize the sandboxed GTM DataLayer
window.dataLayer = window.dataLayer || [];
// Load GTM library inside sandboxed environment
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');
// Event Analytics Subscriptions
analytics.subscribe("page_viewed", (event) => {
window.dataLayer.push({
event: "shopify_page_viewed",
id: event.id,
clientId: event.clientId,
timestamp: event.timestamp,
url: event.context.document.location.href,
referrer: event.context.document.referrer
});
});
analytics.subscribe("product_viewed", (event) => {
const customData = event.data?.productVariant;
window.dataLayer.push({
event: "view_item",
timestamp: event.timestamp,
ecommerce: {
currency: customData?.price?.currencyCode || "USD",
value: parseFloat(customData?.price?.amount || "0"),
items: [{
item_id: customData?.id,
item_name: customData?.title,
price: parseFloat(customData?.price?.amount || "0"),
quantity: 1
}]
}
});
});
analytics.subscribe("collection_viewed", (event) => {
const customData = event.data?.collection;
window.dataLayer.push({
event: "view_item_list",
timestamp: event.timestamp,
ecommerce: {
item_list_id: customData?.id,
item_list_name: customData?.title,
items: []
}
});
});
analytics.subscribe("product_added_to_cart", (event) => {
const customData = event.data?.cartLine?.merchandise;
window.dataLayer.push({
event: "add_to_cart",
timestamp: event.timestamp,
ecommerce: {
currency: customData?.price?.currencyCode || "USD",
value: parseFloat(customData?.price?.amount || "0") * (event.data?.cartLine?.quantity || 1),
items: [{
item_id: customData?.id,
item_name: customData?.product?.title,
item_variant: customData?.title,
price: parseFloat(customData?.price?.amount || "0"),
quantity: event.data?.cartLine?.quantity || 1
}]
}
});
});
analytics.subscribe("checkout_started", (event) => {
const customData = event.data?.checkout;
window.dataLayer.push({
event: "begin_checkout",
timestamp: event.timestamp,
ecommerce: {
currency: customData?.currencyCode || "USD",
value: parseFloat(customData?.totalPrice?.amount || "0"),
items: customData?.lineItems?.map(item => ({
item_id: item.variant?.id,
item_name: item.variant?.product?.title,
price: parseFloat(item.variant?.price?.amount || "0"),
quantity: item.quantity
})) || []
}
});
});
analytics.subscribe("checkout_completed", (event) => {
const customData = event.data?.checkout;
window.dataLayer.push({
event: "purchase",
timestamp: event.timestamp,
ecommerce: {
transaction_id: customData?.order?.id || event.id,
currency: customData?.currencyCode || "USD",
value: parseFloat(customData?.totalPrice?.amount || "0"),
tax: parseFloat(customData?.totalTax?.amount || "0"),
shipping: parseFloat(customData?.shippingLine?.price?.amount || "0"),
items: customData?.lineItems?.map(item => ({
item_id: item.variant?.id,
item_name: item.variant?.product?.title,
price: parseFloat(item.variant?.price?.amount || "0"),
quantity: item.quantity
})) || []
}
});
});
analytics.subscribe("search_submitted", (event) => {
window.dataLayer.push({
event: "search",
search_term: event.data?.searchQuery
});
});
})();How to deploy:
- 1Enter your Google Tag Manager Container ID above (e.g., GTM-K17AB12).
- 2Select the specific e-commerce actions you want to track on your storefront.
- 3Copy the generated 'Shopify Custom Pixel' script. In your Shopify Store Admin, navigate to 'Settings' > 'Customer Events', click 'Add custom pixel', name it (e.g. 'GTM-Tracking'), and paste the code.
- 4Copy the 'GTM Theme Loader' Liquid code, open your Shopify Online Store Theme Editor, edit 'theme.liquid', and paste it directly after the opening '<head>' tag.