2012-11-15 28 views
0

我們有一個網站,我們使用一些自定義JavaScript來彙總所有子域和外部域到一個域。這一切都工作。但是,主域顯示爲轉介並正在打破頁面流。從301重定向谷歌分析推薦

我們在IIS中使用ISAPI ReWriter將301的所有流量重定向到「example.com」到「www.example.com」。

然後在所有網站上,無論是內部網站還是外部網站,我們都使用相同的通用代碼來確定是應設置域還是應推送鏈接。

問題: 1)將域設置爲「.example.com」(在_gaq.push(['_ setDomainName','.example.com']);)如果我們想要跟蹤子域名blog.example.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); 
       } 
      }); 
}); 

回答

0

我不喜歡使用_setDomainName'none',它有一些缺點。

不過,我會被重新考慮window.location.host贊成window.location.hostname_addIgnoredRef的選擇開始,甚至建議做使用window.location.hostname.split('.')[window.location.hostname.split('.').length]

host的問題是,它包含的端口號,因此限制了機會,它與推薦值相匹配。

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

+0

setDomainName的其他選擇無?該塊用於需要將鏈接從一個域推送到另一個域的外部站點。 – User970008

+0

'_gaq.push(['_ setDomainName','。'+ window.location.hostname]);'會做你所需要的,而不是做一個隱含的_setAllowHash虛假的行'無' –

+0

但是,這將外部網站添加到另一個餅乾,對吧?他們希望子域名彙總到主域名 - 就好像只有一個網站一樣。 – User970008

相關問題