How do you get location.hashtag from the referrer url - Google Analytics

前端 未结 2 399
一整个雨季
一整个雨季 2021-01-20 10:38

There\'s a site that uses on page buttons and hashtags (#) in their urls to manipulate how their content (links) is ordered. They link to my site and I\'d like to know what

相关标签:
2条回答
  • 2021-01-20 10:50

    The hash portion of URLs is never sent to the server, and it appears it is not stored in the javascript object for the document.referrer.

    The only way to access the hash portion of a URL is to access it from within the page when the browser is on that page.

    Translation: There's no way to get it unless you control the referring page, and you pass along the hash fragment in the link.

    more info: http://www.razzed.com/2009/02/12/uh-oh-ajax-powered-search-kills-keywords-in-referrers/

    0 讨论(0)
  • 2021-01-20 10:56
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    
      /*
       * Function: Hash Custom Variable
       *   Pass everything after # in document.referrer to GA custom variable
       */
      (function() { 
    
        // Parse out the hash part of the referrer
        var referrerHash = document.referrer.split("#")[1];
    
        // If the hash exists, pass it back to GA
        if(typeof referrerHash !== "undefined") {
          _gaq.push(['_setCustomVar', 1, 'Sort', referrerHash, 3]);
        }
    
      })(); // IIFE to not leak global vars
    
      // Have to _trackPageview after custom variable is pushed    
      _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);
      })();
    </script>
    

    Helpful Sources:

    • Async Tracking Developer Documentation by Google
    • Custom Variable Developer Documentation by Google
    • Immediately-Invoked Function Expression (IIFE) by Ben Alman
    0 讨论(0)
提交回复
热议问题