Skip to main content

useMentiqAnalytics

Core hook for accessing the analytics instance and tracking methods.

Returns

track
(event: string, properties?: any) => void
Track custom events
page
(properties?: any) => void
Track page views
identify
(userId: string, properties?: any) => void
Identify users
reset
() => void
Reset user identity
flush
() => Promise<void>
Flush pending events
trackCustomError
(error: string | Error, properties?: any) => void
Track custom errors
analytics
AnalyticsInstance
Raw analytics instance

Usage

import { useMentiqAnalytics } from 'mentiq-sdk';

function Button() {
  const { track } = useMentiqAnalytics();

  const handleClick = () => {
    track('button_clicked', {
      button_name: 'Subscribe',
      location: 'header',
    });
  };

  return <button onClick={handleClick}>Subscribe</button>;
}

usePageTracking

Automatically track page views when component mounts.

Parameters

properties
EventProperties
Optional properties to include with the page view

Usage

import { usePageTracking } from 'mentiq-sdk';

function AboutPage() {
  usePageTracking({
    title: 'About Us',
    category: 'Marketing',
  });

  return <div>About Us Content</div>;
}

useInteractionTracking

Track user interactions like clicks, hovers, and views.

Returns

trackClick
(element: string, properties?: EventProperties) => void
Track element clicks
trackHover
(element: string, properties?: EventProperties) => void
Track element hovers
trackView
(element: string, properties?: EventProperties) => void
Track element views

Usage

import { useInteractionTracking } from 'mentiq-sdk';

function ProductCard({ product }) {
  const { trackClick, trackHover } = useInteractionTracking();

  return (
    <div
      onMouseEnter={() => trackHover('product_card', { product_id: product.id })}
      onClick={() => trackClick('product_card', { product_id: product.id })}
    >
      <h3>{product.name}</h3>
      <p>{product.price}</p>
    </div>
  );
}

useElementTracking

Track when an element becomes visible using Intersection Observer.

Parameters

elementRef
React.RefObject<HTMLElement>
required
Reference to the element to track
event
string
default:"element_viewed"
Event name to track
properties
EventProperties
Properties to include with the event
options
object
Tracking options
  • threshold: Visibility threshold (0-1, default: 0.5)
  • delay: Delay before tracking in ms (default: 1000)
  • once: Track only once (default: true)

Usage

import { useElementTracking } from 'mentiq-sdk';
import { useRef } from 'react';

function Hero() {
  const heroRef = useRef<HTMLDivElement>(null);

  useElementTracking(
    heroRef,
    'hero_viewed',
    { section: 'homepage' },
    { threshold: 0.8, delay: 2000 }
  );

  return <div ref={heroRef}>Hero Content</div>;
}

useSessionTracking

Access current session data and metrics.

Returns

sessionData
SessionData
Complete session data object
sessionId
string
Current session ID
isActive
boolean
Whether session is currently active
duration
number
Session duration in milliseconds
pageViews
number
Number of page views in session
clicks
number
Number of clicks in session
scrollDepth
number
Maximum scroll depth percentage

Usage

import { useSessionTracking } from 'mentiq-sdk';

function SessionDebugger() {
  const { sessionId, duration, pageViews, clicks } = useSessionTracking();

  return (
    <div>
      <p>Session: {sessionId}</p>
      <p>Duration: {Math.round(duration / 1000)}s</p>
      <p>Pages: {pageViews}</p>
      <p>Clicks: {clicks}</p>
    </div>
  );
}

useErrorTracking

Automatically track JavaScript errors and unhandled promise rejections.

Returns

trackJavaScriptError
(error: Error, properties?: EventProperties) => void
Manually track JavaScript errors
trackCustomError
(message: string, properties?: EventProperties) => void
Track custom error messages

Usage

import { useErrorTracking } from 'mentiq-sdk';

function App() {
  // Automatically tracks all JS errors and unhandled rejections
  useErrorTracking();

  return <YourApp />;
}

usePerformanceTracking

Track performance metrics and custom performance measurements.

Returns

measureCustomPerformance
(label: string) => { start: () => void; end: () => void }
Create custom performance measurements

Usage

import { usePerformanceTracking } from 'mentiq-sdk';

function App() {
  // Automatically tracks page load performance
  usePerformanceTracking();

  return <YourApp />;
}
Automatically tracks:
  • Load time
  • DOM ready time
  • First paint
  • First contentful paint

useAnalytics

Alias for useMentiqAnalytics for backward compatibility.
import { useAnalytics } from 'mentiq-sdk';

// Same as useMentiqAnalytics
const { track, identify } = useAnalytics();

Build docs developers (and LLMs) love