Skip to content

Commit

Permalink
Chore: Allow viewers to emit metrics (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored Feb 22, 2018
1 parent ee559fc commit 4ed0168
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ class Preview extends EventEmitter {
// Node requires listener attached to 'error'
this.viewer.addListener('error', this.triggerError);
this.viewer.addListener(VIEWER_EVENT.default, this.handleViewerEvents);
this.viewer.addListener(VIEWER_EVENT.metric, this.handleViewerMetrics);
}

/**
Expand Down Expand Up @@ -1101,6 +1102,23 @@ class Preview extends EventEmitter {
}
}

/**
* Handle metrics emitted by the viewer
*
* @private
* @param {Object} [data] - Viewer metric data
* @return {void}
*/
handleViewerMetrics(data) {
const formattedEvent = {
event_name: data.event,
value: data.data,
...this.createLogEvent()
};

this.emit(PREVIEW_METRIC, formattedEvent);
}

/**
* Finish loading a viewer - display the appropriate control buttons, re-emit the 'load' event, log
* the preview, and prefetch the next few files.
Expand Down
19 changes: 19 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,7 @@ describe('lib/Preview', () => {
preview.attachViewerListeners();
expect(preview.viewer.addListener).to.be.calledWith('error', sinon.match.func);
expect(preview.viewer.addListener).to.be.calledWith(VIEWER_EVENT.default, sinon.match.func);
expect(preview.viewer.addListener).to.be.calledWith(VIEWER_EVENT.metric, sinon.match.func);
});
});

Expand Down Expand Up @@ -1591,6 +1592,24 @@ describe('lib/Preview', () => {
});
});

describe('handleViewerMetrics()', () => {
it('should create a formatted event and emit a preview_metric', () => {
sandbox.stub(preview, 'createLogEvent');
sandbox.stub(preview, 'emit');
const fakeEvent = {
event: 'test',
data: 7
}

const fakeLog = {
event_name: fakeEvent.event,
value: fakeEvent.data
}
preview.handleViewerMetrics(fakeEvent);
expect(preview.emit).to.be.calledWith(PREVIEW_METRIC, fakeLog);
});
});

describe('finishLoading()', () => {
beforeEach(() => {
stubs.checkPermission = sandbox.stub(file, 'checkPermission');
Expand Down
5 changes: 3 additions & 2 deletions src/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const VIEWER_EVENT = {
notificationShow: 'notificationshow', // Show notification modal.
notificationHide: 'notificationhide', // Hide notification modal.
mediaEndAutoplay: 'mediaendautoplay', // Media playback has completed, with autoplay enabled.
error: 'error', // When an error occurs
default: 'viewerevent' // The default viewer event
error: 'error', // When an error occurs.
default: 'viewerevent', // The default viewer event.
metric: 'viewermetric' // A viewer metric.
};

// Error codes logged by preview with "preview_error" events
Expand Down
16 changes: 16 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ class BaseViewer extends EventEmitter {
});
}

/**
* Emits a viewer metric
*
* @protected
* @emits metric
* @param {string} event - Event name
* @param {Object} data - Event data
* @return {void}
*/
emitMetric(event, data) {
super.emit(VIEWER_EVENT.metric, {
event,
data
});
}

/**
* Handles the beginning of a pinch to zoom event on mobile.
* Although W3 strongly discourages the prevention of pinch to zoom,
Expand Down

0 comments on commit 4ed0168

Please sign in to comment.