@maci
To integrate Google Analytics into a Nuxt.js app, you can follow these steps:
- Sign up for Google Analytics and create a new account for your website or app.
- Once you have created the account, you will receive a Tracking ID that looks like UA-XXXXXXXXX-X. Make sure to keep this Tracking ID handy.
- Install the vue-analytics package in your Nuxt.js app by running the following command in your terminal:
1
|
npm install vue-analytics
|
- Create a new plugin in your Nuxt.js app to configure Google Analytics. Create a new file called ga.js in the plugins directory of your Nuxt.js project and add the following code:
1
2
3
4
5
6
|
import Vue from 'vue'
import VueAnalytics from 'vue-analytics'
Vue.use(VueAnalytics, {
id: 'UA-XXXXXXXXX-X' // Update this with your Tracking ID
})
|
- Register the plugin in the nuxt.config.js file by adding it to the plugins array:
1
2
3
4
5
|
export default {
plugins: [
{ src: '~/plugins/ga.js', mode: 'client' }
]
}
|
- Now, Google Analytics will be integrated into your Nuxt.js app. You can track page views and other events by using the this.$ga object in your Vue components. For example, you can track a page view by adding the following code to your component:
1
|
this.$ga.page('Page Title', 'Page URL')
|
That's it! Your Nuxt.js app should now be set up to use Google Analytics for tracking user behavior and website performance.