Skip to main content
The guide outlines the full process of adding an energy feature to your web or mobile app using Trophy. For illustration purposes we’ll use the example of a study platform that uses energy to meter the rate at which users can view flashcards.
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 grant and consume energy 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 ‘Energy’.
Once created, you’ll be taken to the configure page for the energy system where you can create points triggers for each of the ways you want to grant or consume energy.
Use ‘time’ triggers to grant users with new energy on an hourly or daily basis, and use other types of triggers with negative values to consume energy from the different user interactions you want. 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 any of the triggers set up against your energy system should be triggered, and process them accordingly. Trophy also takes care of automatically granting new energy to users over time in accordance with any ‘time’ triggers you’ve set up. This is what makes building gamified experiences with Trophy so easy, it does all the work for you behind the scenes.

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 changes in energy as a result of the event, and from what triggers energy was consumed.
Response
{
  "metricId": "d01dcbcb-d51e-4c12-b054-dc811dcdc623",
  "eventId": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
  "total": 750,
  ...,
  "points": {
    "energy": {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "name": "Energy",
      "description": null,
      "badgeUrl": null,
      "total": 9,
      "added": -1,
      "awards": [
        {
          "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
          "awarded": -1,
          "date": "2021-01-01T00:00:00Z",
          "total": 9,
          "trigger": {
            "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
            "type": "metric",
            "metricName": "Flashcards Flipped",
            "metricThreshold": 1,
            "points": -1
          }
        }
      ]
    }
  },
  ...
}
Validate this is working by checking the Trophy dashboard.

Metering Usage

To prevent users from taking actions in your product based on energy, use the user points API to fetch their current total energy.
curl --request GET \
  --url https://api.trophy.so/v1/users/{id}/points/{key} \
  --header 'X-API-KEY: <api-key>'
This returns data on the total energy the user has, allowing you to use the total property to control what actions a user can perform:
Response
{
  "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
  "name": "Energy",
  "description": null,
  "badgeUrl": null,
  "total": 10,
  "awards": [
    {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "awarded": 1,
      "date": "2021-01-01T00:00:00Z",
      "total": 10,
      "trigger": {
        "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
        "type": "time",
        "points": 1,
        "timeUnit": "day",
        "timeInterval": 1
      }
    },
    {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "awarded": -1,
      "date": "2021-01-01T00:00:00Z",
      "total": 9,
      "trigger": {
        "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
        "type": "metric",
        "points": -1,
        "metricName": "Flashcards Flipped",
        "metricThreshold": 1
      }
    }
  ]
}
Here’s an example where a user is only allowed to view a flashcard if total > 0
const energy = await trophy.users.points("user-id", "energy");

if (!energy) {
  return;
}

if (energy.total > 0) {
  showNextFlashcard();
}
You can then modify your trigger setup in Trophy and control the rate at which users can interact with your product right from the Trophy dashboard without needing to make any code changes.

Displaying Energy

To fetch a users energy 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 energy but can be configured to also return between 1 and 100 of the user’s most recent energy changes by using the awards query parameter.
Response
{
  "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
  "name": "Energy",
  "description": null,
  "badgeUrl": null,
  "total": 10,
  "awards": [
    {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "awarded": 1,
      "date": "2021-01-01T00:00:00Z",
      "total": 10,
      "trigger": {
        "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
        "type": "time",
        "points": 1,
        "timeUnit": "day",
        "timeInterval": 1
      }
    },
    {
      "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
      "awarded": -1,
      "date": "2021-01-01T00:00:00Z",
      "total": 9,
      "trigger": {
        "id": "0040fe51-6bce-4b44-b0ad-bddc4e123534",
        "type": "metric",
        "points": -1,
        "metricName": "Flashcards Flipped",
        "metricThreshold": 1
      }
    }
  ]
}
The user points summary API can also be used to drive chart-based UI, like showing users their energy usage over time.
curl --request GET \
  --url https://api.trophy.so/v1/users/{id}/points/{key}/event-summary \
  --header 'X-API-KEY: <api-key>'
Here’s an example of a UI that shows users their current energy, a chart showing their usage over time, and a list of their most recent changes in energy.

Analytics

In Trophy your energy system page, includes analytics charts that shows data on total energy awarded/consumed and a breakdown of exactly what triggers cause the most frequent changes in energy.

Get Support

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