Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #917197

    Hello,
    I wanted to share a helpful bit of code that often gets overlooked while we optimize your websites.
    It is important to track outbound links on most sites through Google Analytics but can be confusing.

    You’ll first want to put your Universal GA Code from Google into the Google Services tab of your Enfold Theme Options.
    Then in your Functions.php (use a child theme so it saves when the theme updates) place the following script which will not only add tracking of outbound links but will place the script in your footer. I use it on my BigBoyTravel blog and it provides very valuable info which even lower traffic websites will find helpful.

    The stats will be recorded under both Realtime > Events as well as Behavior > Events and clicking Outbound as the category will give you stats on a per link basis.

    
    //Track Outbound Link Clicks
    function add_custom_apple(){
    ?>
    <script>
    	
    (function trackOutbounds() {
            
            var hitCallbackHandler = function(url,win) {
                if (win) {
                        window.open(url, win);
                } else {
                    window.location.href = url;
            }
        };
        
        var addEvent = function(el, eventName, handler) {
        
                    if (el.addEventListener) {
                            el.addEventListener(eventName, handler);
                            } else {
                            el.attachEvent('on' + eventName, function(){
                                    handler.call(el);
                        });
                    }
            }
            
            if (document.getElementsByTagName) {
                    var el = document.getElementsByTagName('a');
                    var getDomain = document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0];
                    
                    // Look thru each a element
                    for (var i=0; i < el.length;i++) {
                    
                            // Extract it's href attribute
                            var href = (typeof(el[i].getAttribute('href')) == 'string' ) ? el[i].getAttribute('href') : '';
                            
                            // Query the href for the top level domain (xxxxx.com)
                            var myDomain = href.match(getDomain);
                            
                            // If link is outbound and is not to this domain        
                            if ((href.match(/^(https?:|\/\/)/i)  && !myDomain) || href.match(/^mailto\:/i)) {
                            
                                    // Add an event to click
                                    addEvent(el[i],'click', function(e) {
                                            var url = this.getAttribute('href'), win = (typeof(this.getAttribute('target')) == 'string') ? this.getAttribute('target') : '';
                                                            
                                                            console.log ("add event", url);
                                            // Log even to Analytics, once done, go to the link
                                            ga('send', 'event', 'outbound', 'click', url,
                                                    {'hitCallback': hitCallbackHandler(url,win)},
                                                    {'nonInteraction': 1}
                                            );
                                            
                                            e.preventDefault();
                                    });
                            }
                    }
            }
    })();
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_apple');
    
    

    You may also want to track visitor demographics and remarketing which requires you not only turn the features on but replace the universal tracking code in your Google Services tab with this but replace UA-YOURTRACKING-ID with yours

    <script>
      (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');
    
      ga('create', 'UA-YOURTRACKING-ID', 'auto');
    ga('require', 'displayfeatures');
      ga('send', 'pageview');
    
    </script>

    If anyone has better or cleaner solutions than these please share so people can use them.

    Jon

    #917689

    Hey Jon,

    Than you for sharing!

    Best regards,
    Victoria

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.