// from: http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/31752/cn_zh/1462870126706/loghub-tracking.js?spm=a2c4g.11186623.2.16.16a65ad1spF4TQ&file=loghub-tracking.js
/* eslint-disable */
(function(window, document) {
  function createHttpRequest() {
    if (window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
    }
  }
  function AliLogTracker(host, project, logstore) {
    this.uri_ =
      "https://" +
      project +
      "." +
      host +
      "/logstores/" +
      logstore +
      "/track?APIVersion=0.6.0";
    this.params_ = new Array();
    this.httpRequest_ = createHttpRequest();
  }
  AliLogTracker.prototype = {
    push: function(key, value) {
      if (!key || !value) {
        return;
      }
      this.params_.push(key);
      this.params_.push(value);
    },
    logger: function() {
      var url = this.uri_;
      var k = 0;
      while (this.params_.length > 0) {
        if (k % 2 == 0) {
          url += "&" + encodeURIComponent(this.params_.shift());
        } else {
          url += "=" + encodeURIComponent(this.params_.shift());
        }
        ++k;
      }
      try {
        /**
         * 为了不阻塞页面加载，脚本会异步发送HTTP请求，如果页面加载过程中需要多次发送数据，后面的请求会覆盖前面的HTTP请求，
         * 看到的现象是浏览器中会显示Web Tracking请求退出。使用同步发送可以避免该问题，将脚本中的内容进行如下替换，可实现同步发送。
         *
         * https://help.aliyun.com/document_detail/31752.html
         */

        this.httpRequest_.open("GET", url, false);
        this.httpRequest_.send(null);
      } catch (ex) {
        if (
          window &&
          window.console &&
          typeof window.console.log === "function"
        ) {
          console.log(
            "Failed to log to ali log service because of this exception:\n" + ex
          );
          console.log("Failed log data:", url);
        }
      }
    },
  };
  window.Tracker = AliLogTracker;
})(window, document);
