-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.form-abandonment.js
105 lines (80 loc) · 3.3 KB
/
jquery.form-abandonment.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
/*!
* jquery.form-abandonment.js
* Written by Adam Coard - @AdamCoard
* Original jQuery plugin boilerplate author: @addyosmani
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
var pluginName = "trackEngagement",
defaults = {
abandonmentCateogry: 'abandonment',
abandonmentAction: 'abandonedForm',
abandonedLabel: 'Left form unchanged',
notAbandonedLabel : 'Filled out form',
submitCategory : 'form',
submitAction: 'submit',
//there is no submit label, because the label are the fields in the form modified
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
// We already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.options
if (this.element.tagName.indexOf('form') !== -1){
leftPageWithoutFillingOutAnything(this.element);
submitEvent(this.element);
}
else {
throw new Error('TrackEngagement be called on <form> elements only');
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if ( !$.data(this, "plugin_" + pluginName )) {
$.data( this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};
leftPageWithoutFillingOutAnything = function(element){
var hasFilledOutForm = false;
$(element).find(":input").blur(function(ev){
if (ev.target.type === 'checkbox' && ev.target.checked ){
hasFilledOutForm = true;
}
else if ( $(ev.target).val() ){
hasFilledOutForm = true;
}
});
//Fires right as the page is closing. Should work on most browsers beside Opera.
window.onbeforeunload = function() {
var message = hasFilledOutForm ? options["notAbandonedLabel"] : options["abandonedLabel"];
ga('send', 'event', options['abandonmentCateogry'], options['abandonmentAction'], message, 1);
};
};
submitEvent = function(element){
$(element).submit(function(ev){
ga('send', 'event', 'form', 'submit', 'Form submit', 1);
var formData = $(ev.target).serialize();
//remove the CSRF token from the serialized string
if (formData.indexOf('_token=') !== -1){
formData = formData.slice(formData.indexOf('&'));
}
//Convert the query string to just the parameters
formData = formData.match( /([^&?]*?)[=]/g );
formData = formData.map(function(e){return decodeURI(e);});
formData.forEach(function(e, i){
ga('send', 'event', options['submitCategory'], options['submitAction'], e, 1);
});
});
};
})( jQuery, window, document );