How to set up GA4 on Shopify (the right way)
Complete guide to installing Google Analytics 4 on your Shopify store. Learn native integration, custom pixels, and GTM setup for full e-commerce tracking.
Shopify's GA4 integration has changed significantly. The native integration is easy but limited. For full e-commerce tracking, you need custom pixels or GTM.
Let me show you the options and help you pick the right one.
Your options
| Method | Complexity | Checkout tracking | Best for |
|---|---|---|---|
| Google & YouTube app | Easy | Basic (Shopify Plus only for full) | Simple stores |
| Custom Pixel | Medium | Full | Most stores |
| GTM Custom Pixel | Advanced | Full | Complex tracking needs |
Method 1: Google & YouTube app (simplest)
This is Shopify's official integration. It's the easiest to set up but has limitations.
Step-by-step
-
Install the app
- Go to your Shopify admin
- Navigate to Settings → Apps and sales channels
- Click Shopify App Store
- Search for "Google & YouTube"
- Click Add app
-
Connect your Google account
- Follow the prompts to sign in
- Grant the requested permissions
-
Connect GA4
- In the app settings, find Google Analytics
- Select your GA4 property
- If you don't have one, you can create it here
-
Configure settings
- Enable the tracking options you want
- Save your changes
What you get
- Automatic pageview tracking
- Basic e-commerce events
- Purchase tracking
Limitations
Important: Non-Shopify-Plus stores cannot track checkout steps with this method. You only get purchase data, not the full funnel.
If you need begin_checkout, add_shipping_info, and add_payment_info events, you'll need custom pixels.
Method 2: Custom Pixel (recommended)
Custom pixels give you full control over tracking, including checkout events.
Step-by-step
-
Create a custom pixel
- Go to Settings → Customer events
- Click Add custom pixel
- Name it "GA4 Tracking"
-
Add the GA4 code
Paste this code in the pixel:
// Initialize GA4 const script = document.createElement('script'); script.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX'; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-XXXXXXX'); // Track page views analytics.subscribe('page_viewed', (event) => { gtag('event', 'page_view', { page_location: event.context.document.location.href, page_title: event.context.document.title }); }); // Track product views analytics.subscribe('product_viewed', (event) => { const product = event.data.productVariant; gtag('event', 'view_item', { currency: product.price.currencyCode, value: parseFloat(product.price.amount), items: [{ item_id: product.sku || product.id, item_name: product.product.title, item_variant: product.title, price: parseFloat(product.price.amount), quantity: 1 }] }); }); // Track add to cart analytics.subscribe('product_added_to_cart', (event) => { const line = event.data.cartLine; gtag('event', 'add_to_cart', { currency: line.merchandise.price.currencyCode, value: parseFloat(line.merchandise.price.amount) * line.quantity, items: [{ item_id: line.merchandise.sku || line.merchandise.id, item_name: line.merchandise.product.title, item_variant: line.merchandise.title, price: parseFloat(line.merchandise.price.amount), quantity: line.quantity }] }); }); // Track checkout started analytics.subscribe('checkout_started', (event) => { const checkout = event.data.checkout; gtag('event', 'begin_checkout', { currency: checkout.currencyCode, value: parseFloat(checkout.totalPrice.amount), items: checkout.lineItems.map(item => ({ item_id: item.variant.sku || item.variant.id, item_name: item.title, item_variant: item.variant.title, price: parseFloat(item.variant.price.amount), quantity: item.quantity })) }); }); // Track purchase analytics.subscribe('checkout_completed', (event) => { const checkout = event.data.checkout; gtag('event', 'purchase', { transaction_id: checkout.order.id, currency: checkout.currencyCode, value: parseFloat(checkout.totalPrice.amount), tax: parseFloat(checkout.totalTax?.amount || 0), shipping: parseFloat(checkout.shippingLine?.price?.amount || 0), items: checkout.lineItems.map(item => ({ item_id: item.variant.sku || item.variant.id, item_name: item.title, item_variant: item.variant.title, price: parseFloat(item.variant.price.amount), quantity: item.quantity })) }); });Replace
G-XXXXXXXwith your actual Measurement ID. -
Set permissions
- Under "Data sale", choose your category (usually "Analytics")
- This integrates with Shopify's consent management
-
Connect to save
- Click Save
- The pixel will now run on your store
Events this tracks
| Shopify Event | GA4 Event |
|---|---|
page_viewed | page_view |
product_viewed | view_item |
product_added_to_cart | add_to_cart |
checkout_started | begin_checkout |
checkout_completed | purchase |
Extending the pixel
You can add more event subscriptions:
collection_viewed→view_item_listproduct_removed_from_cart→remove_from_cartsearch_submitted→search
Method 3: GTM Custom Pixel (advanced)
For maximum flexibility, use GTM inside a custom pixel.
Note: GTM Preview Mode doesn't work in Shopify's sandboxed pixel environment. You'll need to use DebugView or network inspection instead.
Step-by-step
-
Create the custom pixel (same as above)
-
Add GTM container code
// GTM Container (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'); // Push Shopify events to dataLayer analytics.subscribe('product_viewed', (event) => { window.dataLayer.push({ event: 'view_item', ecommerce: { currency: event.data.productVariant.price.currencyCode, value: parseFloat(event.data.productVariant.price.amount), items: [{ item_id: event.data.productVariant.sku, item_name: event.data.productVariant.product.title, price: parseFloat(event.data.productVariant.price.amount) }] } }); }); // Add more event subscriptions... -
Configure GA4 in GTM
- Set up your Google Tag
- Create GA4 Event tags for each e-commerce event
- Use dataLayer variables to map parameters
Benefits of GTM approach
- Centralized tag management
- Easier to add other tracking (Facebook, TikTok, etc.)
- More sophisticated trigger logic
- Better maintainability for complex setups
Avoiding duplicate tracking
If you have multiple GA4 installations, you'll get duplicate data.
Common causes
- Google & YouTube app + Custom pixel
- Theme-embedded gtag.js + Custom pixel
- Third-party apps that include GA4
How to fix
-
Disable Google & YouTube analytics
- Go to the app settings
- Turn off Google Analytics integration
- Keep only your custom pixel
-
Check your theme
- Look in Online Store → Themes → Edit code
- Search for "gtag" or "G-" in theme.liquid
- Remove any hardcoded GA4 scripts
-
Audit installed apps
- Review apps that might include analytics
- Disable their GA4 features
Testing your setup
Since GTM Preview doesn't work, use these methods:
GA4 DebugView
-
Add debug mode to your pixel:
gtag('config', 'G-XXXXXXX', { 'debug_mode': true }); -
Go to GA4 → Admin → DebugView
-
Walk through a purchase flow
-
Watch for events appearing
Browser network tab
- Open DevTools → Network tab
- Filter by "collect" or "google"
- Perform actions on your store
- Look for GA4 requests with your events
Realtime reports
- Go to GA4 → Reports → Realtime
- Complete a test action
- Check if events appear
For more testing methods, see our verification guide.
Consent and compliance
Shopify custom pixels respect consent settings:
// Pixel only fires when consent is granted
// for the category you specified (Marketing/Analytics)
For GDPR compliance, ensure:
- You have a cookie consent banner
- Your pixel is categorized correctly
- Users can opt out
See our GDPR privacy guide for more details.
Next steps
Once GA4 is tracking on Shopify:
If you want simpler e-commerce analytics, try Analayer. We turn your GA4 Shopify data into dashboards that show what's actually driving sales.
See your analytics clearly
Stop struggling with Google Analytics. Connect your account and get a cleaner, simpler view of your data in seconds.