Stuart Leech Stuart Leech - 1 year ago 74
PHP Question

REGEX for removing PHP Parameter in URL from Google Analytics

I need to create a regular expression for removing a url php parameter in a google analytics filter but I am not equipped with the right skills!

We have a site that appends the following string to urls

?currency=USD etc

Just need to regex for this if anybody is able to help


Answer Source

Based on the documentation in the link you provided, it seems quite simple:


The first part is the query’s currency and the ‘=’. Keep in mind that the query may be one of many and between or before other queries that we want to remain. The next part of the expression, ‘[^&]’, is targeting any character that is not an ampersand so that it stops before the next query. The asterisk is there representing 0 or more of the preceding character, so it will cover us if it is the last query. If there’s an ampersand after the query parameter, we also want to remove it to prevent something like this from showing up “/blog/post-23?lang=en&&x1=key4929”.

Again, in the documentation, they indicate how to remove the trailing ? and & signs with a simple filter: