0
votes

I've got Google Analytics events set up to track outbound links. I followed the tutorial on Google's site.

The problem that I'm having is that the 'url' parameter (the Event Label) is not being tracked in Google Analytics. 'outbound' and 'click' are both working just fine from the ga() call. If I alert() the 'url' variable on the line before the ga() call, it will alert it to the screen successfully.

Why would the Category (outbound), and the Action (click) work just fine. However, the Label (var url) does not show up in Google Analytics?

jQuery(document).ready(function() {
    initExternalLinkLogging();
});

function initExternalLinkLogging() {
    // ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external
    externalLinks = jQuery("a:external");
    jQuery(externalLinks).click(function() {
        trackOutboundLink(this);
    });
}

function trackOutboundLink(url) {
    // This is the triggered on each outbound link click successfully
    // 'url' has the correct value, but is not showing up in Google Analytics
    try {
        ga('send', 'event', 'outbound', 'click', url, {
            'hitCallback': function() {
                document.location = url;
            }
        });
    } catch (err) {
        // Do nothing for now
    }
}
1

1 Answers

0
votes

I've resolved this. In this context, the 'url' variable was an object, even though an alert to the screen shows it as a string of the URL. Simply changing this function call will fix the issue.

//This doesn't work
trackOutboundLink(this);

//This fixes it. See ".href"
trackOutboundLink(this.href);

So the full code for reference in a working state is:

jQuery(document).ready(function() {
    initExternalLinkLogging();
});

function initExternalLinkLogging() {
    // ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external
    externalLinks = jQuery("a:external");
    jQuery(externalLinks).click(function() {
        trackOutboundLink(this);
    });
}

function trackOutboundLink(url) {
    // This is the triggered on each outbound link click successfully
    // 'url' has the correct value, but is not showing up in Google Analytics
    try {
        ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
                function() {
                    document.location = url;
                }
        });
    } catch (err) {
    }
}