Vue Segment Analytics Plugin

0.5.4 · deprecated · verified Sun Apr 19

This package is a Vue plugin for integrating Segment's `analytics.js` into Vue 2 applications. It provides a `Vue.use()` interface to initialize Segment with a write key (`id`) and can integrate with Vue Router for automatic page tracking. The current stable version is 0.5.4, with the last update in June 2022, indicating an infrequent release cadence focused on maintenance rather than active feature development. Its primary differentiator is its direct plugin integration for Vue 2, making Segment methods available globally via `this.$segment`. However, for modern Vue 3 applications, the official Segment recommendation is `@segment/analytics-vue`, making this package a legacy solution for existing Vue 2 projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install and initialize `vue-segment-analytics` with a Vue 2 application, including optional router integration for automatic page tracking and a basic example of manually tracking an event.

import Vue from 'vue';
import VueRouter from 'vue-router';
import VueSegmentAnalytics from 'vue-segment-analytics';

Vue.use(VueRouter);
const router = new VueRouter({
  routes: [{ path: '/', component: { template: '<div>Home</div>' } }]
});

Vue.use(VueSegmentAnalytics, {
  id: process.env.SEGMENT_WRITE_KEY ?? 'YOUR_SEGMENT_WRITE_KEY',
  router: router, // Optional: Pass Vue Router instance for automatic page tracking
  debug: process.env.NODE_ENV !== 'production'
});

new Vue({
  router,
  el: '#app',
  template: `
    <div>
      <h1>Welcome</h1>
      <button @click="trackClick">Track Me</button>
      <router-view></router-view>
    </div>
  `,
  methods: {
    trackClick() {
      this.$segment.track('Button Clicked', { buttonName: 'Track Me' });
      console.log('Segment event tracked: Button Clicked');
    }
  },
  mounted() {
    this.$segment.page('Home Page View', { path: '/' });
    console.log('Segment event tracked: Home Page View');
  }
});

view raw JSON →