1

I am trying to exclude all URLs that contain a PHP query (? in the URL).

Example:

 example.com/mypage?Go_Away

Should Register through Google Analytics as

example.com/mypage

I'm sure there is a way to do it with filters but I would rather use Google Analytics Exclude URL Query Parameters option if I can.

I am using /? Which should register as a question mark and not return any URLs with a? in them yet it is still doing it...

Does anyone have any ideas on how I could achieve this?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
coltonfranco
  • 147
  • 2
  • 15

2 Answers2

1

The "exclude query parameters" takes a list of named parameters (with ?param=Query you woukd need to enter "param"). This won't work for you. So you either have to use filters after all, or you use virtual urls in your trackingcode and pass the url withwout parameters (for example uf you're using the asynchronous tracking code you could use the "pathname" property of the window.location object as a second parameter to your trackpageView call).

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
1

In Google Analytics 4 there is no field anymore to exclude query parameters.

To do that, you first have to distinguish between 2 ways of how you set up GA4:

  1. with gtag.js
  2. with Google Tag Manager (running a GA4 Tag inside)

JavaScript Code solution from a tutorial

<script>
function cleanPageLocation() {
// define parameters to exclude
var excludeStrings = [
    "Go_Away"
];
var addressString = new URL(document.location);
var queryString = addressString.search;

// check if query string holds any parameters, otherwise just return the url without them
if (queryString.indexOf("?") != -1) {
    // https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
    var getQueryParamsFromURL = function getQueryParamsFromURL() {
        var match,
            search = /([^&=]+)=?([^&]*)/g,
            decode = function decode(s) {
                return decodeURIComponent(s);
            },
            query = addressString.search.substring(1);

        var urlParams = {};

        while ((match = search.exec(query))) {
            urlParams[decode(match[1])] = decode(match[2]);
        }

        return urlParams;
    };
    // create param object from query string
    var urlParams = getQueryParamsFromURL();

    // if it holds any of the defined parameters, remove the key and keep the rest
    Object.keys(urlParams).map(function (key) {
        if (excludeStrings.includes(key)) delete urlParams[key];
    });

    // Create filtered query string
    var queryString = new URLSearchParams(urlParams).toString();

    // add ? to querystring unless it's empty
    if (queryString != "") queryString = "?" + queryString;
}

// return cleaned URL
return addressString.origin + addressString.pathname + queryString;
}
</script>

Apply Code Solution for GA4 gtag.js

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...your id..."> 
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'YOUR-ID-HERE', {
    'page_location': cleanPageLocation(),
  });
</script>

Apply code solution for GTM

  • In GTM you use the code solution in a JS Variable
  • Then in the GA4 configuration tag, you add the field page_location and give it as value the JS Variable Settings for GA4 config tag

https://bluerivermountains.com/en/ga4-query-parameter-exclusion