Ask Your Question
4

What is a way to remove UTM tags from Varnish while ensuring that the desired queries are not removed?

asked 2021-07-17 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-24 23:00:00 +0000

pufferfish gravatar image

One way to remove UTM tags from Varnish while ensuring that desired queries are not removed is to use regular expressions in the VCL (Varnish Configuration Language) to specifically match and remove the UTM tags, but not the desired queries.

For example, to remove any UTM tags (utmsource, utmmedium, utm_campaign, etc.) from the request URL while preserving any other query parameters, you can use the following code in the VCL:

set req.url = regsuball(req.url, "(\\?|&)(utm_(source|medium|campaign|term|content)=[^&]*)", "");

This regular expression will match any UTM tags preceded by a "?" or "&" character and followed by any string of characters that does not include another "&" character. The matched text is replaced with an empty string, effectively removing the UTM tags from the URL.

To ensure that desired query parameters are not removed, you can modify the regular expression to match only the specific UTM tags that you want to remove, or add additional rules to preserve any desired query parameters. For example, to remove only the utm_source tag while preserving all other query parameters, you can use the following code:

set req.url = regsuball(req.url, "(\\?|&)(utm_source=[^&]*)", "");

This regular expression will match only the utm_source tag and replace it with an empty string, leaving all other query parameters intact.

It's important to test any changes to the VCL thoroughly to ensure that desired queries are not removed and the website continues to function as expected.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-17 11:00:00 +0000

Seen: 16 times

Last updated: Apr 24 '22