StartupKitstartupkit
AnalyticsUsage

identify

Associate events with a user

Associate analytics events with a user identity.

Client

Use the useAnalytics hook in client components:

import {  } from "@repo/analytics"

const {  } = ()

Usage

identify(userId: string | null, traits?: Record<string, unknown>)
ParameterTypeDescription
userIdstring | nullUnique user identifier
traitsRecord<string, unknown>Optional user properties

Examples

"use client"

import {  } from "@repo/analytics"

export function () {
  const {  } = ()

  async function (: string, : string) {
    const {  } = await verifyAuthCode(, )
    
    (.id, {
      : .email,
      : .name,
      : .plan
    })
    
    return 
  }

  return {  }
}

With AuthProvider

The AuthProvider can automatically identify users:

app/providers.tsx
<
  ={() => {
    (., {
      : .,
      : .
    })
  }}
>
  {}
</>

Server

Server-side identification happens automatically when you include user traits in track() calls:

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

Usage

PostHog automatically associates traits when you track events with a userId:

await ({
  : "USER_SIGNED_UP",
  : .,
  : {
    : .,
    : .,
    : .
  }
})

Examples

After sign-up

"use server"

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

export async function (: string) {
  const  = await createUser({  })
  
  await ({
    : "USER_SIGNED_UP",
    : .id,
    : {
      : .id,
      : .email,
      : .firstName,
      : .lastName
    }
  })
  
  return 
}

Plan upgrade

"use server"

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

export async function (: string, : string) {
  await db.update(users).set({  }).where(eq(users.id, ))
  
  await ({
    : "PLAN_UPGRADED",
    ,
    ,
    : "free"
  })
}

Common traits

TraitDescription
emailUser's email address
nameDisplay name
firstNameFirst name
lastNameLast name
planSubscription plan
createdAtAccount creation date
companyCompany/organization name
roleUser role (admin, member, etc.)

On this page