diff --git a/spec/regexper_spec.js b/spec/regexper_spec.js index a82bc49..fd5e403 100644 --- a/spec/regexper_spec.js +++ b/spec/regexper_spec.js @@ -262,9 +262,13 @@ describe('regexper.js', function() { describe('#_trackEvent', function() { + beforeEach(function() { + spyOn(window._gaq, 'push'); + }); + it('adds a _trackEvent call to gaq', function() { this.regexper._trackEvent('category', 'action'); - expect(this.regexper.gaq).toContain(['_trackEvent', 'category', 'action']); + expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'category', 'action']); }); }); @@ -407,27 +411,27 @@ describe('regexper.js', function() { it('sets the state to be "has-error"', function(done) { this.regexper.renderRegexp('example expression') - .then(fail, () => { + .then(() => { expect(this.regexper.state).toEqual('has-error'); - }) + }, fail) .finally(done) .done(); }); it('displays the error message', function(done) { this.regexper.renderRegexp('example expression') - .then(fail, () => { + .then(() => { expect(this.regexper.error.innerHTML).toEqual('Error: example parse error'); - }) + }, fail) .finally(done) .done(); }); it('tracks the parse error', function(done) { this.regexper.renderRegexp('example expression') - .then(fail, () => { + .then(() => { expect(this.regexper._trackEvent).toHaveBeenCalledWith('visualization', 'parse error'); - }) + }, fail) .finally(done) .done(); }); @@ -553,15 +557,6 @@ describe('regexper.js', function() { this.renderPromise.reject('example render failure'); }); - it('tracks the failed render', function(done) { - this.regexper.renderRegexp('example expression') - .then(fail, () => { - expect(this.regexper._trackEvent).toHaveBeenCalledWith('visualization', 'exception'); - }) - .finally(done) - .done(); - }); - it('sets the runningParser to false', function(done) { this.regexper.renderRegexp('example expression') .then(fail, () => { diff --git a/src/js/main.js b/src/js/main.js index 532bfea..a31e97f 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -3,7 +3,20 @@ import Regexper from './regexper.js'; import Parser from './parser/javascript.js'; import _ from 'lodash'; +window._gaq = (typeof _gaq !== 'undefined') ? _gaq : { + push: console.debug.bind(console) +}; + (function() { + window.addEventListener('error', function(error) { + _gaq.push([ + '_trackEvent', + 'global', + 'exception', + `${error.filename}(${error.lineno}): ${error.message}` + ]); + }); + if (document.body.querySelector('#content .application')) { var regexper = new Regexper(document.body); diff --git a/src/js/regexper.js b/src/js/regexper.js index a000834..c224a16 100644 --- a/src/js/regexper.js +++ b/src/js/regexper.js @@ -15,8 +15,6 @@ export default class Regexper { this.percentage = root.querySelector('#progress div'); this.svgContainer = root.querySelector('#regexp-render'); this.svgBase = this.root.querySelector('#svg-base').innerHTML; - - this.gaq = (typeof window._gaq === 'undefined') ? [] : window._gaq; } keypressListener(event) { @@ -79,7 +77,7 @@ export default class Regexper { } _trackEvent(category, action) { - this.gaq.push(['_trackEvent', category, action]); + window._gaq.push(['_trackEvent', category, action]); } set state(state) { @@ -169,8 +167,9 @@ export default class Regexper { if (message === 'Render cancelled') { this._trackEvent('visualization', 'cancelled'); this.state = ''; + } else if (message.parseError) { + this._trackEvent('visualization', 'parse error'); } else { - this._trackEvent('visualization', (message.parseError ? 'parse error' : 'exception')); throw message; } })