I recently started a Tumblr blog to help gamers find the best prices on their favorite video games, and like any self respecting content publisher I immediately set about adding analytics to the site so that I could track how well my content was converting. I ran into a few snags along the way when adding outbound link tracking with Google Analytics, so hopefully this guide will help save the next blogger some time.

  1. Click the customize link at the top of your blog.

  2. Click the ‘Edit HTML’ button.

    Tumblr's Edit HTML Button

    It should look something like this:

    Tumblr's HTML Editor

  3. Add the below code into your blog’s HTML:

    <head>
        
        <!-- Rest of the <head> content here -->
        
        <!-- Include jQuery -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>
    <body>
    
        <!-- Rest of the <body> content here -->
        
        <script>
            // Load Google Analytics
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
            
            // Send a pageview event for your domain / GA account
            ga('create', 'UA-XXXXXXXX-X', 'example.com');
            ga('send', 'pageview');
        
            // Listen for clicks from outbound links
            jQuery("a[href*='http://']:not([href*='" + window.location.hostname + "'])").click(function(e) {
            
                // Send a click event with the href as the label
                try
                {
                    ga('send', 'event', 'Outbound Link', 'click', jQuery(this).attr('href'));
                }
                catch (err)
                {}
                
                // Delay the page's loading for 100 milliseconds to allow Google time to send the click event
                setTimeout('document.location = "' + jQuery(this).attr('href') + '"', 100);
                
                // Prevent the page from loading
                e.preventDefault();
            });
        </script>
    </body>

Don’t forget to swap in your domain and GA account id in the script block before the end of the body.

It’s also worth noting that the above code should work fine for adding outbound link tracking to Google Analytics on any website. Hope this was helpful, and happy Tumbling!