Skip to main content
The guide outlines the full process of adding an XP feature to your web or mobile app using Trophy. For illustration purposes we’ll use the example of a study platform that uses XP to reward users for taking different interactions.
To see a fully working example of this in practice, check out the live demo or github repo.

Pre-requisites

  • A Trophy account
  • About 10 minutes

Trophy Setup

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 any number of metrics that best represents the interactions you want to reward XP from. In the Trophy dashboard, head to the metrics page and create a metric.
Once you’ve created your metric, head to the points page and create a new points system called ‘XP’.
Once created, you’ll be taken to the configure page for the XP system where you can create ‘triggers’ for each of the ways you want to reward users with XP.
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 configured as part of any XP triggers. If so Trophy will award the user with the appropriate amount of XP according to the trigger configuration. This is what makes building gamified experiences with Trophy so easy, it does all the work for you behind the scenes.
XP can also be awarded to users based on achievements, streaks and other triggers. See the dedicated points docs for more information.

Installing Trophy SDK

To interact with Trophy from your code you’ll use the Trophy SDK available in most major programming languages. Install the Trophy SDK:
npm install @trophyso/node
Next, grab your API key from the Trophy integration page and add this as a server-side only environment variable.
TROPHY_API_KEY='*******'
Make sure you don’t expose your API key in client-side code.

Tracking User Interactions

To track an event (user interaction) against your metric, use the metric change API.
curl -X POST https://app.trophy.so/api/metrics/flashcards-flipped/event \
     -H "X-API-KEY: <apiKey>" \
     -H "Content-Type: application/json" \
     -d '{
  "user": {
    "id": "18",
    "email": "user@example.com",
    "tz": "Europe/London"
  },
  "value": 750
}'
The response to this API call is the complete set of changes to any features you’ve built with Trophy, including any XP that was awarded to the user as a result of the event, and from what triggers it was awarded.
Response
{
  "metricId": "d01dcbcb-d51e-4c12-b054-dc811dcdc623",
  "eventId": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
  "total": 900,
  ...,
  "points": {
    "xp": {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "name": "XP",
      "description": null,
      "badgeUrl": null,
      "total": 450,
      "added": 50,
      "awards": [
        {
          "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
          "awarded": 50,
          "date": "2021-01-01T00:00:00Z",
          "total": 450,
          "trigger": {
            "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
            "type": "metric",
            "metricName": "Flashcards Flipped",
            "metricThreshold": 100,
            "points": 50
          }
        }
      ]
    }
  },
  ...
}
Validate this is working by checking the Trophy dashboard.

Displaying XP

You have a number of options for displaying XP in your application. Here we’ll look at the most common options.

Pop-up Notifications

We can use the response of the metric change API to show users pop-up notifications when users are awarded new XP. Here’s an example of this in action:
XP Pop-ups
// Sends event to Trophy
const response = await viewFlashcard();

if (!response) {
  return;
}

const xp = response.points.xp;

// Show toast if user was awarded XP
if (xp.awards.length > 0) {
  const trigger = xp.awards[0].trigger;

  toast({
    title: `You gained ${xp.added} XP`,

    // e.g. "+20 XP for 10 flashcards flipped"
    description: `+${trigger.points} XP for ${
      trigger.metricThreshold
    } ${trigger.metricName.toLowercase()}`,
  });
}
If you want to play sound effects, use the HTML5 Audio API and feel free to steal these audio files we recommend.

Displaying User XP

To fetch a users XP use the user points API.
curl --request GET \
  --url https://api.trophy.so/v1/users/{id}/points/{key} \
  --header 'X-API-KEY: <api-key>'
This API returns data on the user’s total XP but can be configured to also return between 1 and 100 of the user’s most recent XP awards by using the awards query parameter.
Response
{
  "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
  "name": "XP",
  "description": null,
  "badgeUrl": null,
  "total": 100,
  "awards": [
    {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "awarded": 10,
      "date": "2021-01-01T00:00:00Z",
      "total": 100,
      "trigger": {
        "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
        "type": "metric",
        "points": 10,
        "metricName": "Flashcards Flipped",
        "metricThreshold": 1000
      }
    }
  ]
}
Here’s an example of a UI that shows users a list of their most recent awards based on the data returned from the user points API.

User XP Chart

The user points summary API returns chart-ready historical data for displaying how a user’s XP has changed over time.
curl --request GET \
  --url https://api.trophy.so/v1/users/{id}/points/{key}/event-summary \
  --header 'X-API-KEY: <api-key>'
Use the aggregation, start_date and end_date query parameters to control the data returned. Here’s an example of XP data aggregated daily:
Response
[
  {
    "date": "2024-01-01",
    "total": 100,
    "change": 100
  },
  {
    "date": "2024-01-02",
    "total": 300,
    "change": 200
  },
  {
    "date": "2024-01-03",
    "total": 600,
    "change": 300
  }
]
And here’s an example of the types of charts you can build with this data:

Analytics

In Trophy your xp system page, includes analytics charts that shows data on total XP earned and a breakdown of exactly what triggers award the most XP.

Get Support

Want to get in touch with the Trophy team? Reach out to us via email. We’re here to help!
I