Thursday, November 23, 2023
HomeMobile MarketingWordPress: How To Append A UTM Marketing campaign Querystring To Exterior Redirects

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


Martech Zone is usually a pass-through web site the place we join our guests with merchandise, options, and companies out there via different websites. We don’t ever need our web site utilized as a backlink farm by website positioning 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 web site ever seen by a search engine as a web site the place we’re making an attempt to sport engines like google on behalf of a shopper or being paid to backlink by an unscrupulous backlinker. Each day we flip down cash to do that as a result of the consequence would spell catastrophe for my search engine rankings, the belief I’ve constructed with our readers, and… finally… the location’s worth.

WordPress Redirects

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

One of many disadvantages of that is that firms might not be monitoring referral websites inside Google Analytics since they may have 1000’s of web sites sending them visitors. Since I’d prefer to get their consideration as a superb supply of sturdy 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 might even see how a lot they’re spending on different campaigns and see the worth in presumably constructing a partnership via 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 web site identify because the supply of the marketing campaign.

In features.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;
    }

    // Examine if the redirect standing is 301
    if ($standing === 301) {
        // Examine if the vacation spot URL is exterior (outdoors the location's area)
        $site_url = site_url(); // Get the location'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);

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

                $utm_parameters = array(
                    'utm_source'   => $encoded_site_name, // Use the URL encoded web site identify 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);
                exit();
            }
        }
    }

    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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments