Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit f3fae47

Browse files
fix: event triggered before ga is loaded are ignored
closes #142
1 parent b0eb56c commit f3fae47

File tree

6 files changed

+69
-42
lines changed

6 files changed

+69
-42
lines changed

src/bootstrap.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
1-
import { loadScript, shouldGaLoad } from './helpers'
1+
import { promisify, loadScript, shouldGaLoad } from './helpers'
22
import config, { update } from './config'
33
import createTrackers from './create-trackers'
44
import collectors from './collectors'
55
import { autoTracking } from 'lib/page'
6+
import untracked from './untracked'
67
import noga from './no-ga'
78

89
export default () => {
910
if (typeof document === 'undefined' || typeof window === 'undefined') {
1011
return
1112
}
1213

13-
const { wait, id, disabled, debug, disableScriptLoader } = config
14-
const filename = debug.enabled ? 'analytics_debug' : 'analytics'
14+
const { disableScriptLoader: noScript } = config
15+
const filename = config.debug.enabled ? 'analytics_debug' : 'analytics'
1516
const resource = `https://www.google-analytics.com/${filename}.js`
1617

17-
if (!id) {
18+
if (!config.id) {
1819
throw new Error(
1920
'[vue-analytics] Missing the "id" parameter. Add at least one tracking domain ID'
2021
)
2122
}
2223

23-
if (shouldGaLoad() && (!window.ga || !disableScriptLoader)) {
24-
loadScript(resource).catch(() => {
25-
console.error(
26-
`[vue-analytics] An error occured trying to load ${resource}. Please check your connection ` +
27-
`or if you have any Google Analytics blocker installed in your browser.`
28-
)
29-
})
24+
const queue = [
25+
promisify(config.id),
26+
promisify(config.disabled)
27+
]
28+
29+
if (shouldGaLoad() && (!window.ga || !noScript)) {
30+
queue.push(loadScript(resource))
3031
}
3132

32-
Promise.resolve(
33-
(typeof id === 'function') ? id() : id
34-
).then(newId => {
35-
update({ id: newId })
36-
}).then(() => {
37-
return (typeof disabled === 'function') ? disabled() : disabled
38-
}).then(disableTracking => {
39-
update({ disabled: disableTracking })
33+
return Promise.all(queue).then(response => {
34+
update({
35+
id: response[0],
36+
disabled: response[1]
37+
})
4038

41-
if (disableTracking) {
42-
noga()
43-
}
39+
// Opt-in/opt-out #gdpr
40+
noga(config.disabled)
4441

42+
// Creates necessary trackers
4543
createTrackers()
44+
45+
// Fires all untracked event that have been fired
46+
// meanwhile GoogleAnalayitcs script was loading
47+
untracked()
48+
49+
// Fires all shorthand fields in the options
4650
collectors()
51+
52+
// Starts auto tracking
4753
autoTracking()
54+
}).catch(response => {
55+
console.error(
56+
`[vue-analytics] An error occured! Please check your connection, ` +
57+
`if you have any Google Analytics blocker installed in your browser ` +
58+
`or check your custom resource URL if you have added any.`
59+
)
4860
})
4961
}

src/helpers.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ export function isRouter (data) {
106106
return data.currentRoute
107107
}
108108

109-
export function hasProps (props) {
110-
return Object.keys(props).length > 0
111-
}
109+
export const promisify = value => {
110+
if (value.then) {
111+
return value
112+
}
113+
114+
if (typeof value === 'function') {
115+
const payload = value()
116+
117+
return payload.then ? payload : Promise.resolve(payload)
118+
}
119+
120+
return Promise.resolve(value)
121+
}

src/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import bootstrap from './bootstrap'
22
import lib from './lib'
3-
import config, { update } from './config'
3+
import { update } from './config'
44
import { onAnalyticsReady } from './helpers'
55
import ga from 'directives/ga'
66
import * as exception from 'lib/exception'
@@ -9,13 +9,6 @@ import analyticsMiddleware from './vuex-middleware'
99
export default function install (Vue, options = {}) {
1010
update({ ...options, $vue: Vue })
1111

12-
if (typeof window !== 'undefined' && !window.ga) {
13-
window.ga = window.ga || function () {
14-
(window.ga.q = window.ga.q || []).push(arguments)
15-
}
16-
window.ga.l = Number(new Date())
17-
}
18-
1912
Vue.directive('ga', ga)
2013

2114
Vue.prototype.$ga = Vue.$ga = lib

src/lib/page.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import set from 'lib/set'
33
import screenview from 'lib/screenview'
44
import query from 'lib/query'
55
import {
6-
noop,
76
getQueryString,
87
isRouteIgnored,
9-
getRouteAnalytics,
108
isRoute,
119
isRouter,
12-
getBasePath,
13-
hasProps
10+
getBasePath
1411
} from '../helpers'
1512

1613
export default function page (...args) {
@@ -71,7 +68,7 @@ export function trackRoute (route) {
7168

7269
let path = route.path + (transformQueryString ? queryString : '')
7370
path = needsBase ? getBasePath(base, path) : path
74-
71+
7572
page(path)
7673
}
7774
}

src/lib/query.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ export default function query (method, ...args) {
1010
}
1111

1212
getId().forEach(function (id) {
13+
const t = {
14+
m: getMethod(method, id),
15+
a: args
16+
}
17+
18+
if(!window.ga) {
19+
config.untracked.push(t)
20+
return
21+
}
22+
1323
if (config.batch.enabled) {
14-
coll.push({
15-
m: getMethod(method, id),
16-
a: args
17-
})
24+
coll.push(t)
1825

1926
if (!intr) {
2027
intr = setInterval(() => {

src/untracked.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import config from './config'
2+
import query from './lib/query'
3+
4+
export default () => {
5+
config.untracked.forEach(t => {
6+
query(t.m, ...t.a)
7+
})
8+
}

0 commit comments

Comments
 (0)