开发者

Tracking external links with Google Analytics trackPageview() not working

开发者 https://www.devze.com 2023-02-06 13:17 出处:网络
I have set up external link tracking as Goals in Google Analytics according to the GA documentation. Here is the page in question: http://playmoreatthey.org/ - the external links on the page are form

I have set up external link tracking as Goals in Google Analytics according to the GA documentation.

Here is the page in question: http://playmoreatthey.org/ - the external links on the page are formatted such as

<a href="http://www.ymcagreaterprovidence.org/Default.aspx?alias=www.ymcagreaterprovidence.org/baysidebranch" onclick="javascript: pageTracker._trackPageview('/G1/bayside_famil开发者_高级运维y.com');" target="_blank">Bayside Family YMCA</a>

I set up the goal as a "head match" to the URL: /G1/bayside_family.com

I checked back four days later, and there are no results in the goals or pageviews for the phony "pagename" (/G1/bayside_family.com) specified in the JavaScript attached to each external link.


Looks like on your page you are using GA's async style code _gaq.push(...) but in your onclick you are using their old, "traditional" style code. You need to use

onclick="_gaq.push(['_trackPageview','/G1/bayside_family.com']);"


If you are using jQuery you can automatically track all the links on your site using this script:

// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$("a").on('click', function(e){
    var url = $(this).attr("href");
    if($.trim(url).indexOf("javascript:") == 0) return;
    if (e.currentTarget.host != window.location.host) {
        _gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]);
        var target = $(this).attr("target");
        if (e.metaKey || e.ctrlKey || target == "_blank") {
             var newtab = true;
        }
        if (!newtab) {
             e.preventDefault();
            if(target) {
               setTimeout('window.open("' + url + '", "' + target + '");', 100);
            } else {
               setTimeout('document.location = "' + url + '"', 100);
            }
        }
    }
});

I found the script here: http://wptheming.com/2012/01/tracking-outbound-links-with-google-analytics/comment-page-1/#comment-39716

At the site you can find a debug version that will let you confirm that the script is working correctly.

I deviated from the original script by adding support for links with javascript (aka href="javascript:..."). Also I added code to honor the target attribute.

Here is a jsFiddle so you can see the script in action: http://jsfiddle.net/luisperezphd/45NPe/


One recommended way of doing this is through Events. That way your pageviews metric will not be inflated by the virtual pageviews tracked using the _trackPageview method.

http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920

I add this as an answer because Crayon Violent's comment above contains a broken link. At least someone will be able to edit this answer if the link needs to change again in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消