Learn how to use Trophy to add a leaderboards feature to your web or mobile app.
The guide outlines the full process of adding a leaderboards 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 leaderboard to create friendly competition around 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 build leaderboards around.In the Trophy dashboard, head to the metrics page and create a metric.
Once you’ve created your metric, head to the leaderboards page and create the leaderboards you want. You can find all the details on the types of leaderboards and the different use cases in the leaderboards docs.
For the purposes of this guide we’ve set up a daily leaderboard that tracks the total XP a user earns by viewing flashcards.
For a full guide on adding an XP feature to your web or mobile app, check out
our full guide.
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 automatically updates their ranking in each leaderboard they are a part of.This is what makes building gamified experiences with Trophy so easy, it does all the work for you behind the scenes.
The response to this API call is the complete set of changes to any features you’ve built with Trophy, including any changes to their ranking in any leaderboards they are a part of.
We can use the response of the metric change API to show pop-up notifications (or ‘toasts’) when users move up the rankings.Here’s an example of this in action:
Leaderboard Rank Up Pop-up
Copy
Ask AI
// Sends event to Trophyconst response = await viewFlashcard();if (!response) { return;}const leaderboard = response.leaderboards["daily_champions"];if (!leaderboard) { return;}// Show toasts if the user moved up the leaderboardif (leaderboard.rank > leaderboard.previousRank) { toast({ title: "You're on the move!, description: `You moved up ${leaderboard.previousRank - leaderboard.rank} places!, });}
If you want to play sound effects, use the HTML5 Audio
API and feel
free to steal these audio
files
we recommend.
Use the user leaderboard API to fetch data on how a specific user’s rank has changed over time in a particular leaderboard.
Copy
Ask AI
curl --request GET \ --url https://api.trophy.so/v1/users/{id}/leaderboards/{key} \ --header 'X-API-KEY: <api-key>'
Here’s an example of the data returned from the user leaderboard rankings API which includes the users current rank in the rank attribute and an array of previous ranks in the history attribute:
The leaderboards page in Trophy shows how many users are actively participating in a leaderboard, as well as a measure of competitiveness based on how many rank changes are occurring.The analytics page also shows a distribution of users scores to help identify clusters of users within rankings.
Additionally the leaderboard rankings page shows current and past rankings of a leaderboard: