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

import Vue from "vue";
import VueGtag from "vue-gtag";

Vue.use(VueGtag, {
  config: { id: "UA-1234567-1" },
  disableScriptLoad: true
});

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.

import Vue from "vue";
import VueGtag from "vue-gtag";

Vue.use(VueGtag, {
  config: { id: "UA-1234567-1" },
  bootstrap: false
});

Later on, in any places of your application, in any moment, you can simply import the bootstrap method in your code and run it

import { bootstrap } from 'vue-gtag'

export default {
  name: 'MyComponent',

  methods: {
    enablePlugin () {
      bootstrap().then((gtag) => {
        // all done!
      })
    }
  }
}

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

import Vue from "vue";
import VueGtag from "vue-gtag";

Vue.use(VueGtag, {
  bootstrap: false
});

then just go to a component on in another place in your code and type something like this

import { setOptions, bootstrap } from 'vue-gtag'

export default {
  name: 'MyComponent',

  methods: {
    enablePlugin () {
      setOptions({
        config: { id: 'UA-123456-7' }
      })

      bootstrap().then((gtag) => {
        // all done!
      })
    }
  }
}

Make sure to set your options before calling the bootstrap method or your new options won't be saved

Last updated