Trackers¶
Trackers track the value(s) of something. Anything, really. A tracker does this every hour or every day. Once a tracker tracks an event, it sends it to our databases to be saved for up to a month.
Trackers are used to easily show statistics without having to manage the data yourself.
Example¶
const kontrl = require("kontrl");
// This tracker counts the rows of the users table (see tables documentation)
const myUserTracker = kontrl.tracker.countResources({
resourceNames: ["users"],
precision: "hour" // Can be once an hour or once a day.
});
There are a few premade trackers. It’s also possible to make your own trackers.
Premade trackers¶
- events Uses custom events to make a graph (see Events documentation).
- countResource Tracks how many items there are in a table
- custom trackers See the “Custom trackers” section.
Events tracker¶
When you throw events, (see Events page), you can make a tracker to display this data on the dashboard.
Params:
- eventName the name of the events. (see Events page).
const kontrl = require("kontrl");
const routeVisitsTracker = kontrl.tracker.events({
eventName: "route_visits"
});
Count Resources tracking¶
A simple premade tracker the items of 1 or more tables. (see Tables page).
Params:
- resourceNames an array of 1 or more table names.
- precision how often you want to track. can be “hour” or “day”.
const kontrl = require("kontrl");
const userCountTracker = kontrl.tracker.countResources({
resourceNames: ["users"],
precision: "hour"
})
Custom trackers¶
It’s incredibly easy to write your own trackers.
First, decide whether you want to track a single number or multiple numbers at the same time.
Tracking a single value¶
You track a single value by using the kontrl.tracker.single(..) function.
Params:
- uniqueName the name of what are tracking. Unique within your application and maximum 20 characters long.
- precision the interval at which the value will get tracked. Can be “hour” or “day” meaning hourly and daily respectively.
- getValue the function which will be called every hour or day that gets the value. This function expects a single number as a return
const kontr = require("kontrl");
kontrl.tracker.single({
uniqueName: "new_users_today",
precision: "day",
getValue() {
const newUserCount = 100; // get the data from your database
return newUserCount;
}
})
Tracking multiple values¶
You track a combination of values using the kontrl.tracker.many(..) function.
Params:
- uniqueName the name of what are tracking. Unique within your application and maximum 20 characters long.
- precision the interval at which the value will get tracked. Can be “hour” or “day” meaning hourly and daily respectively.
- getValue the function that gets the value. This function returns an object.
const kontr = require("kontrl");
kontrl.tracker.many({
uniqueName: "stock",
precision: "day",
getValues() {
// Get the current stock items
const itemsInStock = { "gameboy": 3, "Pokemon fire red": 9 };
return itemsInStock;
}
})