[Shopify Customer Pixel] How to Configure GA4 View Cart Event Using Google Tag Manager

Learn how to configure the GA4 View Cart event on your Shopify store using Google Tag Manager. This comprehensive guide walks you through setting up the data layer, creating event tags, and verifying tracking for improved ecommerce analytics.

Shahzada Ali Hassan

4 min read0 views

Watch on YouTube

Check your site's tracking

Enter your website to see how good its tracking is, and whether Meta is likely restricting it.

Run a free audit

Configuring a "View Cart" event on your Shopify store is crucial for tracking customer interactions and optimizing your e-commerce analytics. In this guide, I'll walk you through the steps to set up this event using the newer customer pixel section in Shopify. We'll also connect it with Google Analytics 4 (GA4) to ensure all your data is accurately captured and analyzed.

1. Activating Customer Pixels in Shopify

The first step is to ensure that customer pixels are active on your Shopify store. To do this:

  1. Go to the Shopify Backend: Log in to your Shopify admin dashboard.
  2. Navigate to Settings: Scroll down to the "Customer Events" section under the settings menu.
  3. Enable Customer Pixels: This section allows you to create and manage customer events. Ensure it is activated so you can proceed with adding custom events like "View Cart."

2. Adding a Custom Pixel for the View Cart Event

Now, let's create the custom pixel that will track the "View Cart" event:

  1. Add a Custom Pixel:
    • In the customer event section, click on "Add Pixel."
    • Name your pixel something relevant, such as "Data Layer."
  2. Configure Pixel Settings:
    • Permission: Set it to "Not Required."
    • Data Sale: Select the option that indicates "Does Not Qualify as Data Sale."
    • Save the Pixel: Once configured, hit the "Save" button.
  3. Insert Code Snippet:
    • Use the code snippet provided (likely found in the video description or tutorial materials).
    • Paste this code into the designated code section.
    • Google Tag Manager Container ID: Ensure you update the placeholder in the code with your specific Google Tag Manager (GTM) container ID.
view_cart.js
1// Initialize the dataLayer 2window.dataLayer = window.dataLayer || []; 3(function (w, d, s, l, i) { 4 w[l] = w[l] || []; 5 w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" }); 6 var f = d.getElementsByTagName(s)[0], 7 j = d.createElement(s), 8 dl = l != "dataLayer" ? "&l=" + l : ""; 9 j.async = true; 10 j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl; 11 f.parentNode.insertBefore(j, f); 12})(window, document, "script", "dataLayer", "GTM-EXAMPLE"); 13function logEventToConsole(dataLayerEvent) { 14 const customStyle01 = 15 "color: #FFFF00; background-color: #000000; font-size: 10px; font-weight: bold; padding: 2px 0;"; 16 console.log("%cDataLayer Event: view_cart ", customStyle01, dataLayerEvent); 17} 18analytics.subscribe("cart_viewed", (event) => { 19 const getEventData = (obj, path, fallback = "") => { 20 return ( 21 path.split(".").reduce((acc, part) => { 22 if (acc && part.includes("[")) { 23 const [key, index] = part.replace("]", "").split("["); 24 return acc[key] ? acc[key][index] : undefined; 25 } 26 return acc ? acc[part] : undefined; 27 }, obj) || fallback 28 ); 29 }; 30 const page_data = { 31 hostname: getEventData(event, "context.document.location.hostname"), 32 location_query_string: getEventData(event, "context.document.location.href"), 33 path: getEventData(event, "context.document.location.pathname"), 34 referrer: getEventData(event, "context.document.referrer"), 35 page_title: getEventData(event, "context.document.title"), 36 url: getEventData(event, "context.document.location.href"), 37 }; 38 const user_data = { 39 id: event.clientId, 40 language: getEventData(event, "context.navigator.language"), 41 userAgent: getEventData(event, "context.navigator.userAgent"), 42 }; 43 const event_data = { 44 timestamp: event.timestamp || "", 45 id: event.id || "", 46 }; 47 const ecommerce_data = { 48 currency: getEventData(event, "data.cart.cost.totalAmount.currencyCode"), 49 value: getEventData(event, "data.cart.cost.totalAmount.amount"), 50 items: event.data.cart.lines.map((line, index) => ({ 51 item_id: getEventData(line, "merchandise.product.id"), 52 item_name: getEventData(line, "merchandise.product.title"), 53 item_brand: getEventData(line, "merchandise.product.vendor"), 54 item_category: getEventData(line, "merchandise.product.type"), 55 price: getEventData(line, "merchandise.price.amount"), 56 item_variant: 57 getEventData(line, "merchandise.title") || 58 getEventData(line, "merchandise.untranslatedTitle"), 59 item_list_name: "Cart", 60 index: index + 1, 61 product_id: getEventData(line, "merchandise.id"), 62 product_image: getEventData(line, "merchandise.image.src"), 63 product_url: getEventData(line, "merchandise.product.url"), 64 product_untranslatedTitle: getEventData(line, "merchandise.product.untranslatedTitle"), 65 product_sku: getEventData(line, "merchandise.sku"), 66 quantity: getEventData(line, "quantity"), 67 })), 68 }; 69 const dataLayerEvent = { 70 event: "view_cart", 71 user_data: user_data, 72 event_data: event_data, 73 ecommerce: ecommerce_data, 74 page_data: page_data, 75 }; 76 const newUrl = new URL(dataLayerEvent.page_data.location_query_string, window.location.origin); 77 const newTitle = dataLayerEvent.page_data.page_title; 78 if (newUrl && newTitle) { 79 history.pushState(null, newTitle, newUrl.toString()); 80 } 81 dataLayer.push({ ecommerce: null }); 82 window.dataLayer.push(dataLayerEvent); 83 logEventToConsole(dataLayerEvent); 84});

3. Editing the Code Snippet with Google Tag Manager ID

To link your custom pixel with Google Tag Manager:

  1. Retrieve GTM Container ID:
  2. Update the Code Snippet:
    • Paste the container ID into the snippet where indicated.
    • Save your changes to ensure the pixel is correctly configured.

4. Connecting the Pixel with Shopify Backend

After configuring the pixel, it must be connected to your Shopify store:

  1. Connect the Pixel:
    • Go back to your Shopify backend.
    • Ensure that the pixel is correctly linked, or the events won't display on your website.

5. Triggering and Testing the View Cart Event

To verify that the "View Cart" event is working:

  1. Trigger the Event:
    • Visit your website and navigate to the cart page.
    • Use your browser's inspect tool (right-click > Inspect) and switch to the console tab.
  2. Check the Data Layer:
    • In the console, you should see the "Page View" and "View Cart" events firing.
    • Verify that the "View Cart" event includes an e-commerce object with the correct item details.

6. Creating GA4 Configuration and Event Tags

For Google Analytics 4 tracking:

  1. Create a GA4 Configuration Tag:
    • In Google Tag Manager, create a new tag for GA4 configuration.
    • Use your GA4 measurement ID (found in your Google Analytics account) to complete this tag.
  2. Create a View Cart Event Tag:
    • Add another tag for the "View Cart" event.
    • Use a custom trigger that only activates on the "View Cart" event.
    • Configure it to send enhanced e-commerce data, ensuring it captures all relevant details.

7. Testing and Verifying Events on Shopify

Testing is essential to confirm everything works as intended:

  1. Submit Changes: In Google Tag Manager, submit your changes to make them live.
  2. Test the Event:
    • Go back to your Shopify store and trigger the "View Cart" event.
    • Inspect the page and verify that the event is firing correctly.
  3. Additional Testing:
    • You can use a data layer extension or the network tab in your browser to see all requests and events.
    • Ensure that the event sends the correct product details, currency, and value.

8. Final Verification in Google Analytics

Finally, verify the event in Google Analytics:

  1. Real-Time View:
    • Check the real-time view in GA4 to see the "View Cart" event firing.
    • Verify that all parameters, such as currency and value, are correctly recorded.
  2. Additional Debugging:
    • If necessary, additional debugging can be done to ensure the items array works correctly. This may involve more advanced steps not covered in this guide.

By following these steps, you'll have a fully functional "View Cart" event on your Shopify store, ensuring you capture valuable customer interaction data for your analytics.

Set up in about 5 minutes. Events in Events Manager within about 30.

~5 min setupwithin ~30 min
Setting up
  • 7-day free trialon every eligible domain, one per domain
  • $297/moper subdomain
  • We set up the first domain with youfounder included
  • 30-day money-back guaranteeand cancel any time in the trial
  • We keep working with youuntil it is fixed

Payment method required at checkout. One trial per domain. The 30 minutes start once tracking is installed and events fire; it is visibility in Events Manager, not same-day ROAS.