/
outbound-form-tracker.js
122 lines (104 loc) · 3.98 KB
/
outbound-form-tracker.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
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {delegate, parseUrl} from 'dom-utils';
import provide from '../provide';
import {plugins, trackUsage} from '../usage';
import {assign, createFieldsObj,
getAttributeFields, withTimeout} from '../utilities';
/**
* Class for the `outboundFormTracker` analytics.js plugin.
* @implements {OutboundFormTrackerPublicInterface}
*/
class OutboundFormTracker {
/**
* Registers outbound form tracking.
* @param {!Tracker} tracker Passed internally by analytics.js
* @param {?Object} opts Passed by the require command.
*/
constructor(tracker, opts) {
trackUsage(tracker, plugins.OUTBOUND_FORM_TRACKER);
// Feature detects to prevent errors in unsupporting browsers.
if (!window.addEventListener) return;
/** @type {OutboundFormTrackerOpts} */
const defaultOpts = {
formSelector: 'form',
shouldTrackOutboundForm: this.shouldTrackOutboundForm,
fieldsObj: {},
attributePrefix: 'ga-',
// hitFilter: undefined
};
this.opts = /** @type {OutboundFormTrackerOpts} */ (
assign(defaultOpts, opts));
this.tracker = tracker;
this.delegate = delegate(document, 'submit', this.opts.formSelector,
this.handleFormSubmits.bind(this), {composed: true, useCapture: true});
}
/**
* Handles all submits on form elements. A form submit is considered outbound
* if its action attribute starts with http and does not contain
* location.hostname.
* When the beacon transport method is not available, the event's default
* action is prevented and re-emitted after the hit is sent.
* @param {Event} event The DOM submit event.
* @param {Element} form The delegated event target.
*/
handleFormSubmits(event, form) {
const action = parseUrl(form.action).href;
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
eventCategory: 'Outbound Form',
eventAction: 'submit',
eventLabel: action,
};
if (this.opts.shouldTrackOutboundForm(form, parseUrl)) {
if (!navigator.sendBeacon) {
// Stops the submit and waits until the hit is complete (with timeout)
// for browsers that don't support beacon.
event.preventDefault();
defaultFields.hitCallback = withTimeout(function() {
form.submit();
});
}
const userFields = assign({}, this.opts.fieldsObj,
getAttributeFields(form, this.opts.attributePrefix));
this.tracker.send('event', createFieldsObj(
defaultFields, userFields,
this.tracker, this.opts.hitFilter, form, event));
}
}
/**
* Determines whether or not the tracker should send a hit when a form is
* submitted. By default, forms with an action attribute that starts with
* "http" and doesn't contain the current hostname are tracked.
* @param {Element} form The form that was submitted.
* @param {Function} parseUrlFn A cross-browser utility method for url
* parsing (note: renamed to disambiguate when compiling).
* @return {boolean} Whether or not the form should be tracked.
*/
shouldTrackOutboundForm(form, parseUrlFn) {
const url = parseUrlFn(form.action);
return url.hostname != location.hostname &&
url.protocol.slice(0, 4) == 'http';
}
/**
* Removes all event listeners and instance properties.
*/
remove() {
this.delegate.destroy();
}
}
provide('outboundFormTracker', OutboundFormTracker);