E-commerce

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.

A
Antoine
January 14, 20266 min read

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

MethodComplexityCheckout trackingBest for
Google & YouTube appEasyBasic (Shopify Plus only for full)Simple stores
Custom PixelMediumFullMost stores
GTM Custom PixelAdvancedFullComplex 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

  1. 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
  2. Connect your Google account

    • Follow the prompts to sign in
    • Grant the requested permissions
  3. Connect GA4

    • In the app settings, find Google Analytics
    • Select your GA4 property
    • If you don't have one, you can create it here
  4. 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

  1. Create a custom pixel

    • Go to Settings → Customer events
    • Click Add custom pixel
    • Name it "GA4 Tracking"
  2. 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-XXXXXXX with your actual Measurement ID.

  3. Set permissions

    • Under "Data sale", choose your category (usually "Analytics")
    • This integrates with Shopify's consent management
  4. Connect to save

    • Click Save
    • The pixel will now run on your store

Events this tracks

Shopify EventGA4 Event
page_viewedpage_view
product_viewedview_item
product_added_to_cartadd_to_cart
checkout_startedbegin_checkout
checkout_completedpurchase

Extending the pixel

You can add more event subscriptions:

  • collection_viewedview_item_list
  • product_removed_from_cartremove_from_cart
  • search_submittedsearch

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

  1. Create the custom pixel (same as above)

  2. 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...
    
  3. 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

  1. Google & YouTube app + Custom pixel
  2. Theme-embedded gtag.js + Custom pixel
  3. Third-party apps that include GA4

How to fix

  1. Disable Google & YouTube analytics

    • Go to the app settings
    • Turn off Google Analytics integration
    • Keep only your custom pixel
  2. Check your theme

    • Look in Online Store → Themes → Edit code
    • Search for "gtag" or "G-" in theme.liquid
    • Remove any hardcoded GA4 scripts
  3. 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

  1. Add debug mode to your pixel:

    gtag('config', 'G-XXXXXXX', { 'debug_mode': true });
    
  2. Go to GA4 → Admin → DebugView

  3. Walk through a purchase flow

  4. Watch for events appearing

Browser network tab

  1. Open DevTools → Network tab
  2. Filter by "collect" or "google"
  3. Perform actions on your store
  4. Look for GA4 requests with your events

Realtime reports

  1. Go to GA4 → Reports → Realtime
  2. Complete a test action
  3. 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:

  1. Mark purchase as a key event
  2. Set up e-commerce reports
  3. Connect to Google Ads if running ads

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.