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

Commit

Permalink
fix(preconnect): use only domain
Browse files Browse the repository at this point in the history
closes #208
  • Loading branch information
MatteoGabriele committed Jan 8, 2020
1 parent 66e9ff4 commit cf1bde0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
})
)
Expand Down Expand Up @@ -55,7 +57,7 @@ export default () => {
// Starts auto tracking
autoTracking()

ready()
config.ready()
}).catch(error => {
if (!config.debug.enabled) {
return
Expand Down
16 changes: 10 additions & 6 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf1bde0

Please sign in to comment.