js追踪脚本

js 追踪脚本
lk_t.js
var LK = {
  version:"1.0",
  host:"",
  key:"LK",
  expire_days:3600,
  cmds:[],
  tracker:function()
  {
    var that = this.tracker;
    that.timestamp = this.getTimestamp();
    that.prop = {};
    //发送请求
    that.log = function (param){
      var img = new Image(1, 1);
      img.onLoad = function (){};
      img. LK.buildUrl(param);
    };
  },
  //处理命令队列
  submit:function()
  {
    this.tracker();
    for(var i in this.cmds)
    {
      line = this.cmds[i];
       //call和apply,它们的作用都是将函数绑定到另外一个对象上去运行
      //Array.prototype.slice这句就是访问Array的内置方法
      var args = Array.prototype.slice.call(line, 1);
      if(!window[this.key])
        window[this.key] = this;
      window[this.key][line[0]].apply(this.tracker, args);
    }
  },
  setSiteId:function(siteid)
  {
    this.siteid = siteid;
  },
  //跟踪PV
  trackPageView:function()
  {
    this.log('');
  },
  //订单数据
  addTransaction:function (order_id, order_source, total, tax, shipping, gateway, city, state, country)
  {
    if(!this.prop.trans)
      this.prop.trans = {};
    
    var trans = {};
    trans.order_id = order_id;
    trans.order_source = encodeURIComponent(order_source);
    trans.total = total;
    trans.tax = tax;
    trans.shipping = shipping;
    trans.gateway = encodeURIComponent(gateway);
    trans.city = encodeURIComponent(city);
    trans.state = encodeURIComponent(state);
    trans.country = encodeURIComponent(country);
    trans.items = [];
    this.prop.trans[order_id] = trans;    
  },
  //产品数据
  addTransactionLineItem:function (order_id, sku, product_name, category, unit_price, quantity)
  {
    var li = {};
    li.li_order_id = order_id;
    li.li_sku = encodeURIComponent(sku);
    li.li_product_name = encodeURIComponent(product_name);
    li.li_category = encodeURIComponent(category);
    li.li_unit_price = encodeURIComponent(unit_price);
    li.li_quantity = quantity;
    var order = this.prop.trans[order_id];
    var items = order['items'];
    items.push(li);
    order['items'] = items;
    this.prop.trans[order_id] = order;
  },
  trackTransaction:function()
  {
    var param = "";
    for(var o in this.prop.trans)
    {
      for(var k in this.prop.trans[o])
      {
        var v = this.prop.trans[o][k];
        if(typeof v === 'object')
          for(var t in v)
            for(ta in v[t])
              param += "&" + ta + "=" + v[t][ta];
        else
          param += "&" + k + "=" + v;
      }
    }
    this.log(param);
  },
  //构建URL
  buildUrl:function (param)
  {
    var that = this.tracker;
    var url = "";
    url += this.host;
    url += "?siteid=" + that.siteid;
    url += "&visitor_id=" + this.getVisitorId();
    url += "&page_title=" + this.getPageTitle();
    url += "&tourl=" + this.getUrl();
    url += "&referrer=" + this.getReferrer();
    url += param;
    url += "&version=" + this.version;
    return url;
  },
  setCookie:function (name, value, days, path, domain, secure)
  {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var domain_arr = location.hostname.split('.');
    
    if(domain_arr.length == 2)
      domain = location.hostname;
    else    
    {
      domain_arr.shift();
      domain = "." + domain_arr.join('.');
    }    
    document.cookie = name + "=" + escape(value) +
      ((days) ? "; expires=" + date.toGMTString() : "") +
      "; path=/"    +
      "; domain=" + domain +
      ((secure) ? "; secure" : "");
  },
  readAllCookies : function ()
  {
    var jar = {};
    var ca = document.cookie.split(';');
    if (ca) {
      for (var i = 0; i < ca.length; i++) {
        var cat = this.util.trim(ca[i]);
        var pos = this.util.strpos(cat, '=');
        var key = cat.substring(0, pos);
        var value = cat.substring(pos + 1, cat.length);
        if (!jar.hasOwnProperty(key)) {
          jar[key] = [];
        }
        jar[key].push(value);
      }
      return jar;
    }
  },
  getCookie : function (name)    
  {
    var jar = this.readAllCookies();
    if (jar) {
      if (jar.hasOwnProperty(name)) {
        return jar[name][0];
      } else {
        return "";
      }
    }
  },
  getTimestamp:function()
  {
    return Math.round(new Date().getTime() / 1000);
  },
  getVisitorId:function()
  {
    
    var visitorId = this.getCookie("lk_vuid");
    if(visitorId)
    {
      return visitorId;
    }
    else
    {
      var time = new Date().getTime();
      var random = Math.round(Math.random() * 1000000);
      visitorId = time + "" + random;
      this.setVisitorId(visitorId);
      return visitorId;
    }
  },
  setVisitorId:function(visitorId)
  {
    this.setCookie('lk_vuid', visitorId, this.expire_days);
  },
  getPageTitle:function()
  {
    try {
                                var t = document.getElementsByTagName("title");
                                return encodeURIComponent(t[0].text);
                } catch (e) {return "";}
    //var t = document.getElementsByTagName("title");
    //return t == null ? encodeURIComponent(t[0].text) : '';
  },
  getUrl:function()
  {
    //访问 URL
    var tourl = location.href;
    return encodeURIComponent(tourl);
  },
  getReferrer:function()
  {
    //来源URL
    var referrer = document.referrer;
    return encodeURIComponent(referrer);
  },
  util:{
    trim:function(str)
    {
      return str.replace(/(^\s+)|(\s+$)/, '');
    },
    strpos:function(haystack, needle, offset)
    {
      var i = (haystack + '').indexOf(needle, (offset || 0));
      return i === -1 ? false : i;
    }
  }
};

(function(){
  var lk = LK;
  lk.host = "http://192.168.3.250/__lk.gif";
  if(lk_cmds)
    lk.cmds = lk_cmds;
    
  lk.submit();
})();    
 
其中
//处理命令队列 
  
submit:function()    
    {    
        this.tracker();    
        for(var i in this.cmds)    
        {    
            line = this.cmds[i];    
            //Array.prototype.slice这句就是访问Array的内置方法    
            var args = Array.prototype.slice.call(line, 1);    
            if(!window[this.key])    
                window[this.key] = this;    
            window[this.key][line[0]].apply(this.tracker, args);    
        }    
 
Array.prototype.slice这句就是访问Array的内置方法
 
相关信息查看  JS 有趣的应用 : Array.prototype.slice.call(arguments,0)
 
基本统计代码:


 
订单统计代码
下面红色标识部分是您需要针对自己的订单进行修改的部分,参数含义如下
订单数据addTransaction: order_id(订单号), order_source(订单来源), total(订单金额[总和]), tax(税), shipping(运费), gateway(途径), city(城市), state(省), country(国家)
产品数据addTransactionLineItem: order_id(订单号), sku(产品SKU[品牌]), product_name(产品名称), category(各类,类别), unit_price(单价), quantity(购买产品数量)
 



 
 
使用示例:
利用Javasctipt方式进行跟踪, 复制以下代码,粘贴到您的HTML页面中用来跟踪您的网站页面的流量。Javascript 跟踪API 跟踪您的网站和网页流量






当前标题:js追踪脚本
本文路径:http://cdiso.cn/article/iehoig.html

其他资讯