]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/analytics/_base.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / analytics / _base.js
1 if(!dojo._hasResource["dojox.analytics._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.analytics._base"] = true;
3 dojo.provide("dojox.analytics._base");
4
5 dojox.analytics = function(){
6         //where we store data until we're ready to send it off.
7
8         //the data queue;
9         this._data = [] ;
10
11         //id of messages for this session/page
12         this._id=1;
13
14         //some default values
15         this.sendInterval=dojo.config["sendInterval"] || 5000;
16         this.inTransitRetry=dojo.config["inTransitRetry"] || 200;
17         this.dataUrl=dojo.config["analyticsUrl"] || dojo.moduleUrl("dojox.analytics.logger", "dojoxAnalytics.php");
18         this.sendMethod = dojo.config["sendMethod"] || "xhrPost";
19         if (dojo.isIE){
20                 this.maxRequestSize = 2000;
21         }else{
22                 this.maxRequestSize = dojo.config["maxRequestSize"] || 4000;    
23         }
24
25         //while we can go ahead and being logging as soon as this constructor is completed
26         //we're not going to schedule pushing data to the server until after the page
27         //has completed loading
28         dojo.addOnLoad(this, "schedulePusher");
29         dojo.addOnUnload(this, "pushData", true);
30 };
31
32 dojo.extend(dojox.analytics, {
33         schedulePusher: function(interval){
34                 // summary:
35                 //      schedule the data pushing routines to happen in interval ms
36                 setTimeout(dojo.hitch(this, "checkData"), interval||this.sendInterval); 
37         },
38
39         addData: function(dataType, data){
40                 // summary:
41                 //      add data to the queue. Will be pusshed to the server on the next
42                 //      data push
43
44                 if (arguments.length>2){
45                         var d = [];
46                         for(var i=1;i<arguments.length;i++){
47                                 d.push(arguments[i]);
48                         }
49                         data = d;
50                 }
51
52                 this._data.push({plugin: dataType, data: data});
53         },
54
55         checkData: function(){
56                 // summary
57                 if (this._inTransit){
58                         this.schedulePusher(this.inTransitRetry);
59                         return;
60                 }
61
62                 if (this.pushData()){return}
63                 this.schedulePusher();
64         },
65
66         pushData: function(){
67                 // summary
68                 //      pushes data to the server if any exists.  If a push is done, return
69                 //      the deferred after hooking up completion callbacks.  If there is no data
70                 //      to be pushed, return false;
71                 if (this._data.length>0){
72                         //clear the queue
73                         this._inTransit=this._data;
74                         this._data=[];
75                         var def;
76                         switch(this.sendMethod){
77                                 case "script":
78                                         def = dojo.io.script.get({url: this.getQueryPacket(), preventCache: 1, callbackParamName: "callback"});
79                                         break;
80                                 case "xhrPost":
81                                 default:
82                                         console.info("post send: ", this._inTransit);
83                                         def = dojo.xhrPost({url:this.dataUrl, content: {id: this._id++, data: dojo.toJson(this._inTransit)}});
84                                         break;
85                         }
86                         def.addCallback(this, "onPushComplete");
87                         return def;
88                 }
89                 return false;
90         },
91
92         getQueryPacket: function(){
93                 while(true){
94                         var content = {id: this._id++, data: dojo.toJson(this._inTransit)};
95
96                         //FIXME would like a much better way to get the query down to lenght
97                         var query = this.dataUrl + '?' + dojo.objectToQuery(content);
98                         if (query.length>this.maxRequestSize){
99                                 this._data.unshift(this._inTransit.pop());      
100                                 this._split=1;
101                         }else{
102                                 //console.log("script send: ", this._inTransit, " Remaining in queue: ", this._data.length);
103                                 return query;
104                         }       
105                 }
106         },
107
108         onPushComplete: function(results){
109                 // summary
110                 //      if our data push was successfully, remove the _inTransit data and schedule the next
111                 //      parser run.
112                 if (this._inTransit){
113                         delete this._inTransit;
114                 }
115
116                 if (this._data.length>0){
117                         this.schedulePusher(this.inTransitRetry);
118                 } else {
119                         this.schedulePusher();
120                 }
121         }
122 });
123
124 //create the analytics  singleton
125 dojox.analytics = new dojox.analytics();
126
127 }