来自301重定向的Google Analytics推介

时间:2012-11-15 18:15:12

标签: javascript jquery google-analytics

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

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

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

问题: 1)将域设置为“.example.com”(在_gaq.push(['_ setDomainName','。example.com'])中是否正确;)如果我们要跟踪子域blog.example.com, store.example.com,etc.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);  
               }
            });
});

1 个答案:

答案 0 :(得分:0)

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

但是,我首先要重新选择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