|
1 |
| -import { loadScript, shouldGaLoad } from './helpers' |
| 1 | +import { promisify, loadScript, shouldGaLoad } from './helpers' |
2 | 2 | import config, { update } from './config'
|
3 | 3 | import createTrackers from './create-trackers'
|
4 | 4 | import collectors from './collectors'
|
5 | 5 | import { autoTracking } from 'lib/page'
|
| 6 | +import untracked from './untracked' |
6 | 7 | import noga from './no-ga'
|
7 | 8 |
|
8 | 9 | export default () => {
|
9 | 10 | if (typeof document === 'undefined' || typeof window === 'undefined') {
|
10 | 11 | return
|
11 | 12 | }
|
12 | 13 |
|
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' |
15 | 16 | const resource = `https://www.google-analytics.com/${filename}.js`
|
16 | 17 |
|
17 |
| - if (!id) { |
| 18 | + if (!config.id) { |
18 | 19 | throw new Error(
|
19 | 20 | '[vue-analytics] Missing the "id" parameter. Add at least one tracking domain ID'
|
20 | 21 | )
|
21 | 22 | }
|
22 | 23 |
|
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)) |
30 | 31 | }
|
31 | 32 |
|
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 | + }) |
40 | 38 |
|
41 |
| - if (disableTracking) { |
42 |
| - noga() |
43 |
| - } |
| 39 | + // Opt-in/opt-out #gdpr |
| 40 | + noga(config.disabled) |
44 | 41 |
|
| 42 | + // Creates necessary trackers |
45 | 43 | 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 |
46 | 50 | collectors()
|
| 51 | + |
| 52 | + // Starts auto tracking |
47 | 53 | 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 | + ) |
48 | 60 | })
|
49 | 61 | }
|
0 commit comments