StartupKitstartupkit
AnalyticsUsage

track

Track custom events

Track custom events with optional properties.

Client

Use the useAnalytics hook in client components:

import {  } from "@repo/analytics"

const {  } = ()

Usage

track(event: string, properties?: Record<string, unknown>)
ParameterTypeDescription
eventstringEvent name (e.g., "SIGNUP_CLICKED")
propertiesRecord<string, unknown>Optional event properties

Examples

"use client"

import {  } from "@repo/analytics"

export function () {
  const {  } = ()

  return (
    < ={() => ("SIGNUP_CLICKED", { : "hero" })}>
      Get Started
    </>
  )
}
("PURCHASE_COMPLETED", {
  : 99.99,
  : "USD",
  : "pro-plan"
})

Server

Use the track function from @repo/analytics/server in Server Actions, API routes, or server components:

import {  } from "@repo/analytics/server"

Usage

await track(event: TrackableEvent | TrackableEvent[])

Server-side tracking requires a userId or anonymousId:

interface TrackableEvent {
  : string
  : string
  ?: string
  [: string]: unknown
}

Examples

Server Action

"use server"

import {  } from "@repo/analytics/server"
import {  } from "@repo/auth/server"

export async function (: string) {
  const {  } = await ()
  
  const  = await db.insert(teams).values({  }).returning()
  
  await ({
    : "TEAM_CREATED",
    : .id,
    : .id,
    : 
  })
  
  return 
}

API Route

app/api/purchase/route.ts
import {  } from "@repo/analytics/server"

export async function (: Request) {
  const { , ,  } = await .()
  
  await ({
    : "PURCHASE_COMPLETED",
    ,
    ,
    
  })
  
  return .({ : true })
}

Batch tracking

await ([
  { : "TEAM_CREATED", : ., : . },
  { : "TEAM_JOINED", : ., : . }
])

Event naming conventions

Use SCREAMING_SNAKE_CASE for consistency:

  • SIGNUP_CLICKED
  • PURCHASE_COMPLETED
  • FEATURE_USED
  • TEAM_CREATED
  • ERROR_OCCURRED

On this page