Event Tracking
Last updated
Last updated
Event tracking is the foundation of analytics in Mtrix. Events represent user actions, system occurrences, or any custom activities you want to monitor.
To track a simple event:
mtrix.trackEvent('CTAClicked');
Events can include additional properties to provide context:
mtrix.trackEvent('ProductViewed', {
productId: '12345',
productName: 'Wireless Headphones',
category: 'Electronics',
price: 99.99,
currency: 'USD'
});
If you're not using Mtrix as your infrastructure, you need to track events manually. Only the Page View event will fire automatically on every history change --you can opt-out automatic event capturing on the initialization stage.
Page Views (*required)
// For manual tracking
mtrix.trackEvent('PageView', {
category: 'Blog',
author: 'John Doe'
});
Purchase Event (*required)
Purchase event is the main conversion event. It must fire whenever your experiment reaches its goal. Required field listed below:
mtrix.trackEvent('Purchase', {
transactionId: 'unique-transaction-id', // required
revenue: '129.99' // required
});
Customers who uses Mtrix as their infrastructure will receive the below events out-of-the box:
AddToCart
BeginCheckout
CompleteCheckout
Purchase
Refund
Custom Event Validation
You can add custom validation logic before sending events:
mtrix.init({
// ... other config
options: {
events: {
validateEvent: (eventName, properties) => {
// Don't track events with missing required properties
if (eventName === 'Purchase' && !properties.transactionId) {
console.warn('Purchase event missing orderId');
return false;
}
return true;
}
}
}
});
Debugging Events
During development, you can enable debug mode to see detailed logs:
mtrix.init({
// ... other config
options: {
debug: true
}
});
// Now events will be logged to console
mtrix.trackEvent('DebugEvent', { test: true });
// Console: [Mtrix] Event tracked: DebugEvent { test: true }