Learn how to use Trophy to add an achievements feature to your web or mobile app.
The guide outlines the full process of adding an achievements feature to your web or mobile app using Trophy.For illustration purposes we’ll use the example of a study platform that uses achievements 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 build achievements around.In the Trophy dashboard, head to the metrics page and create a metric.
Once you’ve created your metric, head to the achievements page and create the achievements you want. You can find all the details on the types of achievements and the different use cases in the achievements docs.For the purposes of this guide we’ve set up a couple of achievements based on an increasing number of flashcards flipped:
10 flashcards
50 flashcards
100 flashcards
250 flashcards
1,000 flashcards
We’ve also set up a few achievements related to Streaks, but we won’t go into detail on these in this guide.
For a full guide on adding a streaks 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, any achievements linked to the specified metric will be completed automatically if the requirements are met.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 achievements that were unlocked as a result of the event.
We can use the response of the metric change API to show pop-up notifications (or ‘toasts’) when users complete achievements.Here’s an example of this in action:
Achievement Completed Pop-up
Copy
Ask AI
// Sends event to Trophyconst response = await viewFlashcard();if (!response) { return;}// Show toasts if the user has unlocked any new achievementsresponse.achievements.forEach((achievement) => { toast({ title: achievement.name, description: achievement.description, image: { src: achievement.badgeUrl, alt: achievement.name, }, });});
If you want to play sound effects, use the HTML5 Audio
API and feel
free to steal these audio
files
we recommend.
If instead you want to display all achievements you’ve set up in Trophy as part of a globally accessible UI that isn’t linked to a particular user, you can use the all achievements API.
Copy
Ask AI
curl --request GET \ --url https://api.trophy.so/v1/achievements \ --header 'X-API-KEY: <api-key>'
Both the user achievements API and the all achievements API include completion
statistics like completions (the number of users that have completed an
achievement) and rarity (the percentage of users that have completed an
achievement).