Skip to content

Commit

Permalink
Updated Ahoy.js to 0.3.8 [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Dec 4, 2020
1 parent ffe0110 commit 5ba9c70
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added `instance` method
- Added `request` argument to `user_method`
- Updated Ahoy.js to 0.3.8
- Removed `exclude_method` call when geocoding

## 3.0.5 (2020-09-09)
Expand Down
129 changes: 111 additions & 18 deletions vendor/assets/javascripts/ahoy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Ahoy.js
* Simple, powerful JavaScript analytics
* https://github.com/ankane/ahoy.js
* v0.3.6
* v0.3.8
* MIT License
*/

Expand All @@ -12,7 +12,96 @@
(global = global || self, global.ahoy = factory());
}(this, (function () { 'use strict';

var n=function(n){return void 0===n},e=function(n){return Array.isArray(n)},t=function(n){return n&&"number"==typeof n.size&&"string"==typeof n.type&&"function"==typeof n.slice},s=function(o,i,r,f){return (i=i||{}).indices=!n(i.indices)&&i.indices,i.nullsAsUndefineds=!n(i.nullsAsUndefineds)&&i.nullsAsUndefineds,i.booleansAsIntegers=!n(i.booleansAsIntegers)&&i.booleansAsIntegers,r=r||new FormData,n(o)?r:(null===o?i.nullsAsUndefineds||r.append(f,""):"boolean"!=typeof o?e(o)?o.length&&o.forEach(function(n,e){s(n,i,r,f+"["+(i.indices?e:"")+"]");}):o instanceof Date?r.append(f,o.toISOString()):o!==Object(o)||function(n){return t(n)&&"string"==typeof n.name&&("object"==typeof n.lastModifiedDate||"number"==typeof n.lastModified)}(o)||t(o)?r.append(f,o):Object.keys(o).forEach(function(n){var t=o[n];if(e(t)){ for(;n.length>2&&n.lastIndexOf("[]")===n.length-2;){ n=n.substring(0,n.length-2); } }s(t,i,r,f?f+"["+n+"]":n);}):r.append(f,i.booleansAsIntegers?o?1:0:o),r)};
var isUndefined = function (value) { return value === undefined; };

var isNull = function (value) { return value === null; };

var isBoolean = function (value) { return typeof value === 'boolean'; };

var isObject = function (value) { return value === Object(value); };

var isArray = function (value) { return Array.isArray(value); };

var isDate = function (value) { return value instanceof Date; };

var isBlob = function (value) { return value &&
typeof value.size === 'number' &&
typeof value.type === 'string' &&
typeof value.slice === 'function'; };

var isFile = function (value) { return isBlob(value) &&
typeof value.name === 'string' &&
(typeof value.lastModifiedDate === 'object' ||
typeof value.lastModified === 'number'); };

var serialize = function (obj, cfg, fd, pre) {
cfg = cfg || {};

cfg.indices = isUndefined(cfg.indices) ? false : cfg.indices;

cfg.nullsAsUndefineds = isUndefined(cfg.nullsAsUndefineds)
? false
: cfg.nullsAsUndefineds;

cfg.booleansAsIntegers = isUndefined(cfg.booleansAsIntegers)
? false
: cfg.booleansAsIntegers;

cfg.allowEmptyArrays = isUndefined(cfg.allowEmptyArrays)
? false
: cfg.allowEmptyArrays;

fd = fd || new FormData();

if (isUndefined(obj)) {
return fd;
} else if (isNull(obj)) {
if (!cfg.nullsAsUndefineds) {
fd.append(pre, '');
}
} else if (isBoolean(obj)) {
if (cfg.booleansAsIntegers) {
fd.append(pre, obj ? 1 : 0);
} else {
fd.append(pre, obj);
}
} else if (isArray(obj)) {
if (obj.length) {
obj.forEach(function (value, index) {
var key = pre + '[' + (cfg.indices ? index : '') + ']';

serialize(value, cfg, fd, key);
});
} else if (cfg.allowEmptyArrays) {
fd.append(pre + '[]', '');
}
} else if (isDate(obj)) {
fd.append(pre, obj.toISOString());
} else if (isObject(obj) && !isFile(obj) && !isBlob(obj)) {
Object.keys(obj).forEach(function (prop) {
var value = obj[prop];

if (isArray(value)) {
while (prop.length > 2 && prop.lastIndexOf('[]') === prop.length - 2) {
prop = prop.substring(0, prop.length - 2);
}
}

var key = pre ? pre + '[' + prop + ']' : prop;

serialize(value, cfg, fd, key);
});
} else {
fd.append(pre, obj);
}

return fd;
};

var index_module = {
serialize: serialize,
};
var index_module_1 = index_module.serialize;

// https://www.quirksmode.org/js/cookies.html

Expand Down Expand Up @@ -146,17 +235,23 @@
element.webkitMatchesSelector;

if (matches) {
return matches.apply(element, [selector]);
if (matches.apply(element, [selector])) {
return element;
} else if (element.parentElement) {
return matchesSelector(element.parentElement, selector)
}
return null;
} else {
log("Unable to match");
return false;
return null;
}
}

function onEvent(eventName, selector, callback) {
document.addEventListener(eventName, function (e) {
if (matchesSelector(e.target, selector)) {
callback(e);
var matchedElement = matchesSelector(e.target, selector);
if (matchedElement) {
callback.call(matchedElement, e);
}
});
}
Expand Down Expand Up @@ -275,7 +370,7 @@
// stringify so we keep the type
data.events_json = JSON.stringify(data.events);
delete data.events;
window.navigator.sendBeacon(eventsUrl(), s(data));
window.navigator.sendBeacon(eventsUrl(), index_module_1(data));
});
}

Expand All @@ -299,13 +394,12 @@
}

function eventProperties(e) {
var target = e.target;
return cleanObject({
tag: target.tagName.toLowerCase(),
id: presence(target.id),
"class": presence(target.className),
tag: this.tagName.toLowerCase(),
id: presence(this.id),
"class": presence(this.className),
page: page(),
section: getClosestSection(target)
section: getClosestSection(this)
});
}

Expand Down Expand Up @@ -465,24 +559,23 @@

ahoy.trackClicks = function () {
onEvent("click", "a, button, input[type=submit]", function (e) {
var target = e.target;
var properties = eventProperties(e);
properties.text = properties.tag == "input" ? target.value : (target.textContent || target.innerText || target.innerHTML).replace(/[\s\r\n]+/g, " ").trim();
properties.href = target.href;
var properties = eventProperties.call(this, e);
properties.text = properties.tag == "input" ? this.value : (this.textContent || this.innerText || this.innerHTML).replace(/[\s\r\n]+/g, " ").trim();
properties.href = this.href;
ahoy.track("$click", properties);
});
};

ahoy.trackSubmits = function () {
onEvent("submit", "form", function (e) {
var properties = eventProperties(e);
var properties = eventProperties.call(this, e);
ahoy.track("$submit", properties);
});
};

ahoy.trackChanges = function () {
onEvent("change", "input, textarea, select", function (e) {
var properties = eventProperties(e);
var properties = eventProperties.call(this, e);
ahoy.track("$change", properties);
});
};
Expand Down

0 comments on commit 5ba9c70

Please sign in to comment.