ya.js 3.68 KB
/**
* @fileoverview YAS (YOHO! Acquisition System) YOHO!采集系统的前端js的开发包,
* 用于在网页端采集信息(应用信息,用户信息,用户行为信息,浏览器及其系统信息)。
*/

//工具库
var util = require('./util');

//cookie处理
var cookies = require('./cookie');

//跨域通信
var cross = require('./cross');

//事件处理
var ev = require('./event');

var config = require('./config');

//sdk 版本
var version = config.version;

var yasPath = config.yaPath;

var yasDomain = config.yasDomain;

//应用信息
var appInfo =
{
  h:document.domain,//host
  p:window.location.port,//port
  u:window.location.pathname,//url
  a:window.location.hash,//anchor
  c:window.document.charset,//code
  ft:0,//loadFinishTime
  fst:0,//firstScreenTime
  dt:document.title,//document title
  sv:''// YAS版本
};

//获取跟踪ID
var VisitorId = getVisitorId();

//用户信息
var custInfo =
{
  ak:'',
  cd:VisitorId,//client_id
  vd:(new Date().getTime()+VisitorId),
  ud:'',//uid
  rf:document.referrer//referer
};

//TODO:用户操作信息  
var custOpInfo =
{
  mp:'',//mouse postion
  ev:'',//event
  st:0//scrollTop
};

var flash = util.flashChecker();

//浏览器信息
var browserInfo =
{
  sr:window.screen.width+'x'+window.screen.height,//screenWidthxscreenHeight
  wr:window.screen.availWidth+'x'+window.screen.availHeight,//windowWidthxwindowHeight
  sd:window.screen.colorDepth,//screenDepth
  ln:window.navigator.language,//language
  bv:window.navigator.appVersion,//browserVersion
  sy:window.navigator.platform,//systemInfo
  ce:window.navigator.cookieEnabled,//cookiesEnabled
  fv:flash.f?flash.v:0//flashVersion
};

var eventArray = [];

//入口方法 
var _yas = function(version,tid,uid)
{
 custInfo.ak = tid;
 appInfo.sv = version;
 custInfo.ud = uid?uid:'';
 var beginTime = new Date().getTime();
 var sendFlag = null;

 //loadFinishTime
 ev.ready(function()
 {
    var readyTime = new Date().getTime();
    appInfo.ft = endTime - beginTime;
 });

 //firstScreenTime
 ev.addEventHandler(window,'load',function(e)
 {
    var endTime = new Date().getTime();
    appInfo.fst = endTime - beginTime;
    ev.removeEventHandler(window,'load',arguments.callee);
    window.clearTimeout(sendFlag);
    send();
 });

 sendFlag = window.setTimeout(function()
 {
    appInfo.fst = 0;
    appInfo.fse = '>3s';
    send();
 },3000);

};

function send(callback)
{
  var info = util.merge(appInfo,custInfo);
  info = util.merge(info,browserInfo);
  var param = util.genParam(info);

  callback  =  callback ? callback : function(){};

  cross.imgSend(param, callback);
  
  var _yasev = cookies('_yasev');
  if(_yasev)
  {
    cross.imgSend(_yasev,function()
    {
      cookies('_yasev',null);
    });
  }
}

function getVisitorId ()
{
  var cid = cookies('_yasv');
  if(cid)
  {
  return cid;
  }
  cid = util.Random() ^ util.hashClientInfo() & 2147483647;
  cookies('_yasv',cid,{path:"/",domain:yasDomain,expires:365*200});
  return cid;
}

//超过5次发送自定义信息
var evTimes = 0;

//添加或发送自定义信息
_yas.add = function(trackType,input,action,label,value)
{
 if(evTimes>3)
 {
    cross.imgSend(cookies('_yasev'),function()
    {
      cookies('_yasev',null);
      evTimes = 0;
      _addEv2Cookies(trackType,input,action,label,value);
    });
 }
 else
 {
    _addEv2Cookies(trackType,input,action,label,value);
 }
};

//把自定义事件信息加入cookies
function _addEv2Cookies(trackType,input,action,label,value)
{
  var ev = cookies('_yasev');
  if(ev)
  {
    ev += '&';
  }
  else
  {
    ev = '';
  }
  cookies('_yasev',ev+'vd='+custInfo.vd+'&t='+trackType+'&i='+input+'&l='+label+'&v='+value,{path:"\\",domain:yasDomain,expires:365*200});
  evTimes++;
}

module.exports =_yas;