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 companies accessible by means of different websites. We don’t ever need our website utilized as a backlink farm by web 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 attempting to recreation serps on behalf of a consumer or being paid to backlink by an unscrupulous backlinker. Day-after-day 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 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 truly sending to the vacation spot. Whether or not a vacation spot is monetized by means 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 firms 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 wish to get their consideration as a great supply of sturdy site 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 may even see how a lot they’re spending on different campaigns and see the worth in probably constructing a partnership by means of a partnership or sponsorship with Martech Zone.

Add a UTM Querystring To Redirects

Quite 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 website identify because the supply of the marketing campaign.

In features.php in my baby 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 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);

            // 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 positioning identify
                $encoded_site_name = urlencode($site_name); // URL encode the positioning 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments