Every user interaction forms part of their behavior pattern and is the basis for building any kind of gamification experience. Whether it’s achievements, streaks or leaderboards, all these features require continual tracking of user behavior to work.
Here’s an example of the types of gamification experiences that can be powered by user behavior tracking:
Example of gamification features powered by user behavior tracking
Trophy is a platform for building gamified experiences in any web or mobile application and provides developer-friendly APIs for tracking user interactions using metrics. Watch how Charlie integrates user behavior tracking into a NextJS application in under 5 minutes.
Integrating Trophy Into a NextJS app in Under 5 mins
Key Points
- Set up a Trophy metric for each important user interaction.
- Use your existing user IDs, without managing any new IDs specifically for Trophy.
- Trophy has type-safe SDKs available in most major programming languages.
- Trophy can track interactions from a global user base, and provides APIs to fetch achievement and streak data in your users local timezone.
- Trophy can automatically send engagement email campaigns to your users from your custom domain using the proper inbox-friendly markup (<table> not <div>).
Powering Gamification Features
The integration in the video lays the foundations for building a great gamification experience. Once set up, Trophy metrics can support a wide range of features including:
- Multi-stage and one-off achievements
- Daily, weekly and monthly streaks
- Automated emails for achievement unlocked, usage recap and win-back reactivation workflows.
Server-Authoritative Gamification With Next.js Server Actions
Scoring has to run on the server. If the browser can write its own points or achievement progress, a user can open the console and award themselves whatever they want. Trophy fixes this at the boundary: every call to the metrics API is server-to-server, so the authoritative score is never set in the client. Spoofed values and duplicate awards never reach your data.
Next.js Server Actions give you a clean place to do this. A "use server" function runs on the server, reads the session, and sends the event to Trophy using the user's existing ID. No score ever crosses the wire from the client.
// app/actions/track.ts
"use server";
import { revalidatePath } from "next/cache";
import { auth } from "@/lib/auth";
import { trophy } from "@/lib/trophy";
export async function logLesson() {
const session = await auth();
if (!session) throw new Error("Not authenticated");
// Metrics are Trophy's core primitive. The user is your existing user ID.
await trophy.metrics.event("lessons-completed", {
user: { id: session.user.id },
value: 1,
});
// Re-render server components so points, streak, and achievement state
// reflect the write Trophy just confirmed.
revalidatePath("/dashboard");
}
On the client, wrap the action in useOptimistic so the UI moves the moment the user acts, then let revalidatePath reconcile it with the confirmed server state.
"use client";
import { useOptimistic, startTransition } from "react";
import { logLesson } from "@/app/actions/track";
export function CompleteButton({ points }: { points: number }) {
const [optimisticPoints, addOptimistic] = useOptimistic(
points,
(current, delta: number) => current + delta,
);
return (
<button
onClick={() =>
startTransition(async () => {
addOptimistic(10);
await logLesson();
})
}
>
Complete lesson ({optimisticPoints} pts)
</button>
);
}
The optimistic number is a preview. The server is the source of truth, and that is what makes scoring tamper-proof.
Custom Context API + useReducer vs. a Gamification API: When Each Makes Sense
A custom Context API and useReducer setup is a real option, and sometimes the right one. It fits when your gamification state is small and self-contained. Use it for a single app you fully control, where state never needs to persist across devices or survive a server restart. A progress bar that resets each session does not need a backend.
What A Hand-Rolled Build Has To Own
Build it yourself and four jobs land on your team, and each one grows over time.
The first job is state. A reducer holds the current points, streak count, earned achievements, and level, and every user action dispatches through it. The logic looks simple until you add persistence, because a reducer in memory forgets everything on refresh.
The second job is memoization. As that state grows, every consumer of the context re-renders whenever any part of it changes. You wrap values in useMemo and split contexts to stop a leaderboard update from re-rendering a points badge.
The third job is rendering at scale. A leaderboard with thousands of rows will stutter unless you virtualize the list and batch updates. This is standard frontend work, but it is work you now own on top of the gamification itself.
The fourth job is social. Leaderboards and shared achievements need a server, because ranking users against each other cannot happen in one browser. Now you aggregate events across users, guard against duplicate awards, compute streak windows in each time zone, and keep it all in sync.
Where A Headless Gamification API Fits
A headless gamification API removes the server-side half of that list. Trophy owns the metric, achievement, streak, and leaderboard logic behind an API. Duplicate-award prevention, timezone-correct streaks, event aggregation, and ranking at scale stop being your code to maintain. Your app keeps full control of the UI, and Trophy stays headless with an open-source UI kit you can install or ignore.
| Concern | Custom Context API + useReducer | Trophy |
|---|---|---|
| Local UI state | Strong fit, lives in the browser | Your UI code, unchanged |
| Cross-device persistence | You build and host a backend | Included through the API |
| Streak and timezone handling | You write DST and travel logic | Computed in each user's zone |
| Duplicate-award prevention | You enforce it per action | Enforced server-side |
| Leaderboard aggregation at scale | You aggregate and rank events | Handled by the API |
| Social features | You build the shared server layer | Built into the platform |
| Maintenance ownership | Yours, ongoing | Trophy's, ongoing |
Deciding Between Build And API
Choose the custom build when your state stays local and never leaves the browser. Choose an API once you need cross-device persistence or leaderboards that rank users at scale. Building the server side yourself is a 6–12 month project. Wiring up an API is closer to 1–2 weeks.
Start Building With Trophy
Trophy provides scalable user behavior tracking infrastructure built from the ground up with features designed to best aid building gamification experiences like achievements, streaks and re-engagement emails with no custom code.
Create a free account and start tracking user behavior using our developer-first APIs in just a few lines of code.

Get the latest on gamification
Product updates, best practices, and insights on retention and engagement — delivered straight to your inbox.
The gamification layer for consumer apps
Drop-in gamification features you can ship this sprint. Increase retention and user engagement without sacrificing your roadmap.
Book a call