In some cases, you may want to use UTM parameters on your website, and redirections involving a UTM parameter may not happen as you expected. In this knowledgebase article, we will discuss using UTM parameters and fix any redirection issues, if it occurs.
What are UTM Parameters?
UTM (Urchin Tracking Module) code is usually added to the end of a URL as query strings. So when someone clicks the link with UTM code and lands on your website, you can configure Google Analytics or other analytics tools to understand the data passed on with the UTM code. They’re commonly used for tracking the performance of marketing campaigns (in terms of traffic, sales, conversions, etc.).
There are only 5 UTM parameters, and let’s look at them:
utm_source
– It can help identify the site where the traffic has resulted fromutm_medium
– It can help you with identifying the marketing mediumutm_campaign
– It shares the name of the specific marketing campaignutm_term
– Used to identify the search termsutm_content
– Helps to identify the specific element or ad that the visitor has clicked before landing on your page
And when these UTM parameters are included in a URL, it would look like this:
https:/example.com?utm_source=newsletter&utm_medium=email&utm_campaign=sale&utm_term=running%2Bshoes&utm_content=textlink
Redirection Default Behaviour With UTM Parameters
By default, Rank Math passes all the query strings (including the UTM parameters) to the redirected URL. So, for instance, if we set up a redirect with Rank Math as below,
A visitor landing on the page https://www.example.com/old-url/?utm_source=newsletter&utm_campaign=sale
would be redirected to https://www.example.com/new-url/?utm_source=newsletter&utm_campaign=sale
Thus you can notice all the UTM parameters of the source URL are being passed on to the destination URL after redirection. But, if you feel passing on these UTM parameters to your redirected URL may mislead your analytics data, you can use the below filter to disable appending them to your redirected URLs.
/**
* Disable appending the query string in the redirection URL.
*
* @param bool $value Enable or disable appending the query string.
* @param array $matched Matched redirection.
*/
add_filter( 'rank_math/redirection/add_query_string', function($value, $matched) {
return false;
}, 10, 2 );
Note: This filter will disable appending all the query strings to your redirected URLs and not just the UTM parameters.
When you use the above filter along with the redirection from the previous example, a visitor landing on the page https://www.example.com/old-url/?utm_source=newsletter&utm_campaign=sale
would be redirected to https://www.example.com/new-url/
without passing on any of the query strings.
Solving Redirection Issues With UTM Parameters
If you’re facing any issues with the redirection when UTM parameters are involved, then change the redirect source URL match from exact to contains. This will ensure that your redirections continue to function as intended, regardless of whether a UTM parameter is added.
Once you’ve done this, check the redirect, and it should function as usual.
And, that’s it! We hope the article was helpful in configuring redirections involving UTM parameters. If you still face any issues with redirects, feel free to reach our support team, as we’re always here to help.