Removing a query parameter if it contains certain values
Moderator: General Moderators
-
Odenwalder
- Forum Newbie
- Posts: 4
- Joined: Mon Apr 20, 2009 8:15 am
Removing a query parameter if it contains certain values
Hi everybody,
I am currently trying to build a search/replace filter in Google Analytics, which removes certain query parameters of a URI.
I have a query parameter "t_action", which has a number of possible values. I am trying to remove "t_action" from all URIs, but only if it contains certain values ("index" and "play").
Example:
In this URI, the t_action parameter should be matched and removed:
/index.html?t_action=index
In this URI, t_action should NOT be matched:
/fun/fruit/apples.html?t_action=input
So far I've tried this expression:
^(.*?)[\?&]t_action=[(index)|(play)]
The problem is that this expression, for some reason, matches everything up to the "i" of "index", so it also removes the t_action parameter if it has the value "input".
However, removing "play" seems to work.
I think this should be rather simple, but I can't seem to figure it out.
Does anyone see where I made a mistake here?
I am currently trying to build a search/replace filter in Google Analytics, which removes certain query parameters of a URI.
I have a query parameter "t_action", which has a number of possible values. I am trying to remove "t_action" from all URIs, but only if it contains certain values ("index" and "play").
Example:
In this URI, the t_action parameter should be matched and removed:
/index.html?t_action=index
In this URI, t_action should NOT be matched:
/fun/fruit/apples.html?t_action=input
So far I've tried this expression:
^(.*?)[\?&]t_action=[(index)|(play)]
The problem is that this expression, for some reason, matches everything up to the "i" of "index", so it also removes the t_action parameter if it has the value "input".
However, removing "play" seems to work.
I think this should be rather simple, but I can't seem to figure it out.
Does anyone see where I made a mistake here?
Re: Removing a query parameter if it contains certain values
this should better "/&?t_action=(index|play){1}&?/" the square brackets should only be used to define ranges.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing a query parameter if it contains certain values
@OP:jazz090 wrote:... the square brackets should only be used to define ranges.
More specifically, the square brackets (called a character set, or character class) can be used to define ranges but more importantly, they only match a single character. Also, all "normal" regex meta character loose their special meaning in them.
So this part of your regex: "[(index)|(play)]" will only match one of the following (single!) characters: (, ), |, i, n, d, e, x, p, l, a or y.
-
Odenwalder
- Forum Newbie
- Posts: 4
- Joined: Mon Apr 20, 2009 8:15 am
Re: Removing a query parameter if it contains certain values
Hi and thanks for your response,
I am not quite sure which script language Google Analytics is using, but the documentation on filters says that with the square brackets [ ], you can group elements, the | sign serves as a logical OR, and the brackets ( ) are used to define their content as a character string, not as single characters.
I thought this should work to match either "t_action=index" or "t_action=play".
The complicated version of my expression would be
(.*?)[\?&]t_action=index|(.*?)[\?&]t_action=play
which does work the way I want it, but this doesn't seem useful if you are trying to match a larger set of values.
I am not quite sure which script language Google Analytics is using, but the documentation on filters says that with the square brackets [ ], you can group elements, the | sign serves as a logical OR, and the brackets ( ) are used to define their content as a character string, not as single characters.
I thought this should work to match either "t_action=index" or "t_action=play".
The complicated version of my expression would be
(.*?)[\?&]t_action=index|(.*?)[\?&]t_action=play
which does work the way I want it, but this doesn't seem useful if you are trying to match a larger set of values.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing a query parameter if it contains certain values
This Google-Analytics-help page says otherwise:Odenwalder wrote:Hi and thanks for your response,
I am not quite sure which script language Google Analytics is using, but the documentation on filters says that with the square brackets [ ], you can group elements, ...
http://www.google.com/support/googleana ... swer=91152
In that page they talk about a "character list", which IMO is a confusing name to say because in regex-terms this is always called a "set" or "class". But they all do the same thing: they match a single character.
Code: Select all
foo[abc] // matches 'foo' followed by an 'a', 'b' or 'c'
foo[a-c] // the same as the one above-
Odenwalder
- Forum Newbie
- Posts: 4
- Joined: Mon Apr 20, 2009 8:15 am
Re: Removing a query parameter if it contains certain values
That seems logical. The problem, in this case, is that I would have to group "index" and "play" somehow. Otherwise it would only match either "t_action=index" or "play".
What would you suggest, how to separate certain defined character strings using an OR like above, without having to repeat the "t_action="?
What would you suggest, how to separate certain defined character strings using an OR like above, without having to repeat the "t_action="?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing a query parameter if it contains certain values
Like this:Odenwalder wrote:That seems logical. The problem, in this case, is that I would have to group "index" and "play" somehow. Otherwise it would only match either "t_action=index" or "play".
What would you suggest, how to separate certain defined character strings using an OR like above, without having to repeat the "t_action="?
Code: Select all
t_action=(index|play)This is what jazz090 already suggested, only I left out the "{1}" part which is redundant.
-
Odenwalder
- Forum Newbie
- Posts: 4
- Joined: Mon Apr 20, 2009 8:15 am
Re: Removing a query parameter if it contains certain values
Now I see. This one did the trick
(.*?)[\?&]t_action=(index|play)
I just messed up with the positioning of the brackets. And the [ ] really only match each single character inside them.
Thanks for the advice everyone! And sorry for my slow brain.
(.*?)[\?&]t_action=(index|play)
I just messed up with the positioning of the brackets. And the [ ] really only match each single character inside them.
Thanks for the advice everyone! And sorry for my slow brain.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Removing a query parameter if it contains certain values
No problem. Note that inside a character class, the '?' does not have a special meaning, so:Odenwalder wrote:Now I see. This one did the trick
(.*?)[\?&]t_action=(index|play)
I just messed up with the positioning of the brackets. And the [ ] really only match each single character inside them.
Thanks for the advice everyone! And sorry for my slow brain.
Code: Select all
[\?&]Code: Select all
[?&]