forked from akamai/boomerang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boomerang.js
4794 lines (4209 loc) · 133 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @copyright (c) 2011, Yahoo! Inc. All rights reserved.
* @copyright (c) 2012, Log-Normal, Inc. All rights reserved.
* @copyright (c) 2012-2017, SOASTA, Inc. All rights reserved.
* @copyright (c) 2017-2019, Akamai Technologies, Inc. All rights reserved.
* Copyrights licensed under the BSD License. See the accompanying LICENSE.txt file for terms.
*/
/**
* @class BOOMR
* @desc
* boomerang measures various performance characteristics of your user's browsing
* experience and beacons it back to your server.
*
* 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.
*
* Everything in boomerang is accessed through the `BOOMR` object, which is
* available on `window.BOOMR`. It contains the public API, utility functions
* ({@link BOOMR.utils}) and all of the plugins ({@link BOOMR.plugins}).
*
* Each plugin has its own API, but is reachable through {@link BOOMR.plugins}.
*
* ## Beacon Parameters
*
* The core boomerang object will add the following parameters to the beacon.
*
* Note that each individual {@link BOOMR.plugins plugin} will add its own
* parameters as well.
*
* * `v`: Boomerang version
* * `sv`: Boomerang Loader Snippet version
* * `sm`: Boomerang Loader Snippet method
* * `u`: The page's URL (for most beacons), or the `XMLHttpRequest` URL
* * `n`: The beacon number
* * `pgu`: The page's URL (for `XMLHttpRequest` beacons)
* * `pid`: Page ID (8 characters)
* * `r`: Navigation referrer (from `document.location`)
* * `vis.pre`: `1` if the page transitioned from prerender to visible
* * `vis.st`: Document's visibility state when beacon was sent
* * `vis.lh`: Timestamp when page was last hidden
* * `vis.lv`: Timestamp when page was last visible
* * `xhr.pg`: The `XMLHttpRequest` page group
* * `errors`: Error messages of errors detected in Boomerang code, separated by a newline
* * `rt.si`: Session ID
* * `rt.ss`: Session start timestamp
* * `rt.sl`: Session length (number of pages), can be increased by XHR beacons as well
* * `ua.plt`: `navigator.platform`
* * `ua.vnd`: `navigator.vendor`
*/
/**
* @typedef TimeStamp
* @type {number}
*
* @desc
* A [Unix Epoch](https://en.wikipedia.org/wiki/Unix_time) timestamp (milliseconds
* since 1970) created by [BOOMR.now()]{@link BOOMR.now}.
*
* If `DOMHighResTimeStamp` (`performance.now()`) is supported, it is
* a `DOMHighResTimeStamp` (with microsecond resolution in the fractional),
* otherwise, it is `Date.now()`.
*/
/* BEGIN_DEBUG */
// we don't yet have BOOMR.utils.mark()
if ("performance" in window &&
window.performance &&
typeof window.performance.mark === "function" &&
!window.BOOMR_no_mark) {
window.performance.mark("boomr:startup");
}
/* END_DEBUG */
/**
* @global
* @type {TimeStamp}
* @desc
* This variable is added to the global scope (`window`) until Boomerang loads,
* at which point it is removed.
*
* Timestamp the boomerang.js script started executing.
*
* This has to be global so that we don't wait for this entire
* script 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();
/**
* @function
* @global
* @desc
* This function is added to the global scope (`window`).
*
* Check the value of `document.domain` and fix it if incorrect.
*
* This function is run at the top of boomerang, and then whenever
* {@link BOOMR.init} is called. If boomerang is running within an IFRAME, this
* function checks to see if it can access elements in the parent
* IFRAME. If not, it will fudge around with `document.domain` until
* it finds a value that works.
*
* This allows site owners to change the value of `document.domain` at
* any point within their page's load process, and we will adapt to
* it.
*
* @param {string} domain Domain name as retrieved from page URL
*/
function BOOMR_check_doc_domain(domain) {
/*eslint no-unused-vars:0*/
var test;
/* BEGIN_DEBUG */
// we don't yet have BOOMR.utils.mark()
if ("performance" in window &&
window.performance &&
typeof window.performance.mark === "function" &&
!window.BOOMR_no_mark) {
window.performance.mark("boomr:check_doc_domain");
}
/* END_DEBUG */
if (!window) {
return;
}
// If domain is not passed in, then this is a global call
// domain is only passed in if we call ourselves, so we
// skip the frame check at that point
if (!domain) {
// If we're running in the main window, then we don't need this
if (window.parent === window || !document.getElementById("boomr-if-as")) {
return;// true; // nothing to do
}
if (window.BOOMR && BOOMR.boomerang_frame && BOOMR.window) {
try {
// If document.domain is changed during page load (from www.blah.com to blah.com, for example),
// BOOMR.window.location.href throws "Permission Denied" in IE.
// Resetting the inner domain to match the outer makes location accessible once again
if (BOOMR.boomerang_frame.document.domain !== BOOMR.window.document.domain) {
BOOMR.boomerang_frame.document.domain = BOOMR.window.document.domain;
}
}
catch (err) {
if (!BOOMR.isCrossOriginError(err)) {
BOOMR.addError(err, "BOOMR_check_doc_domain.domainFix");
}
}
}
domain = document.domain;
}
if (!domain || domain.indexOf(".") === -1) {
return;// false; // not okay, but we did our best
}
// window.parent might be null if we're running during unload from
// a detached iframe
if (!window.parent) {
return;
}
// 1. Test without setting document.domain
try {
test = window.parent.document;
return;// test !== undefined; // all okay
}
// 2. Test with document.domain
catch (err) {
try {
document.domain = domain;
}
catch (err2) {
// An exception might be thrown if the document is unloaded
// or when the domain is incorrect. If so, we can't do anything
// more, so bail.
return;
}
}
try {
test = window.parent.document;
return;// test !== undefined; // all okay
}
// 3. Strip off leading part and try again
catch (err) {
domain = domain.replace(/^[\w\-]+\./, "");
}
BOOMR_check_doc_domain(domain);
}
BOOMR_check_doc_domain();
// Construct BOOMR
// w is window
(function(w) {
var impl, boomr, d, createCustomEvent, dispatchEvent, visibilityState, visibilityChange, orig_w = w;
// If the window that boomerang is running in is not top level (ie, we're running in an iframe)
// and if this iframe contains a script node with an id of "boomr-if-as",
// Then that indicates that we are using the iframe loader, so the page we're trying to measure
// is w.parent
//
// Note that we use `document` rather than `w.document` because we're specifically interested in
// the document of the currently executing context rather than a passed in proxy.
//
// The only other place we do this is in `BOOMR.utils.getMyURL` below, for the same reason, we
// need the full URL of the currently executing (boomerang) script.
if (w.parent !== w &&
document.getElementById("boomr-if-as") &&
document.getElementById("boomr-if-as").nodeName.toLowerCase() === "script") {
w = w.parent;
}
d = w.document;
// Short namespace because I don't want to keep typing BOOMERANG
if (!w.BOOMR) {
w.BOOMR = {};
}
BOOMR = w.BOOMR;
// don't allow this code to be included twice
if (BOOMR.version) {
return;
}
/**
* Boomerang version, formatted as major.minor.patchlevel.
*
* This variable is replaced during build (`grunt build`).
*
* @type {string}
*
* @memberof BOOMR
*/
BOOMR.version = "%boomerang_version%";
/**
* The main document window.
* * If Boomerang was loaded in an IFRAME, this is the parent window
* * If Boomerang was loaded inline, this is the current window
*
* @type {Window}
*
* @memberof BOOMR
*/
BOOMR.window = w;
/**
* The Boomerang frame:
* * If Boomerang was loaded in an IFRAME, this is the IFRAME
* * If Boomerang was loaded inline, this is the current window
*
* @type {Window}
*
* @memberof BOOMR
*/
BOOMR.boomerang_frame = orig_w;
/**
* @class BOOMR.plugins
* @desc
* Boomerang plugin namespace.
*
* All plugins should add their plugin object to `BOOMR.plugins`.
*
* A plugin should have, at minimum, the following exported functions:
* * `init(config)`
* * `is_complete()`
*
* See {@tutorial creating-plugins} for details.
*/
if (!BOOMR.plugins) {
BOOMR.plugins = {};
}
// CustomEvent proxy for IE9 & 10 from https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
(function() {
try {
if (new w.CustomEvent("CustomEvent") !== undefined) {
createCustomEvent = function(e_name, params) {
return new w.CustomEvent(e_name, params);
};
}
}
catch (ignore) {
// empty
}
try {
if (!createCustomEvent && d.createEvent && d.createEvent("CustomEvent")) {
createCustomEvent = function(e_name, params) {
var evt = d.createEvent("CustomEvent");
params = params || { cancelable: false, bubbles: false };
evt.initCustomEvent(e_name, params.bubbles, params.cancelable, params.detail);
return evt;
};
}
}
catch (ignore) {
// empty
}
if (!createCustomEvent && d.createEventObject) {
createCustomEvent = function(e_name, params) {
var evt = d.createEventObject();
evt.type = evt.propertyName = e_name;
evt.detail = params.detail;
return evt;
};
}
if (!createCustomEvent) {
createCustomEvent = function() { return undefined; };
}
}());
/**
* Dispatch a custom event to the browser
* @param {string} e_name The custom event name that consumers can subscribe to
* @param {object} e_data Any data passed to subscribers of the custom event via the `event.detail` property
* @param {boolean} async By default, custom events are dispatched immediately.
* Set to true if the event should be dispatched once the browser has finished its current
* JavaScript execution.
*/
dispatchEvent = function(e_name, e_data, async) {
var ev = createCustomEvent(e_name, {"detail": e_data});
if (!ev) {
return;
}
function dispatch() {
try {
if (d.dispatchEvent) {
d.dispatchEvent(ev);
}
else if (d.fireEvent) {
d.fireEvent("onpropertychange", ev);
}
}
catch (e) {
BOOMR.debug("Error when dispatching " + e_name);
}
}
if (async) {
BOOMR.setImmediate(dispatch);
}
else {
dispatch();
}
};
// visibilitychange is useful to detect if the page loaded through prerender
// or if the page never became visible
// https://www.w3.org/TR/2011/WD-page-visibility-20110602/
// https://www.nczonline.net/blog/2011/08/09/introduction-to-the-page-visibility-api/
// https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API
// Set the name of the hidden property and the change event for visibility
if (typeof d.hidden !== "undefined") {
visibilityState = "visibilityState";
visibilityChange = "visibilitychange";
}
else if (typeof d.mozHidden !== "undefined") {
visibilityState = "mozVisibilityState";
visibilityChange = "mozvisibilitychange";
}
else if (typeof d.msHidden !== "undefined") {
visibilityState = "msVisibilityState";
visibilityChange = "msvisibilitychange";
}
else if (typeof d.webkitHidden !== "undefined") {
visibilityState = "webkitVisibilityState";
visibilityChange = "webkitvisibilitychange";
}
// impl is a private object not reachable from outside the BOOMR object.
// Users can set properties by passing in to the init() method.
impl = {
// Beacon URL
beacon_url: "",
// Forces protocol-relative URLs to HTTPS
beacon_url_force_https: true,
// List of string regular expressions that must match the beacon_url. If
// not set, or the list is empty, all beacon URLs are allowed.
beacon_urls_allowed: [],
// Beacon request method, either GET, POST or AUTO. AUTO will check the
// request size then use GET if the request URL is less than MAX_GET_LENGTH
// chars. Otherwise, it will fall back to a POST request.
beacon_type: "AUTO",
// Beacon authorization key value. Most systems will use the 'Authentication'
// keyword, but some some services use keys like 'X-Auth-Token' or other
// custom keys.
beacon_auth_key: "Authorization",
// Beacon authorization token. This is only needed if your are using a POST
// and the beacon requires an Authorization token to accept your data. This
// disables use of the browser sendBeacon() API.
beacon_auth_token: undefined,
// Sends beacons with Credentials (applies to XHR beacons, not IMG or `sendBeacon()`).
// If you need this, you may want to enable `beacon_disable_sendbeacon` as
// `sendBeacon()` does not support credentials.
beacon_with_credentials: false,
// Disables navigator.sendBeacon() support
beacon_disable_sendbeacon: false,
// 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 BW cookie.
user_ip: "",
// Whether or not to send beacons on page load
autorun: true,
// Whether or not we've sent a page load beacon
hasSentPageLoadBeacon: false,
// document.referrer
r: undefined,
// strip_query_string: false,
// onloadfired: false,
// handlers_attached: false,
// waiting_for_config: false,
// All Boomerang cookies will be created with SameSite=Lax by default
same_site_cookie: "Lax",
// All Boomerang cookies will be without Secure attribute by default
secure_cookie: false,
// Sometimes we would like to be able to set the SameSite=None from a Boomerang plugin
forced_same_site_cookie_none: false,
events: {
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when the page is usable by the user.
*
* By default this is fired when `window.onload` fires, but if you
* set `autorun` to false when calling {@link BOOMR.init}, then you
* must explicitly fire this event by calling {@link BOOMR#event:page_ready}.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload}
* @event BOOMR#page_ready
* @property {Event} [event] Event triggering the page_ready
*/
"page_ready": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired just before the browser unloads the page.
*
* The first event of `window.pagehide`, `window.beforeunload`,
* or `window.unload` will trigger this.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Events/pagehide}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload}
* @event BOOMR#page_unload
*/
"page_unload": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired before the document is about to be unloaded.
*
* `window.beforeunload` will trigger this.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload}
* @event BOOMR#before_unload
*/
"before_unload": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired on `document.DOMContentLoaded`.
*
* The `DOMContentLoaded` event is fired when the initial HTML document
* has been completely loaded and parsed, without waiting for stylesheets,
* images, and subframes to finish loading
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded}
* @event BOOMR#dom_loaded
*/
"dom_loaded": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired on `document.visibilitychange`.
*
* The `visibilitychange` event is fired when the content of a tab has
* become visible or has been hidden.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Events/visibilitychange}
* @event BOOMR#visibility_changed
*/
"visibility_changed": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when the `visibilityState` of the document has changed from
* `prerender` to `visible`
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Events/visibilitychange}
* @event BOOMR#prerender_to_visible
*/
"prerender_to_visible": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when a beacon is about to be sent.
*
* The subscriber can still add variables to the beacon at this point,
* either by modifying the `vars` paramter or calling {@link BOOMR.addVar}.
*
* @event BOOMR#before_beacon
* @property {object} vars Beacon variables
*/
"before_beacon": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when a beacon was sent.
*
* The beacon variables cannot be modified at this point. Any calls
* to {@link BOOMR.addVar} or {@link BOOMR.removeVar} will apply to the
* next beacon.
*
* Also known as `onbeacon`.
*
* @event BOOMR#beacon
* @property {object} vars Beacon variables
*/
"beacon": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when the page load beacon has been sent.
*
* This event should only happen once on a page. It does not apply
* to SPA soft navigations.
*
* @event BOOMR#page_load_beacon
* @property {object} vars Beacon variables
*/
"page_load_beacon": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when an XMLHttpRequest has finished, or, if something calls
* {@link BOOMR.responseEnd}.
*
* @event BOOMR#xhr_load
* @property {object} data Event data
*/
"xhr_load": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when the `click` event has happened on the `document`.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick}
* @event BOOMR#click
*/
"click": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when any `FORM` element is submitted.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit}
* @event BOOMR#form_submit
*/
"form_submit": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever new configuration data is applied via {@link BOOMR.init}.
*
* Also known as `onconfig`.
*
* @event BOOMR#config
* @property {object} data Configuration data
*/
"config": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever `XMLHttpRequest.open` is called.
*
* This event will only happen if {@link BOOMR.plugins.AutoXHR} is enabled.
*
* @event BOOMR#xhr_init
* @property {string} type XHR type ("xhr")
*/
"xhr_init": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever a SPA plugin is about to track a new navigation.
*
* @event BOOMR#spa_init
* @property {string} navType Navigation type (`spa` or `spa_hard`)
* @property {object} param SPA navigation parameters
*/
"spa_init": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever a SPA navigation is complete.
*
* @event BOOMR#spa_navigation
*/
"spa_navigation": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever a SPA navigation is cancelled.
*
* @event BOOMR#spa_cancel
*/
"spa_cancel": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever `XMLHttpRequest.send` is called.
*
* This event will only happen if {@link BOOMR.plugins.AutoXHR} is enabled.
*
* @event BOOMR#xhr_send
* @property {object} xhr `XMLHttpRequest` object
*/
"xhr_send": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever and `XMLHttpRequest` has an error (if its `status` is
* set).
*
* This event will only happen if {@link BOOMR.plugins.AutoXHR} is enabled.
*
* Also known as `onxhrerror`.
*
* @event BOOMR#xhr_error
* @property {object} data XHR data
*/
"xhr_error": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever a page error has happened.
*
* This event will only happen if {@link BOOMR.plugins.Errors} is enabled.
*
* Also known as `onerror`.
*
* @event BOOMR#error
* @property {object} err Error
*/
"error": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever connection information changes via the
* Network Information API.
*
* This event will only happen if {@link BOOMR.plugins.Mobile} is enabled.
*
* @event BOOMR#netinfo
* @property {object} connection `navigator.connection`
*/
"netinfo": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired whenever a Rage Click is detected.
*
* This event will only happen if {@link BOOMR.plugins.Continuity} is enabled.
*
* @event BOOMR#rage_click
* @property {Event} e Event
*/
"rage_click": [],
/**
* Boomerang event, subscribe via {@link BOOMR.subscribe}.
*
* Fired when an early beacon is about to be sent.
*
* The subscriber can still add variables to the early beacon at this point
* by calling {@link BOOMR.addVar}.
*
* This event will only happen if {@link BOOMR.plugins.Early} is enabled.
*
* @event BOOMR#before_early_beacon
* @property {object} data Event data
*/
"before_early_beacon": []
},
/**
* Public events
*/
public_events: {
/**
* Public event (fired on `document`), and can be subscribed via
* `document.addEventListener("onBeforeBoomerangBeacon", ...)` or
* `document.attachEvent("onpropertychange", ...)`.
*
* Maps to {@link BOOMR#event:before_beacon}
*
* @event document#onBeforeBoomerangBeacon
* @property {object} vars Beacon variables
*/
"before_beacon": "onBeforeBoomerangBeacon",
/**
* Public event (fired on `document`), and can be subscribed via
* `document.addEventListener("onBoomerangBeacon", ...)` or
* `document.attachEvent("onpropertychange", ...)`.
*
* Maps to {@link BOOMR#event:before_beacon}
*
* @event document#onBoomerangBeacon
* @property {object} vars Beacon variables
*/
"beacon": "onBoomerangBeacon",
/**
* Public event (fired on `document`), and can be subscribed via
* `document.addEventListener("onBoomerangLoaded", ...)` or
* `document.attachEvent("onpropertychange", ...)`.
*
* Fired when {@link BOOMR} has loaded and can be used.
*
* @event document#onBoomerangLoaded
*/
"onboomerangloaded": "onBoomerangLoaded"
},
/**
* Maps old event names to their updated name
*/
translate_events: {
"onbeacon": "beacon",
"onconfig": "config",
"onerror": "error",
"onxhrerror": "xhr_error"
},
/**
* Number of page_unload or before_unload callbacks registered
*/
unloadEventsCount: 0,
/**
* Number of page_unload or before_unload callbacks called
*/
unloadEventCalled: 0,
listenerCallbacks: {},
vars: {},
singleBeaconVars: {},
/**
* Variable priority lists:
* -1 = first
* 1 = last
*/
varPriority: {
"-1": {},
"1": {}
},
errors: {},
disabled_plugins: {},
localStorageSupported: false,
LOCAL_STORAGE_PREFIX: "_boomr_",
/**
* Native functions that were overwritten and should be restored when
* the Boomerang IFRAME is unloaded
*/
nativeOverwrites: [],
xb_handler: function(type) {
return 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 events on flash objects
// because of context slowdowns in PepperFlash
if (target &&
target.nodeName &&
target.nodeName.toUpperCase() === "OBJECT" &&
target.type === "application/x-shockwave-flash") {
return;
}
impl.fireEvent(type, target);
};
},
clearEvents: function() {
var eventName;
for (eventName in this.events) {
if (this.events.hasOwnProperty(eventName)) {
this.events[eventName] = [];
}
}
},
clearListeners: function() {
var type, i;
for (type in impl.listenerCallbacks) {
if (impl.listenerCallbacks.hasOwnProperty(type)) {
// remove all callbacks -- removeListener is guaranteed
// to remove the element we're calling with
while (impl.listenerCallbacks[type].length) {
BOOMR.utils.removeListener(
impl.listenerCallbacks[type][0].el,
type,
impl.listenerCallbacks[type][0].fn);
}
}
}
impl.listenerCallbacks = {};
},
fireEvent: function(e_name, data) {
var i, handler, handlers, handlersLen;
e_name = e_name.toLowerCase();
/* BEGIN_DEBUG */
BOOMR.utils.mark("fire_event");
BOOMR.utils.mark("fire_event:" + e_name + ":start");
/* END_DEBUG */
// translate old names
if (this.translate_events[e_name]) {
e_name = this.translate_events[e_name];
}
if (!this.events.hasOwnProperty(e_name)) {
return;// false;
}
if (this.public_events.hasOwnProperty(e_name)) {
dispatchEvent(this.public_events[e_name], data);
}
handlers = this.events[e_name];
// Before we fire any event listeners, let's call real_sendBeacon() to flush
// any beacon that is being held by the setImmediate.
if (e_name !== "before_beacon" && e_name !== "beacon" && e_name !== "before_early_beacon") {
BOOMR.real_sendBeacon();
}
// only call handlers at the time of fireEvent (and not handlers that are
// added during this callback to avoid an infinite loop)
handlersLen = handlers.length;
for (i = 0; i < handlersLen; i++) {
try {
handler = handlers[i];
handler.fn.call(handler.scope, data, handler.cb_data);
}
catch (err) {
BOOMR.addError(err, "fireEvent." + e_name + "<" + i + ">");
}
}
// remove any 'once' handlers now that we've fired all of them
for (i = 0; i < handlersLen; i++) {
if (handlers[i].once) {
handlers.splice(i, 1);
handlersLen--;
i--;
}
}
/* BEGIN_DEBUG */
BOOMR.utils.mark("fire_event:" + e_name + ":end");
BOOMR.utils.measure(
"fire_event:" + e_name,
"fire_event:" + e_name + ":start",
"fire_event:" + e_name + ":end");
/* END_DEBUG */
return;// true;
},
spaNavigation: function() {
// a SPA navigation occured, force onloadfired to true
impl.onloadfired = true;
},
/**
* Determines whether a beacon URL is allowed based on
* `beacon_urls_allowed` config
*
* @param {string} url URL to test
*
*/
beaconUrlAllowed: function(url) {
if (!impl.beacon_urls_allowed || impl.beacon_urls_allowed.length === 0) {
return true;
}
for (var i = 0; i < impl.beacon_urls_allowed.length; i++) {
var regEx = new RegExp(impl.beacon_urls_allowed[i]);
if (regEx.exec(url)) {
return true;
}
}
return false;
},
/**
* Checks browser for localStorage support
*/
checkLocalStorageSupport: function() {
var name = impl.LOCAL_STORAGE_PREFIX + "clss";
impl.localStorageSupported = false;
// Browsers with cookies disabled or in private/incognito mode may throw an
// error when accessing the localStorage variable
try {
// we need JSON and localStorage support
if (!w.JSON || !w.localStorage) {
return;
}
w.localStorage.setItem(name, name);
impl.localStorageSupported = (w.localStorage.getItem(name) === name);
w.localStorage.removeItem(name);
}
catch (ignore) {
impl.localStorageSupported = false;
}