Skip to main content

Event Tracking

Analytics

Track custom events, user actions, and conversion funnels in your application.

Custom Events

Track any user action

Page Views

Automatic or manual tracking

Batch Events

Send multiple events at once

Privacy First

GDPR-compliant by default

Track Events

Track custom events with properties:

Server-side
import { platform } from '@/lib/platform'

// Track a simple event
await platform.analytics.track({
  event: 'button_clicked',
  userId: user.id,
})

// Track with properties
await platform.analytics.track({
  event: 'purchase_completed',
  userId: user.id,
  properties: {
    orderId: 'ord_123',
    amount: 99.99,
    currency: 'USD',
    items: ['item_1', 'item_2'],
  },
})

// Track anonymous events
await platform.analytics.track({
  event: 'page_viewed',
  anonymousId: 'anon_abc123', // For non-logged-in users
  properties: {
    path: '/pricing',
    referrer: 'https://google.com',
  },
})
Client-side (React)
'use client'

import { useAnalytics } from '@sylphx/sdk/react'

export function CheckoutButton() {
  const { track } = useAnalytics()

  const handleClick = () => {
    track('checkout_started', {
      cartValue: 149.99,
      itemCount: 3,
    })

    // Continue with checkout...
  }

  return <button onClick={handleClick}>Checkout</button>
}

Page Views

Track page views automatically or manually:

Automatic tracking (Next.js)
// app/providers.tsx
'use client'

import { SylphxProvider } from '@sylphx/sdk/react'

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <SylphxProvider
      appId={process.env.NEXT_PUBLIC_SYLPHX_APP_ID!}
      analytics={{
        trackPageViews: true, // Auto-track route changes
      }}
    >
      {children}
    </SylphxProvider>
  )
}
Manual tracking
import { useAnalytics } from '@sylphx/sdk/react'
import { usePathname } from 'next/navigation'
import { useEffect } from 'react'

export function PageViewTracker() {
  const { page } = useAnalytics()
  const pathname = usePathname()

  useEffect(() => {
    page(pathname, {
      title: document.title,
      referrer: document.referrer,
    })
  }, [pathname, page])

  return null
}

Event Naming

Best practices for naming events:

snake_casefor event names
object_actionformat (e.g., button_clicked)
PropertyTypeDescription
signup_completed✓ GoodClear object + action format
SignUpCompleted✗ BadPascalCase is inconsistent
product_added_to_cart✓ GoodSpecific and descriptive
addToCart✗ BadcamelCase, no object
search_performed✓ GoodUses past tense verb
search✗ BadToo vague, no action

Batch Events

Send multiple events in a single request for better performance:

import { platform } from '@/lib/platform'

await platform.analytics.trackBatch([
  {
    event: 'item_viewed',
    userId: user.id,
    properties: { itemId: 'item_1' },
    timestamp: new Date('2024-01-15T10:00:00Z'),
  },
  {
    event: 'item_added_to_cart',
    userId: user.id,
    properties: { itemId: 'item_1', quantity: 2 },
    timestamp: new Date('2024-01-15T10:01:00Z'),
  },
  {
    event: 'checkout_started',
    userId: user.id,
    properties: { cartValue: 59.98 },
    timestamp: new Date('2024-01-15T10:02:00Z'),
  },
])

Common Events

Recommended events to track for common use cases:

User Lifecycle

track('signup_started')
track('signup_completed', { method: 'email' })
track('login_completed', { method: 'google' })
track('profile_updated', { fields: ['name', 'avatar'] })
track('subscription_started', { plan: 'pro' })

E-commerce

track('product_viewed', { productId, name, price })
track('product_added_to_cart', { productId, quantity })
track('checkout_started', { cartValue, itemCount })
track('payment_submitted', { paymentMethod: 'card' })
track('order_completed', { orderId, total, items })

Content

track('article_viewed', { articleId, category })
track('video_started', { videoId, duration })
track('video_completed', { videoId, watchTime })
track('content_shared', { contentId, platform: 'twitter' })

Conversion Tracking

Track conversions with automatic click ID capture and user enrichment:

useConversionTracking hook
import { useConversionTracking } from '@sylphx/sdk/react'

function CheckoutSuccess({ order }) {
  const { trackConversion, primaryClickId } = useConversionTracking()

  useEffect(() => {
    // Auto-enriched with click IDs (gclid, fbclid, ttclid) and user data
    trackConversion('purchase', {
      value: order.total,
      currency: 'USD',
      orderId: order.id,
    })
  }, [order])

  return <div>Thank you!</div>
}

Zero Configuration

Click IDs are automatically captured from URL on page load. User email and name are auto-enriched from your auth state. See Conversion Destinations for details.

Reserved Properties

These properties are automatically captured:

PropertyTypeDescription
timestampDateWhen the event occurred
userId / anonymousIdstringWho triggered the event
$browserstringBrowser name (client-side)
$devicestringDevice type (client-side)
$osstringOperating system (client-side)
$referrerstringReferrer URL (client-side)
$ipstringIP address (hashed for privacy)

Privacy

IP addresses are hashed by default. You can configure additional privacy settings in your dashboard.