Custom triggers
Overview
Custom triggers enable you to track specific events in your application that aren’t automatically captured by the Reward SDK. This feature is ideal for monitoring:
- User interactions
- Purchase events
- Feature usage
- Custom business events
- Application-specific milestones
Getting Started
Creating a Custom Trigger
To create a custom trigger:
- Go to the Triggers page in your account settings.
- Click the Create Trigger button.
- Provide a descriptive name for your trigger.
- Review the auto-generated trigger ID and change it if necessary.
- Select Custom as the trigger type.
- Click Save to finalize.
Your new trigger will now appear in the triggers list, where its ID is available for reference.
Triggering an Event
Custom events can be triggered programmatically by sending an API request. This requires two key values:
- Trigger ID: Available in your Reward dashboard.
- User ID: The unique identifier assigned during user authentication via the plugin.
Use the API integration to send custom events to Reward. You should use the following endpoint to trigger an event for a specific user:
POST /v1/triggers/{triggerId}/users/{userId}/events
Code Examples
cURL
curl -X POST \
'https://api.rewardplugin.com/v1/triggers/successful_payment/users/user123/events' \
-H 'Authorization: Bearer sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
-H 'Content-Type: application/json' \
-d '{
"value": 19.99
}'
JavaScript
const triggerEvent = async (triggerId, userId, payload = {}) => {
const encodedUserId = encodeURIComponent(userId);
try {
const response = await fetch(
`https://api.rewardplugin.com/v1/triggers/${triggerId}/users/${encodedUserId}/events`,
{
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
},
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Failed to trigger event:", error);
throw error;
}
};
Python
import requests
from urllib.parse import quote
def trigger_event(trigger_id, user_id, payload=None):
encoded_user_id = quote(user_id)
url = f"https://api.rewardplugin.com/v1/triggers/{trigger_id}/users/{encoded_user_id}/events"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = payload if payload else {}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
Best Practices
URL Encoding
Always encode user IDs to handle special characters:
JavaScript
const encodedUserId = encodeURIComponent(userId);
Python
from urllib.parse import quote
encoded_user_id = quote(user_id)