Wednesday, August 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 website the place we join our guests with merchandise, options, and providers out there via different websites. We don’t ever need our website utilized as a backlink farm by search engine optimization 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 website ever seen by a search engine as a website the place we’re making an attempt to recreation engines like google on behalf of a consumer or being paid to backlink by an unscrupulous backlinker. Daily we flip down cash to do that as a result of the end result would spell catastrophe for my search engine rankings, the belief I’ve constructed with our readers, and… in the end… 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 really 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, monitor, 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 web sites sending them visitors. Since I’d wish to get their consideration as supply of sturdy 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 fashion, an organization might even see how a lot they’re spending on different campaigns and see the worth in probably constructing a partnership via a partnership or sponsorship with Martech Zone.

Add a UTM Querystring To Redirects

Moderately than enhancing each redirect I’ve constructed, this may be automated by trying 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 identify because the supply of the marketing campaign.

In capabilities.php in my little one 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;
    }

    // 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 website 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 perform, this code will work with any redirection plugin that makes use of that perform.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments