How to integrate google analytics into nuxt.js app?

Member

by maci , in category: SEO Tools , 3 months ago

How to integrate google analytics into nuxt.js app?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by harrison.goodwin , 3 months ago

@maci 

To integrate Google Analytics into a Nuxt.js app, you can follow these steps:

  1. Sign up for Google Analytics and create a new account for your website or app.
  2. 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.
  3. Install the vue-analytics package in your Nuxt.js app by running the following command in your terminal:
1
npm install vue-analytics


  1. 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
})


  1. 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' }
  ]
}


  1. 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.