Shopify6 min read3 views

Shopify GA4 view_cart tracking with GTM

Track Shopify cart-page views with GA4 view_cart, use the cart's currency and item prices, and test the data layer before adding checkout events.

Watch on YouTube

Jump to the code

The short answer

Track a Shopify cart-page view by sending GA4 view_cart with the cart's items, currency and value through GTM. Google's ecommerce guidedevelopers.google.com distinguishes viewing a cart from starting checkout. This guide is for implementers maintaining a storefront theme data layer and testing a full cart-page load, not a cart drawer.

  • If Google & YouTube already sends view_cart, inspect it before adding a tag.
  • Use custom_view_cart for the trigger and view_cart for GA4.
  • Run the Liquid snippet only on the cart template.
  • Steps and settings checked against Google and Shopify documentation on 25 October 2026.

Ask an assistant

ChatGPTClaudePerplexity

FREE CALL

30 MIN

Meta stopped counting your leads? Get them counted again.

  1. Pick a time
  2. Check your Meta account with Hassan
  3. See what to do next

On a free 30-minute call, Hassan checks your Meta account with you and shows you what to do next.

Book my free recovery callFree 30-minute call with Hassan.

Not ready to talk? Run a free check

What does GA4 view_cart measure?

Load a test cart whose products and quantities you can recognize, then record those values before testing. Compare that snapshot with the items in Google's view_cart exampledevelopers.google.com.

The video shows the older snippet; the written version adds a cart-template guard and corrects the currency and line-price fields.

When should I use this cart-page setup?

Use it when the store has a full cart page and a storefront GTM data layer you maintain. Google's current Shopify guidesupport.google.com includes view_cart in the app integration, so inspect the native GA4 setup first.

This walkthrough uses GTM installed in the storefront theme. It does not feed a GTM container loaded inside a custom pixel; Shopify's migration tutorialhelp.shopify.com uses a subscription or an explicit published custom event to cross that boundary. Keep these theme snippets with the storefront container.

This is the wrong fix for a cart drawer or cart updates without a page reload. The Liquid payload below is a snapshot of the cart at page render; those interfaces need a mapping that runs when the cart is displayed or updated.

How do I connect the cart tag to GA4?

Reuse the Google tag for the intended GA4 web stream.

1. Check the destination

Google's setup guidesupport.google.com documents the Google Analytics destination and the alternate Google tag method.

  1. Reuse the store's Google tag and GA4 web stream.
  2. For Google's alternate methodsupport.google.com, enter the stream's Measurement ID as Tag ID.
  3. Give that Google tag an Initialization trigger.

Done when the saved ID matches the intended stream.

Where should the cart data layer run?

Run it only on the full cart template. Shopify's template referenceshopify.dev provides template.name, including the cart type.

What should I give the developer?

Give them this page, the development theme, the intended GTM container ID and the test cases below. Ask them to confirm that the storefront container reads the same data layer as the snippet.

How do I add the cart snippet to the theme?

Create a cart snippet in the development theme and render it once.

2. Prepare the theme

  1. Work on a development store.
  2. Duplicate the theme before editing, as Shopify's code-editing guidehelp.shopify.com advises.
  3. Confirm the storefront has the intended test GTM container.

Done when Preview connects to that container in the development theme.

3. Save and render the snippet

  1. Save the code below as snippets/datalayer-cart.liquid.
  2. Place {% render 'datalayer-cart' %} once in theme.liquid. The snippet contains its own cart-page guard.

Done when the development cart page source contains custom_view_cart and a product page does not.

What code sends the cart items to GTM?

The code reads Shopify's cart and sends one cart snapshot with its item amounts.

Shopify's cart referenceshopify.dev exposes the cart's presentment currency, and its line-item referenceshopify.dev defines final_price as the unit price including line-level discounts. The sample uses those fields so the currency label and prices agree.

liquid35 lines
{% if template.name == 'cart' %}<script type="text/javascript">  window.dataLayer = window.dataLayer || [];  console.log("datalayer-cart snippet is loaded")  const cart_data = {{ cart | json}};   var product_details = cart_data.items.map((item, index)=>{      return {            item_id: item.id,            id: item.id,            item_name: item.product_title,            item_brand: item.vendor,            item_category: item.product_type,            quantity: item.quantity,            currency: {{ cart.currency.iso_code | json }},            google_business_vertical: 'retail',            price: parseFloat(item.final_price / 100.0)          }

17 more lines of Liquid

  • Copy and download
  • Emailed to you

Unlock this snippet

Two details, and the full code is yours

No account needed. Unlock once and every snippet on the blog opens.

We email you a copy.

How do I configure the GA4 view_cart tag?

Map the cart fields into a GA4 Event tag and fire it on custom_view_cart.

4. Create the variables and trigger

  1. Create Data Layer Variablessupport.google.com, which read named values from the data layer.
  2. Choose Version 2 for nested fieldssupport.google.com.
  3. Enter the paths below.
  4. Save an exact-match Custom Event trigger for custom_view_cart.
  5. Create a Google Analytics: GA4 Eventsupport.google.com tag.
  6. Enter the intended Measurement ID.
  7. Set Event Name to view_cart.
  8. Map each table row in Event Parameters.
  9. Attach the custom_view_cart Custom Event trigger. A trigger is the condition that runs a tag.
  10. Save the tag.

items

Data Layer Variable path
ecommerce.items
Check
Products and quantities in the rendered cart

currency

Data Layer Variable path
ecommerce.currency
Check
The cart's presentment currency

value

Data Layer Variable path
ecommerce.value
Check
Sum of item unit price times quantity
These paths must match the event payload.

Done when each saved variable has the correct path, the trigger's saved name matches the snippet, and reopening the tag shows that event name, stream, parameters and trigger.

How do I test the cart event?

Compare the rendered cart, GTM payload and received GA4 event.

5. Check the cart snapshot

Google's Preview guidesupport.google.com explains how to inspect fired tags and their data.

  1. Open GTM Preview.
  2. Load a cart containing a known product and quantity.
  3. Inspect custom_view_cart and its ecommerce payload.
  4. Check that the expected GA4 tag fires with the product fields from this visit.
  5. Enable debug modesupport.google.com for the test session.
  6. Open GA4 Admin > Data display > DebugView.
  7. Select view_cart and compare its parameters and items with your test action.
  8. Change the quantity, then reload.
  9. Test a line-level discount.
  10. Switch the development storefront to a second presentment currency different from the shop currency.
  11. Reload the cart page.
  12. Keep debug_mode out of the final non-test configuration.

Done when the event name, products, currency and amounts agree in DebugView; after the quantity change and discount the payload matches the new cart and the calculated value matches the item prices it sends; and in the second currency the event currency, item currencies, rendered unit prices and calculated cart value agree in GTM Preview and the fresh GA4 DebugView event.

Google's event referencedevelopers.google.com defines value as the sum of price times quantity without shipping or tax.

Why does view_cart contain stale or wrong values?

Check where the snippet runs and when its Liquid data was rendered.

  • Placing an unguarded snippet in every page counts non-cart visits. Keep the template.name cart guard.
  • Reading shop currency can mislabel local-currency amounts. Use the cart currency with its presentment prices.
  • A cart drawer can change after the Liquid snapshot. Use a cart event from the current interface instead of assuming this snippet updates itself.

What should I test after the cart page?

Check Shopify checkout entry and purchase tracking. Compare item IDs and quantity definitions across each step.

What changed in this guide

  • 25 October 2026: Added a cart-template guard and data-layer initialization, corrected cart currency and discounted unit prices, fixed the purchase-event wording and added full-page scope and GA4 tests. Added a second-presentment-currency completion check in Preview and DebugView. Replaced the repeated opening with an event-specific test instruction.

Sources

13 sources
  1. 1Google's ecommerce guide↩
  2. 2Google's view_cart example↩
  3. 3Google's current Shopify guide↩
  4. 4Shopify's migration tutorial↩
  5. 5Google's setup guide↩
  6. 6Shopify's template reference↩
  7. 7Shopify's code-editing guide↩
  8. 8Shopify's cart reference↩
  9. 9its line-item reference↩
  10. 10Data Layer Variables↩
  11. 11Google Analytics: GA4 Event↩
  12. 12Google's Preview guide↩
  13. 13debug mode↩