From 9f1d3a2aa2253abc847ae172c622d80c886fd478 Mon Sep 17 00:00:00 2001 From: Matteo Gabriele Date: Tue, 26 Dec 2017 17:29:52 +0100 Subject: [PATCH] refactor(exception): remove window handler --- __tests__/lib/autotracking.exception.spec.js | 6 ++-- src/bootstrap.js | 5 +-- src/index.js | 38 +++++--------------- src/lib/exception.js | 23 +++--------- src/lib/index.js | 25 +++++++++++++ 5 files changed, 40 insertions(+), 57 deletions(-) create mode 100644 src/lib/index.js diff --git a/__tests__/lib/autotracking.exception.spec.js b/__tests__/lib/autotracking.exception.spec.js index 891f577..1b7ff27 100644 --- a/__tests__/lib/autotracking.exception.spec.js +++ b/__tests__/lib/autotracking.exception.spec.js @@ -3,9 +3,6 @@ import VueAnalytics from '../../src' window.ga = jest.fn() -const originalErrorHandler = jest.fn() -Vue.config.errorHandler = originalErrorHandler - Vue.use(VueAnalytics, { id: 'UA-1234-5', autoTracking: { @@ -33,5 +30,6 @@ it('should track Vue render error', () => { }) it('should preserve original error handler', () => { - expect(originalErrorHandler).toBeCalledWith(renderError, $vm, 'created hook') + Vue.config.errorHandler = jest.fn() + expect(window.ga).not.toBeCalledWith(renderError, $vm, 'created hook') }) diff --git a/src/bootstrap.js b/src/bootstrap.js index 8d21521..c853213 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -4,7 +4,6 @@ import createTrackers from './create-trackers' import collectors from './collectors' import untracked from 'lib/untracked' import * as page from 'lib/page' -import * as exception from 'lib/exception' export default function bootstrap () { if (typeof document === 'undefined') { @@ -44,14 +43,12 @@ export default function bootstrap () { .then(id => { // Update the ID with the new value config.id = id - // Create analytics trackers first + // Create analytics trackers first createTrackers() // Add all collectors collectors() // Fire the callback function that analytics is ready config.ready() - // Run exception autotracking - exception.autotracking() // Run page autotracking page.autotracking() // Fire all untracked events diff --git a/src/index.js b/src/index.js index 266173a..bc6c525 100644 --- a/src/index.js +++ b/src/index.js @@ -1,46 +1,24 @@ import bootstrap from './bootstrap' +import lib from './lib' +import { errorHandler } from 'lib/exception' import config, { update } from './config' import { onAnalyticsReady } from './helpers' - -// Directives import ga from 'directives/ga' -// Features -import event from 'lib/event' -import exception, { setupErrorHandler } from 'lib/exception' -import page from 'lib/page' -import query from 'lib/query' -import require from 'lib/require' -import set from 'lib/set' -import social from 'lib/social' -import time from 'lib/time' -import untracked from 'lib/untracked' -import ecommerce from 'lib/ecommerce' - export default function install (Vue, options = {}) { update(options) Vue.directive('ga', ga) - Vue.prototype.$ga = Vue.$ga = { - event, - exception, - page, - query, - require, - set, - social, - time, - untracked, - ecommerce, - commands: config.commands - } + Vue.prototype.$ga = Vue.$ga = lib - setupErrorHandler(Vue) + if (!Vue.config.errorHandler) { + Vue.config.errorHandler = errorHandler + } bootstrap() } -export { - onAnalyticsReady +export { + onAnalyticsReady } diff --git a/src/lib/exception.js b/src/lib/exception.js index 2303971..4f4a0cd 100644 --- a/src/lib/exception.js +++ b/src/lib/exception.js @@ -8,24 +8,9 @@ export default function exception (error, fatal = false) { }) } -export function setupErrorHandler(Vue) { - if (config.autoTracking.exception) { - const originalErrorHandler = Vue.config.errorHandler - Vue.config.errorHandler = function (error, vm, info) { - vm.$ga.exception(error.message || error, true) - if (typeof originalErrorHandler === 'function') { - originalErrorHandler.call(this, error, vm, info) - } - } - } -} - -export function autotracking () { - if (!config.autoTracking.exception) { - return - } +export function errorHandler (error, vm) { + const { exception } = config.autoTracking + const message = error.message || error - window.addEventListener('error', function (error) { - exception(error.message || error) - }) + exception && vm.$ga.exception(message, true) } diff --git a/src/lib/index.js b/src/lib/index.js new file mode 100644 index 0000000..99fcffb --- /dev/null +++ b/src/lib/index.js @@ -0,0 +1,25 @@ +import event from 'lib/event' +import exception from 'lib/exception' +import page from 'lib/page' +import query from 'lib/query' +import require from 'lib/require' +import set from 'lib/set' +import social from 'lib/social' +import time from 'lib/time' +import untracked from 'lib/untracked' +import ecommerce from 'lib/ecommerce' +import config from '../config' + +export default { + event, + exception, + page, + query, + require, + set, + social, + time, + untracked, + ecommerce, + commands: config.commands +}