简体   繁体   中英

target=“_blank” not opening new page due to Google Analytics cross domain javascript link tracking

I have probably screwed up the onClick javascript at leanstartupcircle.com . I am trying to add linktracking with javascript to all outbound links that I've tagged with an attribute. Whatever I did is preventing target="_blank" from functioning and probably not triggering the events either.

One other piece of information, target="_blank" is working fine on staging and development because I don't show the google analytics init code on those servers. That is why I believe my javascript is incorrect.

Could use a better pair of eyes. What am I doing wrong?

function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
};

function getDomain( url ) {
    hostname = parseUrl(link).hostname
    splitHostname = hostname.split('.');
    if (splitHostname.length > 2) {
        domain = splitHostname[1] // Most likely the domain
    } else {
        domain = splitHostname[0]
    };
    return domain;
};

function trackLinks() {
    // Setup outbound link tracking and push events to GA based on link attribute linkTracking
    $('a').each(function () {
        link = $(this).attr('href');
        linkTracking = $(this).attr('linkTracking');
        if (linkTracking) {
            category = linkTracking.toLowerCase()
            action = getDomain(link);
            label = link;
            $(this).attr('target', '_blank').attr('onClick', '_gaq.push(["_link", "' + link + '"]); return false; ' +
                                                             'recordOutboundLink(this, "' + category + '", "' + action + '", "' + label + '"); return false;');
        };
    });
};

Here is my google analytics code. There are several modifications suggested by google in order to use cross domain tracking:

var _gaq = _gaq || [];
var pluginUrl =
 '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]); // Enhanced Link Attribution
_gaq.push(['_setAccount', 'UA-30468280-1']);
_gaq.push(['_setDomainName', 'leanstartupcircle.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

// Delay outbound page load in order to record outbound links
function recordOutboundLink(link, category, action) {
  try {
    var myTracker=_gat._getTrackerByName();
    _gaq.push(['myTracker._trackEvent', category , action ]);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}

The return false cancels the click action which means the link will not work. Also the second method you call will never fire because the return false will exit.

Look at the code in a different manner

function anExample() {
    _gaq.push(XXX); 
    return false;  <-- I cause the function to exit                       
    recordOutboundLink(XXXX);  <-- I would never be called
    return false;
}

If you want the link to be followed, you need to drop the return false from the click event.

Google Analytics on Steroids solves this fairly elegantly .

<script type="text/javascript">
var _gas = _gas || [];
_gas.push(['_setAccount', 'UA-YYYYYY-Y']); // REPLACE WITH YOUR GA NUMBER
_gas.push(['_setDomainName', '.mydomain.com']); // REPLACE WITH YOUR DOMAIN
_gas.push(['_trackPageview']);
_gas.push(['_gasTrackOutboundLinks']);

(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = '//cdnjs.cloudflare.com/ajax/libs/gas/1.10.1/gas.min.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM