Thursday, August 24, 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 providers accessible via different websites. We don’t ever need our web site 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 are able to’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. Day by day 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… finally… the location’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 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 permits me to prepare, observe, and create methods across the visitors I’m sending.

One of many disadvantages of that is that firms will not be monitoring referral websites inside Google Analytics since they may have hundreds of websites sending them visitors. Since I’d wish to get their consideration as a great supply of robust visitors, I’d wish 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 manner, an organization may 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

Moderately than modifying 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 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
perform 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 (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 present 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']) ||
                !isset($existing_params['utm_campaign'])
            ) {
                $site_name = get_option('blogname'); // Get the location title
                $encoded_site_name = urlencode($site_name); // URL encode the location title

                $utm_parameters = array(
                    'utm_source'   => $encoded_site_name, // Use the URL encoded web site 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 present 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 perform, this code will work with any redirection plugin that makes use of that perform.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments