User970008
User970008

Reputation: 1155

Google Analytics Referral from 301 Redirect

We have a site where we use some custom javascript to rollup all subdomains and external domains into one domain. That is all working. However, the main domain shows as a referral and is breaking the page flow.

We use ISAPI ReWriter in IIS to 301 redirect all traffic to "example.com" to "www.example.com".

Then on all sites, both internal and external, we use the same universal code to determine whether the domain should be set or if the links should be pushed.

Questions: 1) Is it correct to set the domain to ".example.com" (in _gaq.push(['_setDomainName', '.example.com']);) if we want to track the subdomains blog.example.com, store.example.com, etc.example.com?

2) What if we redirected the example.com site in IIS to www.example.com instead of using ISAPI ReWrite?

3) Why is example.com showing up as a referral and how can stop that?

 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXETC']);
      var locText = window.location.host;

        // vars to check domain
        var exampleDomain = new RegExp('www.example.com', 'i');
        var exampleRedirect = new RegExp('^example.com', 'i');

        //check if we are on the main domain
        if(exampleDomain.test(locText) || exampleRedirect.test(locText)){

            //Roll Up (domain and subdomains)
              _gaq.push(['_setAllowLinker', true]);
            _gaq.push(['_setDomainName', '.example.com']);

        } else {
            _gaq.push(['_setDomainName', 'none']);
            _gaq.push(['_addIgnoredRef', window.location.host]);
              _gaq.push(['_setAllowLinker', true]);
        }

        _gaq.push(['_trackPageview']);

        (function() {

        // load the ga.js file.  This happens last now.
        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);
        })();

        // this function is an exclusion blacklist of sites we don't want to push
        function donotpushlinks(linkpattern)
        {
            var blacklist = [
                "twitter"
                ,"facebook"
                ,"tumblr"
                ,"youtube"
                ,"pinterest"
                ,"infantino"
                ,"exampleuk"
            ];
            var re = new RegExp('\\b' + blacklist.join("|") + '\\b', 'i');

            return(linkpattern.match(re) != null);
        }

jQuery(document).ready(function(){
        //for each anchor on the page, check whether we need to push
        jQuery('a').each(function() {
               var a = new RegExp('/' + window.location.host + '/');
               //do not touch javascript links
               var b = new RegExp('^javascript', 'i');
                  var c = new RegExp('javascript', 'i');
                  //test against the blacklist
               var d = donotpushlinks(this.href);

               //1) check if link is not internal
               if(!a.test(this.href))
               {
                   //2) Check if it is javascript
                   if (!b.test(this.href) || !c.test(this.href))
                   {
                           //3) check if it not one of the blackklist test patterns
                           if (!d)
                           {
                               //console.log(d + " " + this.href);
                               jQuery(this).click(function(event)
                               {
                                   event.preventDefault();
                                   event.stopPropagation();

                                   // Google it
                                   _gaq.push(['_link',this.href]);

                                    return false;
                               });
                           }
                           //3) Otherwise, it is a link to an external site outside of example. Open in new window with NO tracking
                           else
                           {

                            jQuery(this).click(function(event)
                               {
                                   event.preventDefault();
                                   event.stopPropagation();
                                   window.open(this.href, '_blank');
                                    return false;
                               });

                           }
                   }
                   //2) Otherwise, it is javascript, so leave it alone
                   else{

                           }
               }
               //1) otherwise it is internal, so leave it alone
               else
               {
                //console.log('Internal link: ' + this.href);  
               }
            });
});

Upvotes: 1

Views: 1013

Answers (1)

Open SEO
Open SEO

Reputation: 1712

I'm not fan of using _setDomainName 'none', it has a few drawbacks.

However, I would start by revisiting the choice of window.location.host in favor of window.location.hostname for _addIgnoredRef, even suggest to do use window.location.hostname.split('.')[window.location.hostname.split('.').length]

The issue with host is that it contains the port number, thus it restricts the chances that it matches the referral value.

https://developer.mozilla.org/en/docs/DOM/window.location

Upvotes: 1

Related Questions