How To Add Google Analytics In Vue Js

Google Analytics is an extremely effective tool for monitoring user engagement on your website and gaining insight into your audience. If you are utilizing Vue.js to build your website, you may be curious about how you can incorporate Google Analytics into your development. Fortunately, the process is straightforward and in this blog post, we will walk you through the necessary steps to seamlessly integrate Google Analytics into your Vue.js application.

Step 1: Create a Google Analytics Account

Before you can add Google Analytics to your Vue.js application, you first need to have a Google Analytics account. If you don’t already have one, you can create one from the Google Analytics website. After creating your account, you will be provided with a tracking ID, which we will use later in the process.

Step 2: Install vue-analytics

Now, you need to install the vue-analytics plugin, which conveniently integrates Google Analytics into your Vue.js applications. Open your terminal and navigate to your project directory, then enter this command to install the plugin.

npm install vue-analytics

Step 3: Configure vue-analytics

With the plugin installed, you need to configure it to use your Google Analytics tracking ID. Open the file where you initialize your Vue application, typically in your main.js or app.js file, and add the following lines of code:

import Vue from 'vue'
import VueAnalytics from 'vue-analytics'

Vue.use(VueAnalytics, {
  id: 'Your-Google-Analytics-ID',
  checkDuplicatedScript: true

Don’t forget to replace ‘Your-Google-Analytics-ID’ with the tracking ID you got from Google Analytics. The checkDuplicatedScript: true option is used to prevent the Google Analytics script from being included more than once if the page is reloaded.

Step 4: Use vue-analytics

Now you’re all set to use Google Analytics in your Vue.js application. The vue-analytics plugin provides several methods for tracking user interactions. For example, you can track page views in your router’s afterEach hook like this:

router.afterEach((to, from) => {

Or you can track custom events like button clicks with this syntax:

Vue.$ga.event('button', 'click', 'play')


Adding Google Analytics to your Vue.js application is pretty straightforward with the vue-analytics plugin. Now you can track user interactions, analyze your audience and improve your website based on data-driven decisions. If you have any questions or run into any issues, feel free to leave a comment below.