Home > OS >  Google Analytics exclude string apart from one variation using Regex
Google Analytics exclude string apart from one variation using Regex

Time:07-25

I'm trying to configure a Google Analytics filter that would exclude all pages matching ^/app$ and ^/app/.* apart from the following: /app/business-signup

It looks like Google doesn't support negative lookahead. I have found the 2 following relevant discussions, but I haven't managed to make it work. Google Analytics Regex include and exclude string without negative lookahead and RegExp alternative to negative lookahead match for Google Analytics

So far the following include filter is showing the most accurate results, but it's still excluding URLs that shouldn't be excluded.

^(/$|/app/business\-signup|/[^a][^p][^p][^/])

Expected excluded URLs:

  • /app
  • /app/
  • /app/abc
  • /app/abc?test=1
  • ...

Expected included URLs:

  • /app/business-signup
  • /
  • /?test=1
  • /about
  • /about/abc
  • ...

CodePudding user response:

I think you can use:

^/(?:app/business-signup|(?:[^a\n].?.?|.?[^p\n].?|.?.?[^p\n])(?:/.*|$)|...[^/\n].*)?$

See an online demo


  • ^/ - Start-line anchor followed by a literal forward slash;
  • (?: - Open non-capture group;
    • app/business-signup - Match the one option you want to exclude;
    • | - Or;
    • (?:[^a\n].?.?|.?[^p\n].?|.?.?[^p\n])(?:/.*|$) - Two nested non-capture groups. Where the 1st would match up to three characters where we excluded the 'a', 'p' and 2nd 'p' in order to exclude the word 'app' followed by the 2nd group to match a literal forward slash followed by 0 more characters or end-line anchor;
    • | - Or;
    • ...[^/\n].* - Match three characters followed by any character other than forward slash followed by 0 characters;
    • )? - Close non-capture group and make it optional to allow a single forward slash;
  • $ - End-line anchor.

Note: you may just remove the newline characters from the pattern if need be.

CodePudding user response:

You can use

^(/$|/app/business-signup|/(?:[^a]..|.[^p].|..[^p]).*)

See the regex demo. Details:

  • ^ - start of string
  • (/$|/app/business\-signup|/(?:[^a]..|.[^p].|..[^p]).*) - Group 1:
    • /$ - / at the end of string
    • | - or
    • /app/business-signup - a /app/business-signup fixed string
    • | - or
    • /(?:[^a]..|.[^p].|..[^p]).* - a /, then either any char other than a and then any two chars, or any char a char other than p any char, or any two chars and then any char other than p, and the rest of the line.
  • Related