WordPress.org

Ready to get started?Download WordPress

Forums

Google Tag Manager and Webmaster Tools
How to track outbound links? (3 posts)

  1. kamaleon
    Member
    Posted 1 year ago #

    Is there an easy way to track outbound links in GA with this plugin?

    http://wordpress.org/extend/plugins/google-webmaster-tools-manager/

  2. ratz0r
    Member
    Posted 10 months ago #

    Kamaleon,

    Try this in analtyics tag inside of the GTM

    // Outbound Link Tracking with Google Analytics
    // Requires jQuery 1.7 or higher (use .live if using a lower version)
    $(function() {
        $("a").on('click',function(e){
            var url = $(this).attr("href");
            // Console logs shows the domain name of the link being clicked and the current window
            // console.log('e.currentTarget.host: ' + e.currentTarget.host);
            // console.log('window.location.host: ' + window.location.host);
            // If the domains names are different, it assumes it is an external link
            // Be careful with this if you use subdomains
            if (e.currentTarget.host != window.location.host) {
                // console.log('external link click');
                // Outbound link! Fires the Google tracker code.
                _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
                // Checks to see if the ctrl or command key is held down
                // which could indicate the link is being opened in a new tab
                if (e.metaKey || e.ctrlKey) {
                    // console.log('ctrl or meta key pressed');
                    var newtab = true;
                }
                // If it is not a new tab, we need to delay the loading
                // of the new link for a just a second in order to give the
                // Google track event time to fully fire
                if (!newtab) {
                    // console.log('default prevented');
                    e.preventDefault();
                    // console.log('loading link after brief timeout');
                    setTimeout('document.location = "' + url + '"', 100);
                }
            }
            /*
            else {
                console.log('internal link click');
            }
            */
        });
    });
  3. kamaleon
    Member
    Posted 10 months ago #

    Bufff answering after two months... Thank you, but I have used another method. I won't try it now, I hope someone else will.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.