Record Feature Usage
When somebody uses a feature of your product, you’re going to need to record this usage so that Kana can understand what and how much of a feature that a user has used.
This guide assumes that you have setup the client as we have in the Setup instructions, and have initialized this to the client
variable.
Grab details on the feature usage
You will need to ensure you have details on both:
-
The user who has used the feature
-
The feature which the user has used
Namely, you will need to have an identifier which maps to the id
in Kana for both Objects.
If neither the user nor feature exist yet in Kana, then you should create these first:
We will work on the assumption that you have pulled the following user and have all the following details to hand:
Send the request to Kana
Now we can make the request to Kana in order to record a user’s usage of a particular feature.
We do this through the createusageevent mutation. This requires three arguments:
-
userId
- theid
of the User -
featureId
- theid
if the Feature -
delta
- the amount of the feature that’s been used as an Integer.
What format should the id arguments both be in?
We expect these id
arguments to be String’s - as this matches the id
scalar type on the User object and Feature object respectively. If any other field type is used, an error will occur telling you we can’t accept any field type except String.
We’ll define these as variables to send alongside the query:
Next, we have to define the query string for the mutation operation. This returns a boolean informing if the usage event was or not so no object fields are specified to be returned.
We can now send the request to Kana in order to record the user’s usage of the feature.
Make sure that this request is sent to Kana only upon the feature being successfully used, when no more errors could occur preventing usage. This will prevent discrepancies whereby feature usage has been recorded to Kana despite the feature not actually having been used (ie. due to errors, etc).
Congratulations 🎉 You’ve now successfully recorded a feature’s usage.
We will return true
if the usage event was successful. Feel free to use the response you get back to log the call, or raise any errors/retry the call if the request is not successful or returns false
in the response.
Next Steps
All of these usage events are recorded against a users Entitlement and Consumption of a feature. This means that as they use a feature, we will reduce the amount they are now entitled to. Therefore, they may hit limits and you will need to restrict access to features when that happens. You can achieve this by identifying their current feature entitlement and imposing blocks where necessary: