Skip to main content

Overview

The init functions provide a simplified API for using MentiQ Analytics without managing an Analytics instance directly. These functions operate on a shared default instance.

Initialization

init

Initialize MentiQ Analytics with your configuration and return the Analytics instance.
init(config: AnalyticsConfig): Analytics
config
AnalyticsConfig
required
Configuration object for the Analytics instance
return
Analytics
The initialized Analytics instance

Example

import { init } from 'mentiq-sdk';

const analytics = init({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  debug: true,
  enableAutoPageTracking: true
});

getInstance

Get the default Analytics instance.
getInstance(): Analytics | null
return
Analytics | null
The default Analytics instance, or null if not initialized

Example

import { getInstance } from 'mentiq-sdk';

const analytics = getInstance();
if (analytics) {
  // Use the instance
}

Core Tracking

track

Track a custom event using the default instance.
track(event: string, properties?: any): void
event
string
required
Name of the event to track
properties
any
Additional properties to attach to the event

Example

import { track } from 'mentiq-sdk';

track('button_clicked', {
  button_id: 'signup',
  page: 'home'
});
Make sure to call init() before using track() or you’ll see a warning in the console.

page

Track a page view using the default instance.
page(properties?: any): void
properties
any
Additional properties to attach to the page view

Example

import { page } from 'mentiq-sdk';

page({
  name: 'Product Details',
  category: 'ecommerce',
  product_id: '12345'
});

identify

Identify a user using the default instance.
identify(userId: string, traits?: any): void
userId
string
required
Unique identifier for the user
traits
any
User traits and properties

Example

import { identify } from 'mentiq-sdk';

identify('user-123', {
  email: '[email protected]',
  name: 'John Doe',
  plan: 'premium'
});

alias

Alias a user ID to another ID using the default instance.
alias(newId: string, previousId?: string): void
newId
string
required
New user ID
previousId
string
Previous user ID (optional)

Example

import { alias } from 'mentiq-sdk';

alias('user-123');

reset

Reset the analytics state using the default instance.
reset(): void

Example

import { reset } from 'mentiq-sdk';

// On user logout
reset();

flush

Manually flush the event queue using the default instance.
flush(): Promise<void>
return
Promise<void>
Promise that resolves when flush is complete

Example

import { flush } from 'mentiq-sdk';

// Before page unload
await flush();

Session Recording

startRecording

Start session recording using the default instance.
startRecording(): void

Example

import { startRecording } from 'mentiq-sdk';

startRecording();

stopRecording

Stop session recording using the default instance.
stopRecording(): void

Example

import { stopRecording } from 'mentiq-sdk';

stopRecording();

pauseRecording

Pause session recording using the default instance.
pauseRecording(): void

Example

import { pauseRecording } from 'mentiq-sdk';

pauseRecording();

resumeRecording

Resume session recording using the default instance.
resumeRecording(): void

Example

import { resumeRecording } from 'mentiq-sdk';

resumeRecording();

isRecordingActive

Check if recording is currently active using the default instance.
isRecordingActive(): boolean
return
boolean
True if recording is active, false otherwise

Example

import { isRecordingActive } from 'mentiq-sdk';

if (isRecordingActive()) {
  console.log('Recording is active');
}

Feature Tracking

trackFeatureUsage

Track usage of a specific feature using the default instance.
trackFeatureUsage(featureName: string, properties?: any): void
featureName
string
required
Name of the feature
properties
any
Additional properties

Example

import { trackFeatureUsage } from 'mentiq-sdk';

trackFeatureUsage('export_data', {
  format: 'csv',
  rows: 1000
});

Funnel Tracking

startFunnel

Start tracking a conversion funnel using the default instance.
startFunnel(funnelName: string, properties?: any): void
funnelName
string
required
Name of the funnel
properties
any
Additional properties

Example

import { startFunnel } from 'mentiq-sdk';

startFunnel('checkout');

advanceFunnel

Advance to the next step in a funnel using the default instance.
advanceFunnel(funnelName: string, stepName: string, properties?: any): void
funnelName
string
required
Name of the funnel
stepName
string
required
Name of the current step
properties
any
Additional properties

Example

import { advanceFunnel } from 'mentiq-sdk';

advanceFunnel('checkout', 'shipping_info');
advanceFunnel('checkout', 'payment_info');

completeFunnel

Mark a funnel as completed using the default instance.
completeFunnel(funnelName: string, properties?: any): void
funnelName
string
required
Name of the funnel
properties
any
Additional properties

Example

import { completeFunnel } from 'mentiq-sdk';

completeFunnel('checkout', {
  order_id: '12345',
  total: 99.99
});

abandonFunnel

Mark a funnel as abandoned using the default instance.
abandonFunnel(funnelName: string, reason?: string, properties?: any): void
funnelName
string
required
Name of the funnel
reason
string
Reason for abandonment
properties
any
Additional properties

Example

import { abandonFunnel } from 'mentiq-sdk';

abandonFunnel('checkout', 'payment_failed');

trackFunnelStep

Track a specific funnel step using the default instance.
trackFunnelStep(
  funnelName: string,
  stepName: string,
  stepIndex: number,
  properties?: any
): void
funnelName
string
required
Name of the funnel
stepName
string
required
Name of the step
stepIndex
number
required
Index of the step (0-based)
properties
any
Additional properties

getFunnelState

Get the current state of a funnel using the default instance.
getFunnelState(funnelName: string): any
funnelName
string
required
Name of the funnel
return
any
Current funnel state or null if funnel not found

Session & Engagement

getActiveSession

Get detailed session metrics using the default instance.
getActiveSession(): any
return
any
Current session data or null if not initialized

Example

import { getActiveSession } from 'mentiq-sdk';

const session = getActiveSession();
console.log('Engagement score:', session.engagementScore);

calculateEngagementScore

Calculate the current engagement score using the default instance.
calculateEngagementScore(): number
return
number
Engagement score from 0-100, or 0 if not initialized

Example

import { calculateEngagementScore } from 'mentiq-sdk';

const score = calculateEngagementScore();
if (score < 30) {
  // Show engagement prompt
}

Subscription Tracking

trackSubscriptionStarted

Track when a subscription starts using the default instance.
trackSubscriptionStarted(properties: any): void
properties
any
required
Subscription properties

Example

import { trackSubscriptionStarted } from 'mentiq-sdk';

trackSubscriptionStarted({
  plan_name: 'Pro',
  status: 'active',
  mrr: 99
});

trackSubscriptionUpgraded

Track when a subscription is upgraded using the default instance.
trackSubscriptionUpgraded(properties: any): void

trackSubscriptionDowngraded

Track when a subscription is downgraded using the default instance.
trackSubscriptionDowngraded(properties: any): void

trackSubscriptionCanceled

Track when a subscription is canceled using the default instance.
trackSubscriptionCanceled(properties: any): void

trackSubscriptionPaused

Track when a subscription is paused using the default instance.
trackSubscriptionPaused(properties: any): void

trackSubscriptionReactivated

Track when a subscription is reactivated using the default instance.
trackSubscriptionReactivated(properties: any): void

trackTrialStarted

Track when a trial starts using the default instance.
trackTrialStarted(properties: any): void

trackTrialConverted

Track when a trial converts using the default instance.
trackTrialConverted(properties: any): void

trackTrialExpired

Track when a trial expires using the default instance.
trackTrialExpired(properties: any): void

trackPaymentFailed

Track when a payment fails using the default instance.
trackPaymentFailed(properties: any): void

trackPaymentSucceeded

Track when a payment succeeds using the default instance.
trackPaymentSucceeded(properties: any): void

getSubscriptionData

Get stored subscription data using the default instance.
getSubscriptionData(): any
return
any
Subscription data or null if not available

calculateChurnRisk

Calculate churn risk using the default instance.
calculateChurnRisk(): any
return
any
Churn risk metrics or null if not initialized

Example

import { calculateChurnRisk } from 'mentiq-sdk';

const risk = calculateChurnRisk();
if (risk.risk_category === 'high') {
  // Take action to retain user
}

Complete Example

import {
  init,
  track,
  page,
  identify,
  startFunnel,
  advanceFunnel,
  completeFunnel
} from 'mentiq-sdk';

// Initialize once at app start
init({
  apiKey: 'your-api-key',
  projectId: 'your-project-id',
  enableAutoPageTracking: true
});

// Identify user on login
identify('user-123', {
  email: '[email protected]',
  name: 'John Doe'
});

// Track page views
page({ name: 'Home' });

// Track events
track('button_clicked', {
  button_id: 'cta'
});

// Track funnels
startFunnel('checkout');
advanceFunnel('checkout', 'cart');
advanceFunnel('checkout', 'shipping');
advanceFunnel('checkout', 'payment');
completeFunnel('checkout', { total: 99.99 });

Build docs developers (and LLMs) love