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

Commit 65a2bcc

Browse files
author
Matteo Gabriele
committed
feat(script-loader): add optional analytics script tag check
is now possible to disable auto-loading or let the plugin detect if the script tag has been added already closes #60
1 parent 3daa19d commit 65a2bcc

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<a href="https://www.npmjs.com/package/vue-analytics">
1010
<img src="https://img.shields.io/npm/dm/vue-analytics.svg" />
1111
<a/>
12-
12+
1313
<img src="https://travis-ci.org/MatteoGabriele/vue-analytics.svg?branch=master" />
1414
</p>
1515

@@ -30,6 +30,7 @@ npm install vue-analytics
3030
## User guide
3131

3232
* [Get started](/docs/installation.md)
33+
* [How to load Google Analytics](/docs/script-loader.md)
3334
* [Untracked hits](/docs/untracked-hits.md)
3435
* [Page tracking](/docs/page-tracking.md)
3536
* [Enable page auto tracking](/docs/page-tracking.md#enable-page-auto-tracking)
@@ -40,7 +41,7 @@ npm install vue-analytics
4041
* [Event tracking](/docs/event-tracking.md)
4142
* [User timings](/docs/user-timings.md#user-timings)
4243
* [Exception tracking](/docs/exception-tracking.md)
43-
* [Enable exception auto tracking](/docs/exception-tracking.md#enable-exception-auto-tracking)
44+
* [Enable exception auto tracking](/docs/exception-tracking.md#enable-exception-auto-tracking)
4445
* [Require](/docs/require.md)
4546
* [Set](/docs/set.md)
4647
* [Social interactions](/docs/social-interactions.md)
@@ -55,4 +56,3 @@ npm install vue-analytics
5556
Please drop an issue, if you find something that doesn't work, or a feature request at [https://github.com/MatteoGabriele/vue-analytics/issues](https://github.com/MatteoGabriele/vue-analytics/issues)
5657

5758
Follow me on twitter [@matteo\_gabriele](https://twitter.com/matteo_gabriele)
58-

docs/debug.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Implements Google Analaytics debug library.
66

77
```js
88
Vue.use(VueAnalytics, {
9+
id: 'UA-XXX-X',
910
debug: {
1011
enabled: true,
1112
trace: false,
@@ -15,4 +16,3 @@ Vue.use(VueAnalytics, {
1516
```
1617

1718
Google Analytics docs: [debugging](https://developers.google.com/analytics/devguides/collection/analyticsjs/debugging)
18-

docs/script-loader.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## How to load Google Analytics script
2+
3+
By default nothing is needed: the plugin does everything for you!
4+
5+
However, in some cases, you might want to avoid auto-loading the analytics.js script because:
6+
- maybe the framework you're using already does it for you
7+
- you really can't remove it from your project
8+
- other issue that I can't come up with
9+
10+
So for all those cases it is possible to let the plugin detect if an analytics script has been added already in your html
11+
12+
```js
13+
import Vue from 'vue'
14+
import VueAnalytics from 'vue-analytics'
15+
16+
Vue.use(VueAnalytics, {
17+
id: 'UA-XXX-X',
18+
checkDuplicatedScript: true
19+
})
20+
```
21+
22+
or just disable the script loader
23+
24+
```js
25+
import Vue from 'vue'
26+
import VueAnalytics from 'vue-analytics'
27+
28+
Vue.use(VueAnalytics, {
29+
id: 'UA-XXX-X',
30+
disableScriptLoader: true
31+
})
32+
```
33+
34+
### Important
35+
It is not possible for the plugin to remove the initial trackers, because it needs them to create all methods for the multi-tracking feature.
36+
**If you can't remove initial trackers, don't use this plugin: the outcome could be unpredictable.**

src/bootstrap.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import load from 'load-script'
2-
import { onAnalyticsReady } from './helpers'
3-
import config from './config'
2+
import { onAnalyticsReady, hasGoogleScript } from './helpers'
3+
import config, { update } from './config'
44
import createTrackers from './create-trackers'
55
import untracked from 'lib/untracked'
66
import { startAutoTracking as pageAutoTracking } from 'lib/page'
@@ -11,31 +11,44 @@ export default function bootstrap () {
1111
return
1212
}
1313

14-
const { id, debug, ready } = config
15-
const filename = debug.enabled ? 'analytics_debug.js' : 'analytics.js'
14+
const { id, ready, debug, checkDuplicatedScript, disableScriptLoader } = config
15+
const filename = debug.enabled ? 'analytics_debug' : 'analytics'
16+
const googleScript = `https://www.google-analytics.com/${filename}.js`
1617

1718
if (!id) {
1819
throw new Error('[vue-analytics] Please enter a Google Analytics tracking ID')
1920
}
2021

21-
load(`https://www.google-analytics.com/${filename}`, function (error) {
22-
if (error) {
23-
console.error('[vue-analytics] It\'s not possible to load Google Analytics script')
24-
return
22+
return new Promise((resolve, reject) => {
23+
if ((checkDuplicatedScript && hasGoogleScript(googleScript)) || disableScriptLoader) {
24+
return resolve()
2525
}
2626

27-
onAnalyticsReady().then(() => {
28-
// we first need to add trackers to be able to track
29-
// every other aspect of the application
30-
createTrackers()
31-
// trigger the plugin `ready` callback right after the trackers
32-
ready()
33-
// add exceptions auto tracking
34-
exceptionAutoTracking()
35-
// add page auto tracking
36-
pageAutoTracking()
37-
// track every untracked events before analytics was ready
38-
untracked()
27+
load(googleScript, function (error) {
28+
if (error) {
29+
return reject('[vue-analytics] It\'s not possible to load Google Analytics script')
30+
}
31+
32+
return resolve()
3933
})
4034
})
35+
.then(() => {
36+
return onAnalyticsReady()
37+
})
38+
.then(() => {
39+
// we first need to add trackers to be able to track
40+
// every other aspect of the application
41+
createTrackers()
42+
// trigger the plugin `ready` callback right after the trackers
43+
ready()
44+
// add exceptions auto tracking
45+
exceptionAutoTracking()
46+
// add page auto tracking
47+
pageAutoTracking()
48+
// track every untracked events before analytics was ready
49+
untracked()
50+
})
51+
.catch(error => {
52+
console.error(error)
53+
})
4154
}

src/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const defaultConfig = {
2121
sendHitTask: true
2222
},
2323

24+
checkDuplicatedScript: false,
25+
disableScriptLoader: false,
26+
2427
beforeFirstHit: noop,
2528
ready: noop,
2629

src/helpers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ export function merge (obj, src) {
1414
return obj
1515
}
1616

17+
export function hasGoogleScript () {
18+
const scriptTags = Array.prototype.slice.call(
19+
document.getElementsByTagName('script')
20+
).filter(script => {
21+
return script.src.indexOf('analytics') !== -1
22+
})
23+
24+
return scriptTags.length > 0
25+
}
26+
1727
export function getTracker (trackerId) {
1828
return trackerId.replace(/-/gi, '')
1929
}

0 commit comments

Comments
 (0)