Vue Segment Analytics Plugin
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
-
TypeError: Cannot read properties of undefined (reading '$segment')
cause This error occurs when trying to access `this.$segment` within a Vue component, but the `vue-segment-analytics` plugin has not been correctly installed via `Vue.use()` or if the component is somehow outside the scope of the Vue instance where the plugin was installed.fixEnsure `Vue.use(VueSegmentAnalytics, { id: 'YOUR_SEGMENT_WRITE_KEY' })` is called before instantiating your Vue app. Verify that the component attempting to access `$segment` is part of the Vue instance tree. -
Segment events are not being sent or are not visible in the Segment debugger.
cause This is typically caused by an incorrect or missing Segment 'Write Key' (`id` property in the options), network blocking (e.g., ad blockers, corporate firewalls), or incorrect event payload structure.fixDouble-check your `id` configuration in `Vue.use()`. Ensure it matches your Segment project's Write Key. Use the browser's network tab or Segment's debugger to inspect network requests. Temporarily disable ad blockers during development. -
Error: 'Vue' is not defined or 'Vue' is not a constructor
cause This usually indicates that Vue is not correctly imported or globally exposed in your project environment, or you are attempting to use the plugin in a non-Vue 2 environment.fixMake sure you have `import Vue from 'vue'` at the top of your main JavaScript file and that you are using Vue 2. If using CommonJS, ensure Vue is properly `require`d and exposed.
Warnings
- breaking This package is specifically built for Vue 2 applications. It is not compatible with Vue 3 due to fundamental changes in Vue's API and plugin system.
- deprecated The official Segment recommendation for modern Vue projects (especially Vue 3) is to use the `@segment/analytics-vue` package. This `vue-segment-analytics` package is considered a legacy solution primarily for existing Vue 2 applications.
- gotcha The package has seen infrequent updates since mid-2022, primarily for dependency bumps. New features or rapid bug fixes are unlikely, indicating it is in maintenance mode.
Install
-
npm install vue-segment-analytics -
yarn add vue-segment-analytics -
pnpm add vue-segment-analytics
Imports
- Vue
import { Vue } from 'vue'import Vue from 'vue'
- VueSegmentAnalytics
const VueSegmentAnalytics = require('vue-segment-analytics')import VueSegmentAnalytics from 'vue-segment-analytics'
- $segment
Segment.track('Event Name')this.$segment.track('Event Name', { property: 'value' })
Quickstart
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');
}
});