From cf1bde0f71806acfed9e75659858671c9ac0716c Mon Sep 17 00:00:00 2001 From: Matteo Gabriele Date: Wed, 8 Jan 2020 23:34:45 +0100 Subject: [PATCH] fix(preconnect): use only domain closes #208 --- src/bootstrap.js | 14 ++++++++------ src/helpers.js | 16 ++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index 2739277..ae86e76 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -11,10 +11,6 @@ export default () => { return } - const { ready } = config - const filename = config.debug.enabled ? 'analytics_debug' : 'analytics' - const resource = config.customResourceURL || `https://www.google-analytics.com/${filename}.js` - if (!config.id) { log('Missing the "id" parameter. Add at least one tracking domain ID') return @@ -26,8 +22,14 @@ export default () => { ] if (shouldGaLoad()) { + const domain = 'https://www.google-analytics.com' + const filename = config.debug.enabled ? 'analytics_debug' : 'analytics' + const resourcePromise = config.customResourceURL + ? loadScript(config.customResourceURL) + : loadScript(`${domain}/${filename}.js`, domain) + queue.push( - loadScript(resource).catch(() => { + resourcePromise.catch(() => { log('An error occured! Please check your connection or disable your AD blocker') }) ) @@ -55,7 +57,7 @@ export default () => { // Starts auto tracking autoTracking() - ready() + config.ready() }).catch(error => { if (!config.debug.enabled) { return diff --git a/src/helpers.js b/src/helpers.js index f1c2b8c..d2ef8a7 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -6,20 +6,24 @@ export const log = message => { console.warn(`[vue-analytics] ${message}`) } -export function loadScript (url) { +export function loadScript (url, domain) { return new Promise((resolve, reject) => { - var head = document.head || document.getElementsByTagName('head')[0] + const head = document.head || document.getElementsByTagName('head')[0] const script = document.createElement('script') - const link = document.createElement('link') script.async = true script.src = url script.charset = 'utf-8' - link.href = url - link.rel = 'preconnect' + if (domain) { + const link = document.createElement('link') + + link.href = domain + link.rel = 'preconnect' + + head.appendChild(link) + } - head.appendChild(link) head.appendChild(script) script.onload = resolve