docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a Nuxt.js 2 application that tracks user interactions and page navigation across different parts of the application lifecycle. The application should demonstrate analytics tracking in components, middleware, and plugins.
Create a user dashboard page with the following features:
Dashboard Component (pages/dashboard.vue):
Navigation Middleware (middleware/analytics.js):
Analytics Plugin (plugins/analytics-helper.js):
$trackErrorError Tracking:
$trackError helper to track an error event with the message "Test error triggered"Provides Google Analytics integration for Nuxt.js applications.
File: pages/dashboard.test.js { .test-file-name }
import { mount } from '@vue/test-utils'
import Dashboard from './dashboard.vue'
describe('Dashboard Page', () => {
test('displays welcome message and buttons', () => {
const wrapper = mount(Dashboard, {
mocks: {
$ga: {
page: jest.fn(),
event: jest.fn()
},
$trackError: jest.fn()
}
})
expect(wrapper.text()).toContain('Welcome')
expect(wrapper.find('button').exists()).toBe(true)
})
})File: middleware/analytics.test.js { .test-file-name }
import analyticsMiddleware from './analytics.js'
describe('Analytics Middleware', () => {
test('tracks route navigation', () => {
const mockContext = {
route: { path: '/dashboard' },
$ga: {
event: jest.fn()
}
}
analyticsMiddleware(mockContext)
expect(mockContext.$ga.event).toHaveBeenCalled()
})
})File: plugins/analytics-helper.test.js { .test-file-name }
describe('Analytics Helper Plugin', () => {
test('trackError helper is available and tracks errors', () => {
const mockContext = {
$ga: {
event: jest.fn()
}
}
// Simulate plugin injection
const trackError = (message) => {
mockContext.$ga.event({
eventCategory: 'error',
eventAction: message
})
}
trackError('Test error')
expect(mockContext.$ga.event).toHaveBeenCalledWith(
expect.objectContaining({
eventCategory: 'error'
})
)
})
})