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

女生的网名这么多〃 提交于 2019-12-31 02:01:28

问题


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 buttons people are clicking before they finally find my site and click through.

For example, the referrer url looks like this - http://www.example.com/page1?content=1234#button1

Is there a way to extract the value after the hashtag (#) so I can tell how people are sorting to find my site? I thought about using document.referrer.location.hashtag but I don't think that works...

I'd ultimately like to import this data into Google Analytics (I can probably do that with a custom variable) but any other tips on how to do that in GA are appreciated.

I appreciate any help with this!


回答1:


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/




回答2:


<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


来源:https://stackoverflow.com/questions/11041148/how-do-you-get-location-hashtag-from-the-referrer-url-google-analytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!