Skip to content

Commit bc93ebe

Browse files
fblundunalexanderdean
authored andcommitted
Unchecked radio buttons and checkboxes now have value null (snowplow#252)
Fixed bug where DOM elements were added to the form event JSONs Fixed bug where not all inner form elements were tracked on form submission event
1 parent 8da1764 commit bc93ebe

2 files changed

Lines changed: 33 additions & 24 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = function(grunt) {
8282
dest: 'src/js/lib_managed/lodash.js',
8383
options: {
8484
exports: 'node',
85-
include: 'isArray, isFunction, isString, isObject, isDate, isUndefined, isNull, map, filter, compact, isEmpty',
85+
include: 'isArray, isFunction, isString, isObject, isDate, isUndefined, isNull, map, filter, find, compact, isEmpty',
8686
flags: ['debug']
8787
}
8888
}

src/js/tracker.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,8 @@
231231
// Will be committed, sent and emptied by a call to trackTrans.
232232
ecommerceTransaction = ecommerceTransactionTemplate(),
233233

234-
// Element types relevant to form tracking
235-
formTrackingNodeNames = {
236-
'INPUT': true,
237-
'TEXTAREA': true,
238-
'SELECT': true
239-
},
234+
// Tag names of mutable elements inside a form
235+
innerElementTags = ['textarea', 'input', 'select'],
240236

241237
outQueueManager = new requestQueue.OutQueueManager(functionName, namespace);
242238

@@ -934,7 +930,12 @@
934930
* Get an identifier for a form, input, textarea, or select element
935931
*/
936932
function getFormElementName(elt) {
937-
return elt.name || elt.id || elt.type || elt.nodeName;
933+
return elt[lodash.find(['name', 'id', 'type', 'nodeName'], function (propName) {
934+
935+
// If elt has a child whose name is "id", that element will be returned
936+
// instead of the actual id of elt unless we ensure that a string is returned
937+
return typeof elt[propName] === 'string';
938+
})];
938939
}
939940

940941
/*
@@ -953,21 +954,29 @@
953954
* Returns a list of the input, textarea, and select elements inside a form along with their values
954955
*/
955956
function getInnerFormElements(elt) {
956-
return lodash.map(lodash.filter(elt.children, function (child) {
957-
958-
// Only include mutable inner elements
959-
return formTrackingNodeNames[child.nodeName.toUpperCase()] && child.type !== 'submit';
960-
}), function (child) {
961-
var elementJson = {
962-
name: getFormElementName(child),
963-
value: child.value,
964-
nodeName: child.nodeName,
965-
};
966-
if (child.type && child.nodeName.toUpperCase() === 'INPUT') {
967-
elementJson.type = child.type;
968-
}
969-
return elementJson;
957+
var innerElements = [];
958+
var formElements = lodash.map(innerElementTags, function (tagname) {
959+
lodash.map(elt.getElementsByTagName(tagname), function (child) {
960+
if (child.type === 'submit') {
961+
return;
962+
}
963+
var elementJson = {
964+
name: getFormElementName(child),
965+
value: child.value,
966+
nodeName: child.nodeName,
967+
};
968+
if (child.type && child.nodeName.toUpperCase() === 'INPUT') {
969+
elementJson.type = child.type;
970+
}
971+
972+
if ((child.type === 'checkbox' || child.type === 'radio') && !child.checked) {
973+
elementJson.value = null;
974+
}
975+
innerElements.push(elementJson);
976+
});
970977
});
978+
979+
return innerElements;
971980
}
972981

973982
/*
@@ -977,7 +986,8 @@
977986
return function (e) {
978987
var elt = e.target;
979988
var type = elt.nodeName.toUpperCase() === 'INPUT' ? elt.type : null;
980-
core.trackFormChange(getParentFormName(elt), getFormElementName(elt), elt.nodeName, type, lodash.map(elt.classList), elt.value, context);
989+
var value = (elt.type === 'checkbox' && !elt.checked) ? null : elt.value;
990+
core.trackFormChange(getParentFormName(elt), getFormElementName(elt), elt.nodeName, type, lodash.map(elt.classList), value, context);
981991
};
982992
}
983993

@@ -997,7 +1007,6 @@
9971007
* Add value change event listeners to all mutable inner form elements
9981008
*/
9991009
function addFormListeners (context) {
1000-
var innerElementTags = ['textarea', 'input', 'select'];
10011010
var trackingMarker = trackerId + 'form';
10021011

10031012
lodash.map(innerElementTags, function (tagname) {

0 commit comments

Comments
 (0)