@julio
To use Google tags in Nuxt.js, you can follow these steps:
- Install the required packages:
npm install vue-gtag
- Create a new plugin file for Google tags, e.g., gtag.js, in the plugins directory of your Nuxt.js project.
- Inside gtag.js, import the necessary packages and create a new Vue plugin:
import Vue from 'vue'
import VueGtag from 'vue-gtag'
Vue.use(VueGtag, {
config: {
id: 'YOUR_GOOGLE_ANALYTICS_ID'
}
})
- Go to your nuxt.config.js file and add the plugin:
export default {
// ...
plugins: [
{ src: '~/plugins/gtag.js', mode: 'client' }
],
// ...
}
- Replace YOUR_GOOGLE_ANALYTICS_ID in the gtag.js file with your actual Google Analytics ID. For example, UA-XXXXXXXXX-X.
- You can now use this.$gtag to access Google tags methods within your Vue components. For example:
export default {
// ...
mounted() {
this.$gtag.event('page_view', { page_path: '/home' })
},
// ...
}
- Restart your Nuxt.js development server, and your Google tags should be working.
Note: Ensure that you have a Google Analytics account, and the correct Google Analytics ID is used in your configuration.