Skip to main content

Overview

The MentiQ Analytics SDK provides multiple methods for tracking events, user interactions, and application behavior. Events are automatically enriched with context data including user information, session details, and page metadata.

Basic Event Tracking

Track Custom Events

The track() method is the primary way to send custom events:
analytics.track('button_clicked', {
  button_id: 'signup-button',
  location: 'homepage',
});
event
string
required
The name of the event to track
properties
EventProperties
Optional object containing event properties. Values can be strings, numbers, booleans, null, or undefined.

Page Views

Track page views manually or rely on automatic tracking:
// Manual page tracking
analytics.page({
  title: 'Product Details',
  url: window.location.href,
  path: '/products/123',
});

// Automatic page tracking (enabled by default)
const analytics = new Analytics({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  enableAutoPageTracking: true, // Default
});
properties
PageProperties
Optional page properties:
  • title: Page title
  • url: Full URL
  • path: URL path
  • referrer: Referrer URL
  • search: Query string

Feature Tracking

Track Feature Usage

Monitor which features users are engaging with:
analytics.trackFeatureUsage('export_data', {
  format: 'csv',
  record_count: 150,
});
featureName
string
required
Name of the feature being used
properties
EventProperties
Optional properties describing the feature usage

Funnel Tracking

Start a Funnel

Begin tracking a user journey through a conversion funnel:
analytics.startFunnel('signup_flow', {
  referral_source: 'landing_page',
});

Advance Funnel Steps

Track progress through funnel steps:
analytics.advanceFunnel('signup_flow', 'email_entered', {
  email_provider: 'gmail',
});

analytics.advanceFunnel('signup_flow', 'password_created');

analytics.advanceFunnel('signup_flow', 'profile_completed');

Complete a Funnel

Mark a funnel as successfully completed:
analytics.completeFunnel('signup_flow', {
  signup_method: 'email',
  time_to_complete: 120000, // milliseconds
});

Abandon a Funnel

Track funnel abandonment with optional reason:
analytics.abandonFunnel('signup_flow', 'form_validation_error', {
  error_field: 'password',
});
Funnels automatically abandon after 5 minutes of inactivity and track a timeout reason.

Get Funnel State

Retrieve the current state of an active funnel:
const state = analytics.getFunnelState('signup_flow');
// Returns: { funnelName, currentStep, startTime, steps, isActive, timeInFunnel }

Performance Tracking

Automatic Performance Tracking

Enable automatic tracking of Core Web Vitals:
const analytics = new Analytics({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  enablePerformanceTracking: true,
});
This automatically tracks:
  • Load Time
  • DOM Ready Time
  • First Byte Time
  • DNS Lookup Time
  • First Paint
  • First Contentful Paint
  • Largest Contentful Paint
  • First Input Delay
  • Cumulative Layout Shift
  • Time to Interactive

Manual Performance Tracking

Track custom performance metrics:
analytics.trackPerformance({
  loadTime: 1250,
  firstContentfulPaint: 850,
  largestContentfulPaint: 1500,
  timeToInteractive: 2000,
});
performanceData
PerformanceData
required
Object containing performance metrics:
  • loadTime: Total page load time in ms
  • domReady: DOM ready time in ms
  • firstPaint: First paint time in ms
  • firstContentfulPaint: FCP in ms
  • largestContentfulPaint: LCP in ms
  • firstInputDelay: FID in ms
  • cumulativeLayoutShift: CLS score
  • timeToInteractive: TTI in ms

Error Tracking

Automatic Error Tracking

Enable automatic tracking of JavaScript errors:
const analytics = new Analytics({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  enableErrorTracking: true,
});
This automatically tracks:
  • JavaScript runtime errors
  • Unhandled promise rejections

Manual Error Tracking

Track custom errors:
try {
  // Some operation
  riskyOperation();
} catch (error) {
  analytics.trackCustomError(error, {
    operation: 'data_processing',
    severity: 'high',
  });
}

// Track error with string message
analytics.trackCustomError('API request failed', {
  endpoint: '/api/users',
  status_code: 500,
});
error
string | Error
required
Error object or error message string
properties
EventProperties
Optional properties providing error context

Advanced Event Tracking

Funnel Step Tracking

Track specific funnel steps with index:
analytics.trackFunnelStep(
  'checkout',
  'payment_info',
  2,
  { payment_method: 'credit_card' }
);
funnelName
string
required
Name of the funnel
stepName
string
required
Name of the current step
stepIndex
number
required
Zero-based index of the step
properties
EventProperties
Optional step properties

Event Context

All events are automatically enriched with contextual information:
  • User Context: User ID, anonymous ID, email (if available)
  • Session Context: Session ID, session duration, engagement score
  • Page Context: URL, path, title, referrer
  • Device Context: User agent, screen resolution, timezone, locale
  • Library Context: SDK name and version
  • Channel Context: Acquisition channel (UTM parameters, referrer analysis)
  • Subscription Context: Subscription status, plan, MRR (if available)

Event Queue Management

Check Queue Size

const queueSize = analytics.getQueueSize();
console.log(`${queueSize} events queued`);

Flush Events Manually

Send all queued events immediately:
await analytics.flush();

Clear Queue

Remove all queued events without sending:
analytics.clearQueue();
Clearing the queue will permanently delete unsent events.

Best Practices

Use consistent naming conventions for events:
  • Use snake_case: button_clicked, form_submitted
  • Be descriptive: checkout_completed instead of done
  • Include context: signup_button_clicked instead of clicked
Keep property names consistent across similar events for easier analysis.
Events are batched and sent automatically based on batchSize and flushInterval configuration. You can manually flush events with flush() when needed (e.g., before page unload).

Common Event Examples

// User interactions
analytics.track('video_played', {
  video_id: 'intro-video',
  duration: 120,
  autoplay: false,
});

// E-commerce
analytics.track('product_added_to_cart', {
  product_id: 'SKU-123',
  product_name: 'Blue T-Shirt',
  price: 29.99,
  quantity: 2,
});

// Content engagement
analytics.track('article_read', {
  article_id: 'post-456',
  read_percentage: 75,
  time_spent: 180, // seconds
});

// Social actions
analytics.track('content_shared', {
  content_type: 'article',
  content_id: '789',
  platform: 'twitter',
});

Build docs developers (and LLMs) love