hitCallback无法使用Disconnect,Ghostery等

时间:2014-09-26 06:15:25

标签: universal-analytics event-tracking

我在html中跟随代码:

 <a href="/register/" onclick="var href=this.href;ga('send','event',{'eventCategory':'Registration','eventAction':'clickRegister','hitCallback':function(){document.location=href;}});return false;">Sign up</a>

它无效,然后在Chrome或其他浏览器中安装Disconnect或类似的插件。如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我使用了Universal Analytics代码的解码版本,并在其中调用了hitCallback:

<!-- Google Analytics -->
<script>
  /**
   * Creates a temporary global ga object and loads analy  tics.js.
   * Paramenters o, a, and m are all used internally.  They could have been declared using 'var',
   * instead they are declared as parameters to save 4 bytes ('var ').
   *
   * @param {Window}      i The global context object.
   * @param {Document}    s The DOM document object.
   * @param {string}      o Must be 'script'.
   * @param {string}      g URL of the analytics.js script. Inherits protocol from page.
   * @param {string}      r Global name of analytics object.  Defaults to 'ga'.
   * @param {DOMElement?} a Async script tag.
   * @param {DOMElement?} m First script tag in document.
   */
  (function(window, document, strScript, url, variableName, scriptElement, firstScript) {
    window['GoogleAnalyticsObject'] = variableName; // Acts as a pointer to support renaming.

    // Creates an initial ga() function.  The queued commands will be executed once analytics.js loads.
    window[variableName] = window[variableName] || function() {
      (window[variableName].q = window[variableName].q || []).push(arguments);

      // If user uses Disconnect, Ghostery, DoNotTrackMe or similar plugin we shoud make hitCallback to work
      if(typeof arguments[2] == "object" && typeof arguments[2].hitCallback == "function") {
        arguments[2].hitCallback();
      } else if (typeof arguments[5] == "object" && typeof arguments[5].hitCallback == "function") {
        arguments[5].hitCallback();
      }
    };

    // Sets the time (as an integer) this tag was executed.  Used for timing hits.
    window[variableName].l = 1 * new Date();

    // Insert the script tag asynchronously.  Inserts above current tag to prevent blocking in
    // addition to using the async attribute.
    scriptElement = document.createElement(strScript),
    firstScript = document.getElementsByTagName(strScript)[0];
    scriptElement.async = 1;
    scriptElement.src = url;
    firstScript.parentNode.insertBefore(scriptElement, firstScript)
  })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

  ga('create', 'UA-XXXXXXXX-1', 'auto'); // Creates the tracker with default parameters.
  ga('send', 'pageview'); // Sends a pageview hit.
</script>
<!-- End Google Analytics -->

答案 1 :(得分:0)

以下文章详细讨论了这个问题:

http://veithen.github.io/2015/01/24/outbound-link-tracking.html