Skip to content

Commit

Permalink
Update eventTracker to use TrackerQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Aug 18, 2018
1 parent e613e54 commit be29604
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/plugins/event-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import {delegate} from 'dom-utils';
import provide from '../provide';
import TrackerQueue from '../tracker-queue';
import {plugins, trackUsage} from '../usage';
import {assign, createFieldsObj, getAttributeFields} from '../utilities';
import {assign, createFieldsObj, getAttributeFields, now} from '../utilities';


/**
Expand All @@ -34,9 +35,6 @@ class EventTracker {
constructor(tracker, opts) {
trackUsage(tracker, plugins.EVENT_TRACKER);

// Feature detects to prevent errors in unsupporting browsers.
if (!window.addEventListener) return;

/** @type {EventTrackerOpts} */
const defaultOpts = {
events: ['click'],
Expand All @@ -46,20 +44,19 @@ class EventTracker {
};

this.opts = /** @type {EventTrackerOpts} */ (assign(defaultOpts, opts));

this.tracker = tracker;

// Binds methods.
this.handleEvents = this.handleEvents.bind(this);

const selector = '[' + this.opts.attributePrefix + 'on]';

// Creates a mapping of events to their delegates
this.delegates = {};
this.opts.events.forEach((event) => {
this.delegates[event] = delegate(document, event, selector,
this.handleEvents, {composed: true, useCapture: true});
});

this.queue = TrackerQueue.getOrCreate(tracker);
}

/**
Expand All @@ -68,26 +65,33 @@ class EventTracker {
* @param {Element} element The delegated DOM element target.
*/
handleEvents(event, element) {
const prefix = this.opts.attributePrefix;
const events = element.getAttribute(prefix + 'on').split(/\s*,\s*/);
this.queue.add(({time}) => {
const prefix = this.opts.attributePrefix;
const events = element.getAttribute(prefix + 'on').split(/\s*,\s*/);

// Ensures the type matches one of the events specified on the element.
if (events.indexOf(event.type) < 0) return;

// Ensures the type matches one of the events specified on the element.
if (events.indexOf(event.type) < 0) return;
/** @type {FieldsObj} */
const defaultFields = {
transport: 'beacon',
queueTime: now() - time,
};

/** @type {FieldsObj} */
const defaultFields = {transport: 'beacon'};
const attributeFields = getAttributeFields(element, prefix);
const userFields = assign({}, this.opts.fieldsObj, attributeFields);
const hitType = attributeFields.hitType || 'event';
const attributeFields = getAttributeFields(element, prefix);
const userFields = assign({}, this.opts.fieldsObj, attributeFields);
const hitType = attributeFields.hitType || 'event';

this.tracker.send(hitType, createFieldsObj(defaultFields,
userFields, this.tracker, this.opts.hitFilter, element, event));
this.tracker.send(hitType, createFieldsObj(defaultFields,
userFields, this.tracker, this.opts.hitFilter, element, event));
});
}

/**
* Removes all event listeners and instance properties.
*/
remove() {
this.queue.destroy();
Object.keys(this.delegates).forEach((key) => {
this.delegates[key].destroy();
});
Expand Down

0 comments on commit be29604

Please sign in to comment.