1
+ /**
2
+ * form Bundle
3
+ *
4
+ * @author Dominik Matt <[email protected] >
5
+ */
6
+ trackingJS . prototype . eventBundles . form = function ( ) {
7
+ this . bundleName = 'form' ;
8
+
9
+ /**
10
+ * @method init
11
+ *
12
+ * @param tracking
13
+ */
14
+ this . init = function init ( t ) {
15
+ tracking = t ;
16
+ bindEvents ( ) ;
17
+ } ;
18
+
19
+ /**
20
+ * @method bindDomEvents
21
+ */
22
+ var bindEvents = function ( ) {
23
+ $ ( document ) . delegate ( 'form' , 'submit' , formSendHandler ) ;
24
+ } ;
25
+
26
+ /**
27
+ * @method formSendHandler
28
+ *
29
+ * @param {jQuery } event
30
+ * @returns {boolean }
31
+ */
32
+ var formSendHandler = function ( event ) {
33
+ var $form = $ ( event . currentTarget ) ,
34
+ data = getFormData ( $form ) ;
35
+
36
+ trackNewsletterHandler ( $form ) ;
37
+ tracking . event ( data . name , data . name + ' - Send success' , 'Form: ' + data . name + ' send success' ) ;
38
+ } ;
39
+
40
+ /**
41
+ * @method trackNewsletterHandler
42
+ *
43
+ * @type {function(this:trackingJS.prototype.eventBundles) }
44
+ */
45
+ var trackNewsletterHandler = function ( $form ) {
46
+ var $nlEl = $form . find ( '[data-form-newsletter]:checked' ) ,
47
+ data = getFormData ( $form ) ;
48
+
49
+ if ( $nlEl . length > 0 ) {
50
+ $nlEl . each ( function ( key , item ) {
51
+ var $el = $ ( item ) ,
52
+ name = $el . data ( 'form-newsletter-name' ) ;
53
+
54
+ if ( ! name || name == '' ) name = data . name ;
55
+
56
+ this . newsletter . signup ( name ) ;
57
+ } . bind ( this ) ) ;
58
+ }
59
+ } . bind ( this ) ;
60
+
61
+ /**
62
+ * @method sendFailed
63
+ *
64
+ * @param $form
65
+ */
66
+ this . sendFailed = function ( $form ) {
67
+ var data = getFormData ( $form ) ;
68
+
69
+ tracking . event ( data . name , data . name + ' - Send failed' , 'Form: ' + data . name + ' send failed' ) ;
70
+ } ;
71
+
72
+ /**
73
+ * @constructor
74
+ * @type {{signup: Function} }
75
+ */
76
+ this . newsletter = {
77
+ /**
78
+ * @method signup
79
+ *
80
+ * @param name
81
+ * @param callback
82
+ */
83
+ signup : function ( name , callback ) {
84
+ if ( typeof callback != 'function' ) callback = function ( ) { }
85
+ tracking . event ( 'Newsletter' , 'Newsletter - Sign-Up' , 'Newsletter: ' + name + ' sign-up success' , null , callback ) ;
86
+ } ,
87
+
88
+ /**
89
+ * @method signupFailed
90
+ *
91
+ * @param name
92
+ * @param callback
93
+ */
94
+ signupFailed : function ( name , callback ) {
95
+ if ( typeof callback != 'function' ) callback = function ( ) { }
96
+ tracking . event ( 'Newsletter' , 'Newsletter - Sign-Up' , 'Newsletter: ' + name + ' sign-up failed' , null , callback ) ;
97
+ } ,
98
+
99
+ /**
100
+ * @method signoff
101
+ *
102
+ * @param name
103
+ * @param callback
104
+ */
105
+ signoff : function ( name , callback ) {
106
+ if ( typeof callback != 'function' ) callback = function ( ) { }
107
+ tracking . event ( 'Newsletter' , 'Newsletter - Sign-Off' , 'Newsletter: ' + name + ' sign-off' , null , callback ) ;
108
+ }
109
+ } ;
110
+
111
+ /**
112
+ * @method getFormData
113
+ *
114
+ * @param $form
115
+ * @returns {{name: *} }
116
+ */
117
+ var getFormData = function ( $form ) {
118
+ var data = {
119
+ name : $form . data ( 'form-name' )
120
+ } ;
121
+
122
+ return data ;
123
+ }
124
+
125
+ } ;
0 commit comments