Search code examples
regexgoogle-analyticsgoogle-analytics-filters

REGEX for Google Analytics Filters


I have a link with a lot of parameters and want to exclude most of them (only keep one) and replace it for a name

The link has the structure below: https://www.abc.ab/something/somethingelse?card_type=status&a-bunch-of-trailing

Card_type can have different "status" values

Ideally I would like to keep: https://www.abc.ab/something/somethingelse?card_type=status and replace ?card_type=status by "/card_type"

I attempted this on GA:

Search string

/*(.*?)\card_type\=*

Replace string: /card_type

But this isn't working at all


Solution

  • You could match ?card_type= followed by matching any char 0+ times .* If it should be from the start of the string you could use an anchor ^ at the start of the pattern.

    In the replacement use the first capturing group followed by the replacement string.

    (.*?)\?card_type=.*
    
    • (.*?) Capture group 1 matching any char 0+ times non greedy
    • \? Match a ? by escaping it
    • card_type= Match literally
    • .* Match any char 0+ times non greedy

    Replace with

    $1/card_type
    

    Regex demo

    To get a bit more precise match for the url instead of using .*?, you might match the protocol:

    ^(https?:\/\/\S+\/[^?]*)\?card_type=.*
    

    Regex demo