Learn how to use Trophy to add a streaks feature to your web or mobile app.
The guide outlines the full process of adding a streaks feature to your web or mobile app using Trophy.For illustration purposes we’ll use the example of a study platform that uses a daily streak to incentivize and reward users for viewing flashcards.
To see a fully working example of this in practice, check out the live
demo or github
repo.
In Trophy, Metrics are the building blocks of gamification and model the different interactions users make with your product.In this guide the interaction we’re interested in is flashcards-viewed, but you can create a metric that best represents the interaction you want to drive streaks from.In the Trophy dashboard, head to the metrics page and create a metric.
Once you’ve created your metric, head to the streaks page and select the streak frequency you want (daily, weekly or monthly). Then select the metric you created in the dropdown.In Trophy you track user interactions by sending Events from your code to Trophy APIs against a specific metric.When events are recorded for a specific user, Trophy will automatically check if the event is against a metric that is selected in your streak settings and update the user’s streak accordingly.This is what makes building gamified experiences with Trophy so easy, it does all the work for you behind the scenes.
By including the user’s timezone in the tz attribute, Trophy will
automatically track streaks according to the user’s local clock and handle
awkward edge cases where users change time zones.
The response to this API call is the complete set of changes to any features you’ve built with Trophy, including the latest data on the users streak.
We can use the response of the metric change API to show pop-up notifications (or ‘toasts’) when user extends their streak.Here’s an example of this in action:
Streak Extended Pop-up
Copy
Ask AI
// Sends event to Trophyconst response = await viewFlashcard();if (!response) { return;}// Show toast if user has extended their streakif (response.currentStreak?.extended) { toast({ title: "You're on a roll!", description: `Keep going to keep your ${response.currentStreak.length} day streak!`, });}
If you want to play sound effects, use the HTML5 Audio
API and feel
free to steal these audio
files
we recommend.
curl --request GET \ --url https://api.trophy.so/v1/users/{id}/streak \ --header 'X-API-KEY: <api-key>'
This API not only returns data on the user’s current streak like length and expires, but it can also return historical streak data which can be used to display any kind of streak UI you like through the historyPeriods parameter.
Trophy also supports streak freezes which can help prevent users from losing their streak if they accidentally miss a period.Learn more about streak freezes in the dedicated streak freezes docs.