1

我们有一个网站,我们使用一些自定义 javascript 将所有子域和外部域汇总到一个域中。这一切都在起作用。但是,主域显示为引用并且正在破坏页面流。

我们在 IIS 中使用 ISAPI ReWriter 以 301 将所有流量重定向到“example.com”到“www.example.com”。

然后在所有网站上,无论是内部还是外部,我们使用相同的通用代码来确定是否应该设置域或是否应该推送链接。

问题:1)如果我们要跟踪子域 blog.example,将域设置为“.example.com”(在 _gaq.push(['_setDomainName', '.example.com']);) 是否正确。 com、store.example.com 等.example.com?

2) 如果我们将 IIS 中的 example.com 站点重定向到 www.example.com 而不是使用 ISAPI ReWrite 会怎样?

3) 为什么 example.com 显示为推荐,如何阻止这种情况?

 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);  
               }
            });
});
4

1 回答 1

1

我不喜欢使用 _setDomainName 'none',它有一些缺点。

但是,我会首先重新考虑window.location.host支持window.location.hostnamefor的选择_addIgnoredRef,甚至建议使用window.location.hostname.split('.')[window.location.hostname.split('.').length]

问题host在于它包含端口号,因此它限制了它与推荐值匹配的机会。

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

于 2012-11-19T15:56:28.027 回答