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

Commit 9f1d3a2

Browse files
refactor(exception): remove window handler
1 parent 147a499 commit 9f1d3a2

File tree

5 files changed

+40
-57
lines changed

5 files changed

+40
-57
lines changed

__tests__/lib/autotracking.exception.spec.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import VueAnalytics from '../../src'
33

44
window.ga = jest.fn()
55

6-
const originalErrorHandler = jest.fn()
7-
Vue.config.errorHandler = originalErrorHandler
8-
96
Vue.use(VueAnalytics, {
107
id: 'UA-1234-5',
118
autoTracking: {
@@ -33,5 +30,6 @@ it('should track Vue render error', () => {
3330
})
3431

3532
it('should preserve original error handler', () => {
36-
expect(originalErrorHandler).toBeCalledWith(renderError, $vm, 'created hook')
33+
Vue.config.errorHandler = jest.fn()
34+
expect(window.ga).not.toBeCalledWith(renderError, $vm, 'created hook')
3735
})

src/bootstrap.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import createTrackers from './create-trackers'
44
import collectors from './collectors'
55
import untracked from 'lib/untracked'
66
import * as page from 'lib/page'
7-
import * as exception from 'lib/exception'
87

98
export default function bootstrap () {
109
if (typeof document === 'undefined') {
@@ -44,14 +43,12 @@ export default function bootstrap () {
4443
.then(id => {
4544
// Update the ID with the new value
4645
config.id = id
47-
// Create analytics trackers first
46+
// Create analytics trackers first
4847
createTrackers()
4948
// Add all collectors
5049
collectors()
5150
// Fire the callback function that analytics is ready
5251
config.ready()
53-
// Run exception autotracking
54-
exception.autotracking()
5552
// Run page autotracking
5653
page.autotracking()
5754
// Fire all untracked events

src/index.js

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
11
import bootstrap from './bootstrap'
2+
import lib from './lib'
3+
import { errorHandler } from 'lib/exception'
24
import config, { update } from './config'
35
import { onAnalyticsReady } from './helpers'
4-
5-
// Directives
66
import ga from 'directives/ga'
77

8-
// Features
9-
import event from 'lib/event'
10-
import exception, { setupErrorHandler } from 'lib/exception'
11-
import page from 'lib/page'
12-
import query from 'lib/query'
13-
import require from 'lib/require'
14-
import set from 'lib/set'
15-
import social from 'lib/social'
16-
import time from 'lib/time'
17-
import untracked from 'lib/untracked'
18-
import ecommerce from 'lib/ecommerce'
19-
208
export default function install (Vue, options = {}) {
219
update(options)
2210

2311
Vue.directive('ga', ga)
2412

25-
Vue.prototype.$ga = Vue.$ga = {
26-
event,
27-
exception,
28-
page,
29-
query,
30-
require,
31-
set,
32-
social,
33-
time,
34-
untracked,
35-
ecommerce,
36-
commands: config.commands
37-
}
13+
Vue.prototype.$ga = Vue.$ga = lib
3814

39-
setupErrorHandler(Vue)
15+
if (!Vue.config.errorHandler) {
16+
Vue.config.errorHandler = errorHandler
17+
}
4018

4119
bootstrap()
4220
}
4321

44-
export {
45-
onAnalyticsReady
22+
export {
23+
onAnalyticsReady
4624
}

src/lib/exception.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,9 @@ export default function exception (error, fatal = false) {
88
})
99
}
1010

11-
export function setupErrorHandler(Vue) {
12-
if (config.autoTracking.exception) {
13-
const originalErrorHandler = Vue.config.errorHandler
14-
Vue.config.errorHandler = function (error, vm, info) {
15-
vm.$ga.exception(error.message || error, true)
16-
if (typeof originalErrorHandler === 'function') {
17-
originalErrorHandler.call(this, error, vm, info)
18-
}
19-
}
20-
}
21-
}
22-
23-
export function autotracking () {
24-
if (!config.autoTracking.exception) {
25-
return
26-
}
11+
export function errorHandler (error, vm) {
12+
const { exception } = config.autoTracking
13+
const message = error.message || error
2714

28-
window.addEventListener('error', function (error) {
29-
exception(error.message || error)
30-
})
15+
exception && vm.$ga.exception(message, true)
3116
}

src/lib/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import event from 'lib/event'
2+
import exception from 'lib/exception'
3+
import page from 'lib/page'
4+
import query from 'lib/query'
5+
import require from 'lib/require'
6+
import set from 'lib/set'
7+
import social from 'lib/social'
8+
import time from 'lib/time'
9+
import untracked from 'lib/untracked'
10+
import ecommerce from 'lib/ecommerce'
11+
import config from '../config'
12+
13+
export default {
14+
event,
15+
exception,
16+
page,
17+
query,
18+
require,
19+
set,
20+
social,
21+
time,
22+
untracked,
23+
ecommerce,
24+
commands: config.commands
25+
}

0 commit comments

Comments
 (0)