WordPress: How To Append A UTM Marketing campaign Querystring To Exterior Redirects


Martech Zone is usually a pass-through website the place we join our guests with merchandise, options, and providers obtainable by way of different websites. We don’t ever need our website utilized as a backlink farm by search engine marketing consultants, so we’re fairly cautious within the content material we settle for and the way we redirect our guests.

The place we will’t monetize an exterior referring hyperlink, we keep away from passing any authority to the vacation spot. Once more, I don’t need this website ever seen by a search engine as a website the place we’re attempting to sport search engines like google on behalf of a shopper or being paid to backlink by an unscrupulous backlinker. Daily we flip down cash to do that as a result of the outcome would spell catastrophe for my search engine rankings, the belief I’ve constructed with our readers, and… in the end… the positioning’s worth.

WordPress Redirects

To handle this course of, I make the most of Rank Math Professional’s redirection capabilities. It permits me to categorize a redirect to the vacation spot web page I would like and tracks how a lot site visitors I’m really sending to the vacation spot. Whether or not a vacation spot is monetized by way of a referral hyperlink (just like the Rank Math hyperlink I simply shared) or sending site visitors with out an affiliate hyperlink, it permits me to arrange, observe, and create methods across the site visitors I’m sending.

One of many disadvantages of that is that corporations might not be monitoring referral websites inside Google Analytics since they may have 1000’s of web sites sending them site visitors. Since I’d prefer to get their consideration as a very good supply of sturdy site visitors, I’d prefer to append UTM paramers to a marketing campaign querystring in order that Martech Zone doesn’t simply seem of their referring websites; it additionally seems in marketing campaign monitoring inside Google Analytics. This fashion, an organization may even see how a lot they’re spending on different campaigns and see the worth in presumably constructing a partnership by way of a partnership or sponsorship with Martech Zone.

Add a UTM Querystring To Redirects

Quite than enhancing each redirect I’ve constructed, this may be automated by seeking to see if there’s a UTM parameter already on the vacation spot URL, checking to see if it’s an exterior hyperlink, and appending my website title because the supply of the marketing campaign.

In capabilities.php in my youngster theme, I’ve added the next PHP code:

// Add a UTM Querystring to all exterior redirects
operate add_utm_to_redirects($location, $standing) {
    if (is_admin() || !$location) {
        return $location;

    // Test if the redirect standing is 301
    if ($standing === 301) {
        // Test if the vacation spot URL is exterior (exterior the positioning's area)
        $site_url = site_url(); // Get the positioning's base URL
        if (strpos($location, $site_url) !== 0) {
            // Parse the URL to extract current question parameters
            $parsed_url = parse_url($location);
            parse_str($parsed_url['query'] ?? '', $existing_params);

            // Test if UTM parameters exist already within the vacation spot URL
            if (
                !isset($existing_params['utm_source']) ||
                !isset($existing_params['utm_medium']) ||
            ) {
                $site_name = get_option('blogname'); // Get the positioning title
                $encoded_site_name = urlencode($site_name); // URL encode the positioning title

                $utm_parameters = array(
                    'utm_source'   => $encoded_site_name, // Use the URL encoded website title because the utm_source
                    'utm_medium'   => 'article', // Set utm_medium to 'article'
                    'utm_campaign' => 'referral', // Set utm_campaign to 'referral'

                // Merge the brand new UTM parameters with current parameters, excluding duplicates
                $combined_params = array_merge($existing_params, $utm_parameters);

                $query_string = http_build_query($combined_params);

                // Construct the brand new URL with the mixed question parameters
                $new_location = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $query_string;

                // Carry out the redirect with a 301 standing code
                wp_redirect($new_location, 301);

    return $location;
add_filter('wp_redirect', 'add_utm_to_redirects', 10, 2);

As a result of wp_redirect is a WordPress operate, this code will work with any redirection plugin that makes use of that operate.


Please enter your comment!
Please enter your name here