Can we have default UTM parameters on all outbound links in Wordpress?

Boy

@jdcharnell
BuSo Pro
Joined
Feb 21, 2018
Messages
371
Likes
573
Degree
2
Using WordPress, would it be possible to set up default UTMs on all outbound links?
 
Yes, you can achieve this by one of three means:
  1. Filtering the_content() in functions.php
  2. An .htaccess RewriteRule
  3. Using javascript
From top to bottom, I've ordered it in what I think would be the least preferable way to the most preferable way. It's also ordered from how early you're intervening to how late. All code is untested, I'm just showing you the path forward.

Filtering the_content() in functions.php
You can add the parameters in Wordpress itself. The only pitfall here is that it will only work on content within your posts or pages (not sidebar, footer, whatever else).

All post and page content is outputted by the_content() function, and you can add a filter to this to find outbound links and tag on your parameters:

PHP:
function boy_add_utm_outbound( $content) {
    /* find external links and add the parameters in PHP */
    return $content;
}
add_filter(  'the_content', 'boy_add_utm_outbound' );

This is server-side, happens the earliest, and is most resource intensive (even with caching, there's more work to do when you have to filter all the content. It'll get the job done and isn't a bad solution.

.htaccess RewriteRule
This would check every link on the page, make sure the root domain is not your own, then tack your parameters on at the end. It might look something like this:

Code:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !www.yourdomain.com [NC]
RewriteRule ^(.+)$ $1?utm_source=yourbrand [QSA,L]

This is server-side and happens after the page is built but before it's served to the user. I'd say it's less intensive than filtering the_content() but has a greater potential to break your entire site if you have to adjust it or someone tries to mess with it.

Using Javascript
Unless you've stripped jQuery from Wordpress I'd just use that rather than vanilla Javascript. Either way, this is occurring client-side in the browser. Here's a mock-up using jQuery:

See this thread by CCarter - jQuery - Automatically add UTM Campaign data

The positives are that you're offloading the work to their browser (and it's very minimal, not noticeable) and you aren't altering your source code at any fundamental level. The negative is you increase your global payload by a couple kilobytes probably. Not too big of a deal, especially if you have an existing javascript file you can put it in.
 
  • Like
Reactions: Boy
Damn you love taking my "too stupid" questions and making them into their own thread

I appreciate the response, for some reason every Google search resulted in tracking outbound clicks rather than influencing their analytics.

Looks like it's time to learn some jQuery basics so I can edit that to fit my needs...and also brush up on regular expressions.
 
@Boy, It's because it was a good, non-stupid, non-beginner question!

You don't really need Regex. You should be able to run a .each() across all of the links on a page (and you can force this to only run in your main content <div> to optimize the search.

You can also save your own domain as a variable and check to see if the domains you find in the content != the domain in your variable, and only take action on those.

You can also do stuff like this within the .each()... (again, untested, just examples)

JavaScript:
// Cache your parameters
param = ?utm_source=yourbrand&utm_campaign=yourbrand
// Cache the link
var link = $('.main-content a');
// grab the URL
href = link.attr('href');
// strip any existing parameters off
// (watch out for affiliate links, maybe skip any with existing parameters or skip Amazon.com, etc.)
href = href.split('?')[0];
// Append the parameters to the stripped URL
var new_href = href + param;
// Set the new_href into the existing link
link.attr('href', new_href);

You shouldn't need to parse the links with Regex is my point. You can just grab anything that's not your domain, not Amazon, anything with existing parameters, etc. Then jQuery already can strip out everything after and including the '?' in the URL.
 
  • Like
Reactions: Boy
Back