为何Google Analytics追踪表单提交数据与购物车链接?如何停止?
Hey there! Let’s break down your questions about Google Analytics (GA) tracking and how to fix the unwanted tracking for form submissions and that specific shopping cart link.
Form Submissions
GA typically tracks form submissions in two main ways:
- Pageview triggers: If your form submission reloads the page or redirects to a confirmation page, GA’s default pageview tracking will automatically log this new page load as a session hit.
- Custom event tracking: If someone added explicit event tracking code (like
gtag('event', 'form_submit', ...)orga('send', 'event', ...)) to your form, GA will actively send an event hit every time the form is submitted.
Your Shopping Cart Link
That link (/de/shop/pizza.html?tx_pizza_basket[action]=addToCart&tx_pizza_basket[controller]=Basket&cHash=...) is a GET request with unique query parameters. GA treats URLs with different query parameters as distinct pages by default, so clicking this link triggers a new pageview hit because it loads a unique URL (even if the core content is just the shopping cart).
Let’s go through solutions for each scenario:
Stopping Form Submission Tracking
Choose the method that fits how your form is set up:
- Prevent page reloads (AJAX submission): If you can modify the form to submit data via AJAX instead of reloading/redirecting, GA won’t trigger a new pageview. You’ll need to update your form’s JavaScript to handle submission without navigating away.
- Remove custom event tracking: Check your GA code for any lines that explicitly send an event on form submit (like
gtag('event', 'form_submit', { ... })). Delete or comment out those lines to stop event tracking for submissions. - Use GA View Filters: In your GA Admin panel (under View > Filters), create an Exclude filter to block pageviews from your form’s confirmation URL. For example, set the filter field to
Request URIand the filter pattern to match your form’s submission path (e.g.,/form-confirmation).
Stopping Tracking for the Shopping Cart Link
Again, pick the approach that works for your site’s setup:
- Prevent link navigation: If the link is supposed to open a cart popup (not load a new page), modify the link to use JavaScript instead of a full page load. Add an
onclickhandler to stop the default navigation:
Then implement the<a href="/de/shop/pizza.html?tx_pizza_basket[action]=addToCart&..." onclick="event.preventDefault(); openShoppingCart();">View Cart</a>openShoppingCart()function to load the cart content dynamically without reloading the page. This way, GA won’t log a new pageview. - Rewrite the page path in GA: If you have to keep the page reload, override the page path GA sends so all cart-related URLs are grouped under a single entry. Add this code before your GA
configorsend pageviewcall:
For older analytics.js code:const currentUrl = window.location.href; if (currentUrl.includes('tx_pizza_basket[action]=addToCart')) { // Replace with your clean cart page path gtag('set', 'page_path', '/de/shop/cart'); } gtag('config', 'YOUR_GA_TRACKING_ID');ga('create', 'UA-XXXXXXXXX-X', 'auto'); const currentPath = window.location.pathname + window.location.search; if (currentPath.includes('tx_pizza_basket[action]=addToCart')) { ga('set', 'page', '/de/shop/cart'); } ga('send', 'pageview'); - Use GA View Filters: Create an Exclude filter in GA Admin to block any URL containing
tx_pizza_basket[action]=addToCart. Set the filter field toRequest URIand the pattern totx_pizza_basket\[action\]=addToCart(escape brackets for regex).
内容的提问来源于stack exchange,提问作者user838531

