Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
refactor(exception): remove window handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGabriele committed Dec 26, 2017
1 parent 147a499 commit 9f1d3a2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
6 changes: 2 additions & 4 deletions __tests__/lib/autotracking.exception.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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')
})
5 changes: 1 addition & 4 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
Expand Down
38 changes: 8 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 4 additions & 19 deletions src/lib/exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
25 changes: 25 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9f1d3a2

Please sign in to comment.