Skip to main content

Overview

The useAnalytics hook (alias for useMentiqAnalytics) provides access to all core analytics tracking methods. It must be used within a MentiqAnalyticsProvider.

Usage

import { useAnalytics } from 'mentiq-sdk';

function MyComponent() {
  const { track, page, identify, analytics } = useAnalytics();

  const handleButtonClick = () => {
    track('button_clicked', { button_name: 'Submit' });
  };

  return <button onClick={handleButtonClick}>Submit</button>;
}

Returns

track
function
Track custom events with optional propertiesSignature: (event: string, properties?: EventProperties) => void
track('purchase_completed', {
  product_id: '123',
  amount: 99.99,
  currency: 'USD'
});
page
function
Track page views with optional page propertiesSignature: (properties?: PageProperties) => void
page({
  title: 'Dashboard',
  url: '/dashboard',
  path: '/dashboard'
});
identify
function
Identify users and update their traitsSignature: (userId: string, properties?: UserProperties) => void
identify('user-123', {
  email: '[email protected]',
  name: 'John Doe',
  plan: 'pro'
});
reset
function
Reset the analytics instance and clear user dataSignature: () => void
reset(); // Call on logout
flush
async function
Manually flush queued events to the serverSignature: () => Promise<void>
await flush();
trackCustomError
function
Track custom errors or exceptionsSignature: (error: string | Error, properties?: EventProperties) => void
trackCustomError(new Error('API request failed'), {
  endpoint: '/api/users',
  status: 500
});
analytics
AnalyticsInstance
Direct access to the full analytics instance with all methods and propertiesProvides access to advanced methods like getSessionId(), getUserId(), trackFeatureUsage(), and subscription tracking.

Type Definitions

EventProperties

interface EventProperties {
  [key: string]: string | number | boolean | null | undefined;
}

PageProperties

interface PageProperties {
  title?: string;
  url?: string;
  path?: string;
  referrer?: string;
  search?: string;
}

UserProperties

interface UserProperties {
  [key: string]: string | number | boolean | null | undefined;
  subscription?: SubscriptionProperties;
}

Notes

This hook must be used within a MentiqAnalyticsProvider. It will throw an error if used outside the provider context.
Use useAnalytics for basic tracking. For specialized tracking like errors, performance, or sessions, use the dedicated hooks.

Build docs developers (and LLMs) love