Overview
The MentiQ Analytics SDK is initialized with an AnalyticsConfig object that controls its behavior, tracking features, and performance characteristics.
Basic Configuration
import { Analytics } from 'mentiq-sdk';
const analytics = new Analytics({
apiKey: 'your-api-key',
projectId: 'your-project-id',
});
Configuration Options
Required Parameters
Your MentiQ API key for authentication
Your MentiQ project identifier
Endpoint Configuration
endpoint
string
default:"https://api.mentiq.io"
Custom API endpoint URL for self-hosted or regional deployments
User Identification
Initial user ID to set on initialization. Can also be set later using identify()
Session Management
Session timeout in milliseconds. Default is 30 minutes (30 * 60 * 1000)
Event Batching
Number of events to batch before automatic flush
Interval in milliseconds to automatically flush events. Default is 10 seconds
Maximum number of events to queue before dropping oldest events
Retry Configuration
Number of times to retry failed event sends
Initial delay in milliseconds before retry (uses exponential backoff)
Automatic Tracking
Automatically track page views, including SPA navigation (pushState/replaceState)
enablePerformanceTracking
Automatically track page performance metrics (load time, FCP, LCP, etc.)
Enable heatmap tracking for clicks, mouse movements, and scrolls
Enable session recording functionality
Automatically track JavaScript errors and unhandled promise rejections
A/B Testing
Enable A/B testing features
Additional A/B testing configuration options
Debugging
Enable debug logging to console
Advanced Configuration Examples
Full-Featured Setup
const analytics = new Analytics({
apiKey: 'your-api-key',
projectId: 'your-project-id',
endpoint: 'https://api.mentiq.io',
debug: true,
// User identification
userId: 'user-123',
// Session management
sessionTimeout: 30 * 60 * 1000, // 30 minutes
// Event batching
batchSize: 20,
flushInterval: 10000, // 10 seconds
maxQueueSize: 1000,
// Retry configuration
retryAttempts: 3,
retryDelay: 1000,
// Automatic tracking
enableAutoPageTracking: true,
enablePerformanceTracking: true,
enableHeatmapTracking: true,
enableSessionRecording: false,
enableErrorTracking: true,
// A/B Testing
enableABTesting: true,
abTestConfig: {
enableABTesting: true,
assignmentCacheTTL: 5 * 60 * 1000, // 5 minutes
autoTrackExposures: true,
},
});
Minimal Configuration
const analytics = new Analytics({
apiKey: 'your-api-key',
projectId: 'your-project-id',
});
const analytics = new Analytics({
apiKey: 'your-api-key',
projectId: 'your-project-id',
// Larger batches, less frequent flushes
batchSize: 50,
flushInterval: 30000, // 30 seconds
// Disable resource-intensive features
enableHeatmapTracking: false,
enableSessionRecording: false,
enablePerformanceTracking: false,
});
Development Configuration
const analytics = new Analytics({
apiKey: 'dev-api-key',
projectId: 'dev-project',
debug: true,
// Smaller batches for faster feedback
batchSize: 5,
flushInterval: 2000, // 2 seconds
// Enable all tracking for testing
enableAutoPageTracking: true,
enablePerformanceTracking: true,
enableHeatmapTracking: true,
enableErrorTracking: true,
});
Accessing Configuration
You can access the current configuration using the config property:
const apiKey = analytics.config.apiKey;
const debug = analytics.config.debug;
Runtime Updates
Most configuration options are set at initialization and cannot be changed at runtime. To change configuration, create a new Analytics instance:
// Destroy old instance
analytics.destroy();
// Create new instance with updated config
const analytics = new Analytics({
apiKey: 'your-api-key',
projectId: 'your-project-id',
debug: false, // Updated configuration
});
Best Practices
Use environment variables for sensitive configuration like API keys:const analytics = new Analytics({
apiKey: process.env.MENTIQ_API_KEY,
projectId: process.env.MENTIQ_PROJECT_ID,
});
Be cautious with enableHeatmapTracking and enableSessionRecording as they can generate large amounts of data and impact performance.
Enable debug mode during development to see detailed logs of SDK operations.