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

Commit d4bd0d6

Browse files
fix(bootstrap): missing check for duplicated google analytics script
closes #110
1 parent ef1fa26 commit d4bd0d6

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/bootstrap.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { loadScript } from './helpers'
1+
import { loadScript, shouldGaLoad } from './helpers'
22
import config, { update } from './config'
33
import createTrackers from './create-trackers'
44
import collectors from './collectors'
@@ -20,10 +20,11 @@ export default () => {
2020
)
2121
}
2222

23-
if (!window.ga || !disableScriptLoader) {
23+
if (shouldGaLoad() && (!window.ga || !disableScriptLoader)) {
2424
loadScript(resource).catch(() => {
2525
console.error(
26-
`[vue-analytics] An error occured trying to load ${resource}. Please check your connection.`
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.`
2728
)
2829
})
2930
}

src/helpers.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function loadScript (url) {
1111
script.charset = 'utf8'
1212

1313
head.appendChild(script)
14-
14+
1515
script.onload = resolve
1616
script.onerror = reject
1717
})
@@ -40,16 +40,21 @@ export function merge (obj, src) {
4040
return obj
4141
}
4242

43-
export function hasGoogleScript () {
43+
export function hasScript () {
4444
const scriptTags = Array.prototype.slice.call(
4545
document.getElementsByTagName('script')
4646
).filter(script => {
47-
return script.src.indexOf('analytics') !== -1
47+
return (script.src.indexOf('analytics') !== -1) ||
48+
(script.src.indexOf('gtag') !== -1)
4849
})
4950

5051
return scriptTags.length > 0
5152
}
5253

54+
export function shouldGaLoad () {
55+
return !(config.checkDuplicatedScript && hasScript())
56+
}
57+
5358
export function getTracker (tracker) {
5459
return tracker.name || tracker.replace(/-/gi, '')
5560
}

0 commit comments

Comments
 (0)