-
Notifications
You must be signed in to change notification settings - Fork 27
/
GA.ts
67 lines (59 loc) · 2.66 KB
/
GA.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
declare var gitVersion:any;
module AliMNS{
export class GA{
public constructor(accId:string){
this._gitMark = gitVersion.branch + "." + gitVersion.rev + "@" + gitVersion.hash;
this._visitor = UA("UA-75293894-6", this.u2id(accId));
}
public send(action:string, value:number, url:string){
if(this._bGoogleAnalytics){
if(this._bAccumulated){
// 累积多个一起发送
this._bAccumulated = false;
var actionPrefixed = this._bAccumulatePrefix + ":" + action;
if(!this._accumulation[actionPrefixed]) this._accumulation[actionPrefixed] = { value: 0, count: 0 };
this._accumulation[actionPrefixed].value += value;
this._accumulation[actionPrefixed].count++;
if(this._accumulation[actionPrefixed].count >= this._accumutionMax){
this.send(actionPrefixed, this._accumulation[actionPrefixed].value, url);
this._accumulation[actionPrefixed].value = 0;
this._accumulation[actionPrefixed].count = 0;
}
}
else{
var args = { dl: url.replace(this._rgxAccId, "//0.") };
// catagory, action, label, value, params
this._visitor.event("AliMNS", action, this._gitMark, value, args).send();
}
}
}
public accumulateNextSend(prefix:string){
this._bAccumulated = true;
this._bAccumulatePrefix = prefix;
}
public disableGA(bDisable?:boolean){
this._bGoogleAnalytics = (!bDisable);
}
private u2id(uid:string){
var cryptoMD5 = CryptoA.createHash("md5");
var md5HEX = cryptoMD5.update(uid).digest("hex");
var uxid = new Array(36);
for(var i=0,j=0;i<md5HEX.length;i++,j++){
if(i === 8 || i === 12 || i === 16 || i === 20){
uxid[j] = "-";
j++;
}
uxid[j] = md5HEX.charAt(i);
}
return uxid.join("");
}
private _visitor: any;
private _gitMark: string;
private _bGoogleAnalytics = true;
private _rgxAccId = /\/\/\w+\./;
private _bAccumulated = false;
private _bAccumulatePrefix = "";
private _accumutionMax = 100;
private _accumulation:any = {};
}
}