Unofficial Nuxt.js module for Mixpanel integration.
- π Easy Mixpanel integration with Nuxt.js
- π Automatic page view tracking with Vue Router
- βοΈ Configurable options
- π§ Environment variable support
- π± Client-side only (SSR safe)
- π§ͺ Comprehensive test coverage
- π TypeScript declarations included
- π GitHub Actions CI/CD ready
- π‘οΈ Robust error handling and logging
- π§ Production-ready and battle-tested
npm install nuxt-mixpanel
# or
yarn add nuxt-mixpanelAdd the module to your nuxt.config.js:
export default {
modules: ["nuxt-mixpanel"],
mixpanel: {
token: "YOUR_MIXPANEL_TOKEN",
},
};You can also configure the token using environment variables:
MIXPANEL_TOKEN=your_token_hereexport default {
mixpanel: {
token: "YOUR_MIXPANEL_TOKEN",
config: {
debug: true,
track_pageview: true,
persistence: "localStorage",
},
name: "mixpanel", // optional
disabled: false, // optional
useRouter: true, // optional, enables automatic page tracking
},
};export default {
mounted() {
// Track an event
this.$mixpanel.track("Button Clicked", {
button: "header-cta",
page: this.$route.path,
});
// Identify a user
this.$mixpanel.identify("user-123");
// Set user properties
this.$mixpanel.people.set({
$email: "user@example.com",
$name: "John Doe",
});
},
};// In your Vuex actions
export default {
actions: {
login({ commit }, user) {
// Your login logic
commit("SET_USER", user);
// Track login event
this.$mixpanel.track("User Login", {
method: "email",
});
},
},
};export default {
asyncData({ $mixpanel }) {
$mixpanel.track("Page Loaded", {
page: "home",
});
},
};By default, this module will automatically track page views when using Vue Router. You can disable this by setting useRouter: false in the module options.
The tracked event will be named 'Page Viewed' and include the full path of the page.
To verify that Mixpanel is working correctly:
-
Check Browser Console: Enable debug mode to see tracking events
mixpanel: { config: { debug: true; } }
-
Check Network Tab: Look for requests to Mixpanel's API endpoints
-
Use Mixpanel's Live View: Monitor events in real-time in your Mixpanel dashboard
Module not working:
- Ensure your Mixpanel token is correctly set
- Check that the module is properly registered in
nuxt.config.js - Verify that
mixpanel-browserdependency is installed
TypeScript errors:
- Make sure TypeScript can find the type definitions
- Add
"types": ["nuxt-mixpanel"]to yourtsconfig.jsonif needed
Page tracking not working:
- Ensure
useRouteris not set tofalse - Check that Vue Router is properly configured in your Nuxt app
- Verify router integration in browser dev tools
Configuration errors:
- Recent versions (1.0.3+) include improved error handling for configuration issues
- Check the browser console for detailed error messages
- Ensure options.config is properly defined when using debug mode
- Fixed critical configuration bug: Resolved "Cannot read properties of undefined (reading 'debug')" error
- Enhanced error handling: Added try-catch blocks around Mixpanel initialization
- Improved router integration: Better error handling for router setup failures
- Test suite reliability: Fixed Jest mock isolation and setup issues
This module includes a comprehensive test suite built with Jest covering 100+ test scenarios:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage report
npm run test:coverage
# Run linting
npm run lintThe test suite covers:
- β Vue Plugin Installation: Testing Vue plugin registration and configuration with various options
- β Router Integration: Automatic page view tracking with Vue Router and SPA navigation
- β Component Usage: Testing $mixpanel availability and usage in Vue components
- β Event Tracking: Verifying track(), identify(), and people methods functionality
- β Configuration Handling: Testing various configuration options, priorities, and edge cases
- β Error Handling: Graceful handling of initialization and tracking errors
- β Token Management: Environment variables, module options, and runtime configuration
- β Store Integration: Vuex store integration and context injection testing
- β Real-world Scenarios: Complete e-commerce user journey testing from registration to purchase
- β Edge Cases: Missing configurations, invalid tokens, network failures
- 100+ automated tests covering all major functionality
- Comprehensive error handling with graceful fallbacks
- Production-ready with battle-tested code
- TypeScript support with complete type definitions
- ESLint integration for code quality
- CI/CD pipeline with GitHub Actions
The test suite includes realistic scenarios like:
- User registration and authentication flows
- E-commerce shopping cart interactions
- Revenue tracking and user analytics
- SPA routing and page view tracking
- Error recovery and resilience testing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Run the test suite (
npm test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This module includes TypeScript declarations. If you're using TypeScript, you'll get full type checking and IntelliSense support for the Mixpanel integration.
- π‘οΈ Robust Error Handling: Comprehensive try-catch blocks prevent initialization failures
- π§ Configuration Safety: Fixed critical bugs with undefined configuration options
- π§ͺ Test Suite Improvements: Enhanced test reliability with better mock isolation
- π Better Logging: Improved error messages and debugging information
- π§ͺ 100+ Test Scenarios: Complete test coverage for all functionality
- π CI/CD Integration: Automated testing with GitHub Actions
- π TypeScript Support: Full type definitions for better development experience
- π Enhanced Documentation: Comprehensive examples and troubleshooting guides
For more detailed examples, check the /examples directory in the repository, which includes:
- Component usage patterns
- Vuex integration examples
- Middleware implementations
- Plugin configurations