forked from akamai/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboomerang.js
571 lines (470 loc) · 13.5 KB
/
boomerang.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright (c) 2011, Yahoo! Inc. All rights reserved.
* Copyright (c) 2012, Log-Normal, Inc. All rights reserved.
* Copyright (c) 2013, SOASTA, Inc. All rights reserved.
* Copyrights licensed under the BSD License. See the accompanying LICENSE.txt file for terms.
*/
/**
\file boomerang.js
boomerang measures various performance characteristics of your user's browsing
experience and beacons it back to your server.
\details
To use this you'll need a web site, lots of users and the ability to do
something with the data you collect. How you collect the data is up to
you, but we have a few ideas.
*/
// Measure the time the script started
// This has to be global so that we don't wait for the entire
// BOOMR function to download and execute before measuring the
// time. We also declare it without `var` so that we can later
// `delete` it. This is the only way that works on Internet Explorer
BOOMR_start = new Date().getTime();
// beaconing section
// the parameter is the window
(function(w) {
// This is the only block where we use document without the w. qualifier
if(w.parent !== w
&& document.getElementById('boomr-if-as')
&& document.getElementById('boomr-if-as').nodeName.toLowerCase() === 'script') {
w = w.parent;
}
var impl, boomr, k, d=w.document;
// Short namespace because I don't want to keep typing BOOMERANG
if(w.BOOMR === undefined) {
w.BOOMR = {};
}
BOOMR = w.BOOMR;
// don't allow this code to be included twice
if(BOOMR.version) {
return;
}
BOOMR.version = "0.9";
BOOMR.window = w;
// impl is a private object not reachable from outside the BOOMR object
// users can set properties by passing in to the init() method
impl = {
// properties
beacon_url: "",
// strip out everything except last two parts of hostname.
// This doesn't work well for domains that end with a country tld,
// but we allow the developer to override site_domain for that.
// You can disable all cookies by setting site_domain to a falsy value
site_domain: w.location.hostname.
replace(/.*?([^.]+\.[^.]+)\.?$/, '$1').
toLowerCase(),
//! User's ip address determined on the server. Used for the BA cookie
user_ip: '',
onloadfired: false,
handlers_attached: false,
events: {
"page_ready": [],
"page_unload": [],
"dom_loaded": [],
"visibility_changed": [],
"before_beacon": [],
"click": []
},
vars: {},
disabled_plugins: {},
onclick_handler: function(ev) {
var target;
if (!ev) { ev = w.event; }
if (ev.target) { target = ev.target; }
else if (ev.srcElement) { target = ev.srcElement; }
if (target.nodeType === 3) {// defeat Safari bug
target = target.parentNode;
}
// don't capture clicks on flash objects
// because of context slowdowns in PepperFlash
if(target && target.nodeName.toUpperCase() === "OBJECT" && target.type === "application/x-shockwave-flash") {
return;
}
impl.fireEvent("click", target);
},
fireEvent: function(e_name, data) {
var i, h, e;
if(!this.events.hasOwnProperty(e_name)) {
return false;
}
e = this.events[e_name];
for(i=0; i<e.length; i++) {
h = e[i];
h[0].call(h[2], data, h[1]);
}
return true;
},
addListener: function(el, sType, fn) {
if(el.addEventListener) {
el.addEventListener(sType, fn, false);
}
else if(el.attachEvent) {
el.attachEvent("on" + sType, fn);
}
}
};
// We create a boomr object and then copy all its properties to BOOMR so that
// we don't overwrite anything additional that was added to BOOMR before this
// was called... for example, a plugin.
boomr = {
t_start: BOOMR_start,
t_end: null,
// Utility functions
utils: {
objectToString: function(o, separator) {
var value = [], k;
if(!o || typeof o !== "object") {
return o;
}
if(separator === undefined) {
separator="\n\t";
}
for(k in o) {
if(Object.prototype.hasOwnProperty.call(o, k)) {
value.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k]));
}
}
return value.join(separator);
},
getCookie: function(name) {
if(!name) {
return null;
}
name = ' ' + name + '=';
var i, cookies;
cookies = ' ' + d.cookie + ';';
if ( (i=cookies.indexOf(name)) >= 0 ) {
i += name.length;
cookies = cookies.substring(i, cookies.indexOf(';', i));
return cookies;
}
return null;
},
setCookie: function(name, subcookies, max_age) {
var value, nameval, c, exp;
if(!name || !impl.site_domain) {
return false;
}
value = this.objectToString(subcookies, "&");
nameval = name + '=' + value;
c = [nameval, "path=/", "domain=" + impl.site_domain];
if(max_age) {
exp = new Date();
exp.setTime(exp.getTime() + max_age*1000);
exp = exp.toGMTString();
c.push("expires=" + exp);
}
if ( nameval.length < 4000 ) {
d.cookie = c.join('; ');
// confirm cookie was set (could be blocked by user's settings, etc.)
return ( value === this.getCookie(name) );
}
return false;
},
getSubCookies: function(cookie) {
var cookies_a,
i, l, kv,
cookies={};
if(!cookie) {
return null;
}
cookies_a = cookie.split('&');
if(cookies_a.length === 0) {
return null;
}
for(i=0, l=cookies_a.length; i<l; i++) {
kv = cookies_a[i].split('=');
kv.push(""); // just in case there's no value
cookies[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
}
return cookies;
},
removeCookie: function(name) {
return this.setCookie(name, {}, 0);
},
pluginConfig: function(o, config, plugin_name, properties) {
var i, props=0;
if(!config || !config[plugin_name]) {
return false;
}
for(i=0; i<properties.length; i++) {
if(config[plugin_name][properties[i]] !== undefined) {
o[properties[i]] = config[plugin_name][properties[i]];
props++;
}
}
return (props>0);
}
},
init: function(config) {
var i, k,
properties = ["beacon_url", "site_domain", "user_ip"];
if(!config) {
config = {};
}
for(i=0; i<properties.length; i++) {
if(config[properties[i]] !== undefined) {
impl[properties[i]] = config[properties[i]];
}
}
if(config.log !== undefined) {
this.log = config.log;
}
if(!this.log) {
this.log = function(/* m,l,s */) { };
}
for(k in this.plugins) {
if(this.plugins.hasOwnProperty(k)) {
// config[pugin].enabled has been set to false
if( config[k]
&& config[k].hasOwnProperty("enabled")
&& config[k].enabled === false
) {
impl.disabled_plugins[k] = 1;
continue;
}
else if(impl.disabled_plugins[k]) {
delete impl.disabled_plugins[k];
}
// plugin exists and has an init method
if(typeof this.plugins[k].init === "function") {
this.plugins[k].init(config);
}
}
}
if(impl.handlers_attached) {
return this;
}
// The developer can override onload by setting autorun to false
if(!impl.onloadfired && (config.autorun === undefined || config.autorun !== false)) {
if(d.readyState && d.readyState === "complete") {
this.setImmediate(BOOMR.page_ready, null, null, BOOMR);
}
else {
if(w.onpagehide || w.onpagehide === null) {
impl.addListener(w, "pageshow", BOOMR.page_ready);
}
else {
impl.addListener(w, "load", BOOMR.page_ready);
}
}
}
impl.addListener(w, "DOMContentLoaded", function() { impl.fireEvent("dom_loaded"); });
(function() {
// visibilitychange is useful to detect if the page loaded through prerender
// or if the page never became visible
// http://www.w3.org/TR/2011/WD-page-visibility-20110602/
// http://www.nczonline.net/blog/2011/08/09/introduction-to-the-page-visibility-api/
var fire_visible = function() { impl.fireEvent("visibility_changed"); };
if(d.webkitVisibilityState) {
impl.addListener(d, "webkitvisibilitychange", fire_visible);
}
else if(d.msVisibilityState) {
impl.addListener(d, "msvisibilitychange", fire_visible);
}
else if(d.visibilityState) {
impl.addListener(d, "visibilitychange", fire_visible);
}
impl.addListener(d, "mouseup", impl.onclick_handler);
if(!w.onpagehide && w.onpagehide !== null) {
// This must be the last one to fire
// We only clear w on browsers that don't support onpagehide because
// those that do are new enough to not have memory leak problems of
// some older browsers
impl.addListener(w, "unload", function() { BOOMR.window=w=null; });
}
}());
impl.handlers_attached = true;
return this;
},
// The page dev calls this method when they determine the page is usable.
// Only call this if autorun is explicitly set to false
page_ready: function() {
if(impl.onloadfired) {
return this;
}
impl.fireEvent("page_ready");
impl.onloadfired = true;
return this;
},
setImmediate: function(fn, data, cb_data, cb_scope) {
var cb = function() {
fn.call(cb_scope || null, data, cb_data || {});
cb=null;
};
if(w.setImmediate) {
w.setImmediate(cb);
}
else if(w.msSetImmediate) {
w.msSetImmediate(cb);
}
else if(w.webkitSetImmediate) {
w.webkitSetImmediate(cb);
}
else if(w.mozSetImmediate) {
w.mozSetImmediate(cb);
}
else {
setTimeout(cb, 10);
}
},
subscribe: function(e_name, fn, cb_data, cb_scope) {
var i, h, e, unload_handler;
if(!impl.events.hasOwnProperty(e_name)) {
return this;
}
e = impl.events[e_name];
// don't allow a handler to be attached more than once to the same event
for(i=0; i<e.length; i++) {
h = e[i];
if(h[0] === fn && h[1] === cb_data && h[2] === cb_scope) {
return this;
}
}
e.push([ fn, cb_data || {}, cb_scope || null ]);
// attaching to page_ready after onload fires, so call soon
if(e_name === 'page_ready' && impl.onloadfired) {
this.setImmediate(fn, null, cb_data, cb_scope);
}
// Attach unload handlers directly to the window.onunload and
// window.onbeforeunload events. The first of the two to fire will clear
// fn so that the second doesn't fire. We do this because technically
// onbeforeunload is the right event to fire, but all browsers don't
// support it. This allows us to fall back to onunload when onbeforeunload
// isn't implemented
if(e_name === 'page_unload') {
unload_handler = function(ev) {
if(fn) {
fn.call(cb_scope, ev || w.event, cb_data);
}
};
// pagehide is for iOS devices
// see http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
if(w.onpagehide || w.onpagehide === null) {
impl.addListener(w, "pagehide", unload_handler);
}
else {
impl.addListener(w, "unload", unload_handler);
}
impl.addListener(w, "beforeunload", unload_handler);
}
return this;
},
addVar: function(name, value) {
if(typeof name === "string") {
impl.vars[name] = value;
}
else if(typeof name === "object") {
var o = name, k;
for(k in o) {
if(o.hasOwnProperty(k)) {
impl.vars[k] = o[k];
}
}
}
return this;
},
removeVar: function(arg0) {
var i, params;
if(!arguments.length) {
return this;
}
if(arguments.length === 1
&& Object.prototype.toString.apply(arg0) === "[object Array]") {
params = arg0;
}
else {
params = arguments;
}
for(i=0; i<params.length; i++) {
if(impl.vars.hasOwnProperty(params[i])) {
delete impl.vars[params[i]];
}
}
return this;
},
sendBeacon: function() {
var k, url, img, nparams=0;
// At this point someone is ready to send the beacon. We send
// the beacon only if all plugins have finished doing what they
// wanted to do
for(k in this.plugins) {
if(this.plugins.hasOwnProperty(k)) {
if(impl.disabled_plugins[k]) {
continue;
}
if(!this.plugins[k].is_complete()) {
return this;
}
}
}
impl.vars.v = BOOMR.version;
impl.vars.u = d.URL.replace(/#.*/, '');
// use d.URL instead of location.href because of a safari bug
if(w !== window) {
impl.vars["if"] = "";
}
// If we reach here, all plugins have completed
impl.fireEvent("before_beacon", impl.vars);
// Don't send a beacon if no beacon_url has been set
// you would do this if you want to do some fancy beacon handling
// in the `before_beacon` event instead of a simple GET request
if(!impl.beacon_url) {
return this;
}
// if there are already url parameters in the beacon url,
// change the first parameter prefix for the boomerang url parameters to &
url = [];
for(k in impl.vars) {
if(impl.vars.hasOwnProperty(k)) {
nparams++;
url.push(encodeURIComponent(k)
+ "="
+ (
impl.vars[k]===undefined || impl.vars[k]===null
? ''
: encodeURIComponent(impl.vars[k])
)
);
}
}
url = impl.beacon_url + ((impl.beacon_url.indexOf('?') > -1)?'&':'?') + url.join('&');
BOOMR.debug("Sending url: " + url.replace(/&/g, "\n\t"));
// only send beacon if we actually have something to beacon back
if(nparams) {
img = new Image();
img.src=url;
}
return this;
}
};
delete BOOMR_start;
(function() {
var make_logger = function(l) {
return function(m, s) {
this.log(m, l, "boomerang" + (s?"."+s:""));
return this;
};
};
boomr.debug = make_logger("debug");
boomr.info = make_logger("info");
boomr.warn = make_logger("warn");
boomr.error = make_logger("error");
}());
if(w.YAHOO && w.YAHOO.widget && w.YAHOO.widget.Logger) {
boomr.log = w.YAHOO.log;
}
else if(w.Y && w.Y.log) {
boomr.log = w.Y.log;
}
else if(typeof console === "object" && console.log !== undefined) {
boomr.log = function(m,l,s) { console.log(s + ": [" + l + "] " + m); };
}
for(k in boomr) {
if(boomr.hasOwnProperty(k)) {
BOOMR[k] = boomr[k];
}
}
BOOMR.plugins = BOOMR.plugins || {};
}(window));
// end of boomerang beaconing section
// Now we start built in plugins.