jQuery - Automatically add UTM Campaign data

CCarter

If they cease to believe in u, do u even exist?
Staff member
BuSo Pro
Boot Camp
Digital Strategist
Joined
Sep 15, 2014
Messages
2,180
Likes
4,986
Degree
6
Here is a quick jQuery script that automatically adds the UTM stuff to all outbound links:

Code:
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <script type="text/javascript">
    // This script adds UTM variables so outbound links can be tracked
    // (SERPWoo.com doesn't send referring data by default, so traffic would appear as direct - this fixes that)
    $(document).ready(function(){

        //Need to change this to match within the div path you want to restrict this to
        $(".article a").each(function() {
            var $this = $(this);    
            var _href = $this.attr("href");
            var anchor_text =  encodeURIComponent($this.text());
         
            var current_page = window.location.href.toString().split(window.location.host)[1];

            //skips if within my domain
            if (_href.indexOf(window.location.host) != -1) { return; }

            //skips if not http or https (example: "FTP:" or "mailto:" or an absolute '/blog' scenario)
            if ((_href.indexOf('http://') == -1) && (_href.indexOf('https://') == -1)) { return; }
         
            $this.attr("href", _href + '?utm_source=SERPWoo.com&utm_medium=referral&utm_term=' + anchor_text + '&utm_content=' + current_page  + '&utm_campaign=SERPWooBlog');
            //console.log('link_updated:' + _href);
        });
    });
    </script>
--

Why do you need this? My domain doesn't send referring data, so all traffic would be considered "direct" in all outbound link's analytics.

Potential Abuse? Yeah - lot of potential abuse, that's why Google Analytics referral spam still exists.
 

Ryuzaki

女性以上のお金
Staff member
BuSo Pro
Digital Strategist
Joined
Sep 3, 2014
Messages
3,405
Likes
6,210
Degree
7
One thing to be careful here is that Google has been known to index URLs with UTM tracking parameters on them if they're used a lot with internal links. You also end up getting a lot of backlinks with the tracking codes included.

Google assumes that that's what the webmaster considers canonical, EVEN IF you apply a real canonical in the <head>. They assume they understand your canonical more than you do.

This is happening more since googlebot has starting to render javascript.

If you see this happening, or even before, you may want to head into Search Console and tell Google not to index the UTM parameters. You can find that capability in Search Console > Crawl > URL Parameters.
 

CCarter

If they cease to believe in u, do u even exist?
Staff member
BuSo Pro
Boot Camp
Digital Strategist
Joined
Sep 15, 2014
Messages
2,180
Likes
4,986
Degree
6
That’s good to know about search console. I never use Google webmaster tools, but looks like Google has stepped up it’s game.

internal links
This script only applies the UTM parameters to outbound links. I’m curious - do people have needs that require utm with internal links? Wouldn’t most analytics tack internal links natureally through their code?
 

Ryuzaki

女性以上のお金
Staff member
BuSo Pro
Digital Strategist
Joined
Sep 3, 2014
Messages
3,405
Likes
6,210
Degree
7
I’m curious - do people have needs that require utm with internal links? Wouldn’t most analytics tack internal links natureally through their code?
Yeah, I think most analytics cover most use cases, but there's scenarios where you might be split testing something like a sidebar advertisement to your sales lander, or button colors sitewide, or doing AJAX or jQuery stuff on eCommerce with faceted navigation, etc. Times where knowing the page itself doesn't tell you anything or you need to know the data from an accumulation of pages.
 

BCN

Boss
Joined
Dec 26, 2015
Messages
68
Likes
76
Degree
0
Nice, for external link clicks I use tag manager and a lookup or regex table, which then fires an event in Analytics for i.e. affiliateClicks. Then I can see "CTR" on all my pages.