Part 3: Metered access
This tutorial will walk you through adding metered access to your Features via Surfaces.
As noted previously, Feature properties can have different types, one of which is Meterable.
Prerequisites
Before you begin, ensure you have the following:
- Every step from Part 2 completed
Update the Registered Plan
To enable metered access on the Registered Plan, we will update the configured "Widget" Feature's "Enabled" property to be metered.
- Navigate to the Plans page in the MonetizationOS console
- Click on the "Registered" Plan
- Click on "Configure Features"
- Switch the "Enabled" property to "Metered"
- Set the limit to
3in a lifetime - Click "Update Plan"
Rerun the registered Surface decision
Rerun the same registered Surface decision request as Part 2 to see the updated response with your new Surface workflow properties.
You should receive a response with changes similar to the following:
{
...
"enabled": {
"counterId": "default:feat_abcd1234.enabled",
"type": "meterable",
"hasAccess": true,
"consumedUnits": 0,
"remainingUnits": 3,
"totalUnits": 3,
"uniqueResources": false,
"resourceIdUsed": false,
"periodStart": "Thu, 01 Jan 1970 00:00:00 GMT",
"isFallback": false
}
...
}Repeat the request multiple times and you may notice that consumedUnits never increases.
Meters in MonetizationOS are used to enforce limits and are backed by counters that track usage. There are two ways to increment a counter, directly via API or via workflow. Incrementing counters via workflow is the preferred method when working with Surface decisions if you want to keep all logic centralized. However this may not make sense for all use cases, as you may want to increment counters based on outside events.
Add metering to the Surface workflow
Let's now update our Surface workflow to increment the "Enabled" feature property's counter on every Surface decision.
Replace the existing code with the following, and click "Update Workflow" to save your changes:
const workflow: SurfaceWorkflow = async ({ features, identity }) => {
// On every run, use one unit of the enabled property meter
const meterAfterIncrement = await features.widget.enabled.consume();
return {
properties: {
userMessage: `Hello, ${identity.identifier}`,
isAuthenticated: identity.userType === "authenticated",
premiumWidgets: features.widget.premium,
widgetAccess: meterAfterIncrement.hasAccess,
remainingWidgets: meterAfterIncrement.remainingUnits,
},
};
};
export default workflow;This is just a basic example of how to increment a counter via a Surface workflow, but in reality you may want to implement more complex logic such as only incrementing on certain conditions based on request data.
Rerun the registered Surface decision again
Rerun the registered Surface decision request three times to exhaust the meter.
You should receive a response with changes similar to the following:
{
...
"enabled": {
"counterId": "default:feat_abcd1234.enabled",
"type": "meterable",
"hasAccess": true,
"consumedUnits": 2,
"remainingUnits": 1,
"totalUnits": 3,
"uniqueResources": false,
"resourceIdUsed": false,
"periodStart": "Thu, 01 Jan 1970 00:00:00 GMT",
"isFallback": false
}
...
"surfaceBehavior": {
"properties": {
"userMessage": "Hello, test+9@example.com",
"isAuthenticated": true,
"widgetAccess": true,
"widgetAccess": false,
"premiumWidgets": false,
"remainingWidgets": 0
}
},
...
}Once the meter is exhausted, you will see that widgetAccess is now false and remainingWidgets is 0.
You've reached the end of Part 3 of this tutorial. You have successfully created a metered Feature property and added meter handling to Surface decisions.
Proceed to Part 4 to continue the tutorial.