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>)| Parameter | Type | Description |
|---|---|---|
userId | string | null | Unique user identifier |
traits | Record<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:
<
={() => {
(., {
: .,
: .
})
}}
>
{}
</>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
| Trait | Description |
|---|---|
email | User's email address |
name | Display name |
firstName | First name |
lastName | Last name |
plan | Subscription plan |
createdAt | Account creation date |
company | Company/organization name |
role | User role (admin, member, etc.) |