Bootstrap options
Choose how you want to initialise vue-gtag in your application
By default the plugin will create a gtag instance of the library in the window scope and download the library for you, but often users have different uses cases, so here we will take a look at them.

Disable script loading

1
import Vue from "vue";
2
import VueGtag from "vue-gtag";
3
4
Vue.use(VueGtag, {
5
config: { id: "UA-1234567-1" },
6
disableScriptLoad: true
7
});
Copied!

Bootstrap later

It is possible to setup the entire plugin options, but to bootstrap it later on whenever the user decide too. This could also be useful for any GDPR proof projects.
1
import Vue from "vue";
2
import VueGtag from "vue-gtag";
3
4
Vue.use(VueGtag, {
5
config: { id: "UA-1234567-1" },
6
bootstrap: false
7
});
Copied!
Later on, in any places of your application, in any moment, you can simply import the bootstrap method in your code and run it
1
import { bootstrap } from 'vue-gtag'
2
3
export default {
4
name: 'MyComponent',
5
6
methods: {
7
enablePlugin () {
8
bootstrap().then((gtag) => {
9
// all done!
10
})
11
}
12
}
13
}
Copied!
the bootstrap method will return a promise that will be resolved when the script has been loaded completely.
The bootstrap promise returns the actual gtag api object, which is not what it's injected inside the Vuejs scope by the plugin, but the pure and simple gtag library.
To start using vue-gtag after bootstrap successful response, you can simply access the library from the component scope.

Set options

Normally you would add all your options inside the global object when installing the plugin, but it is also possible to set those options later in conjunction with calling the bootstrap method.
Start always but setting the bootstrap option to false
1
import Vue from "vue";
2
import VueGtag from "vue-gtag";
3
4
Vue.use(VueGtag, {
5
bootstrap: false
6
});
Copied!
then just go to a component on in another place in your code and type something like this
1
import { setOptions, bootstrap } from 'vue-gtag'
2
3
export default {
4
name: 'MyComponent',
5
6
methods: {
7
enablePlugin () {
8
setOptions({
9
config: { id: 'UA-123456-7' }
10
})
11
12
bootstrap().then((gtag) => {
13
// all done!
14
})
15
}
16
}
17
}
Copied!
Make sure to set your options before calling the bootstrap method or your new options won't be saved
Last modified 4mo ago