Skip to content

Track

The Track endpoint allows you to track events or actions performed by your users.

Let’s see examples of how to implement Payment Failed and Purchase Complete events.

Payment Failed

To improve your conversion rate on this step you need to understand the most common reasons why the payment fails and take action to avoid them.

You can track all of these failures by using the Payment Failed event. There is no need to interrupt your current flow; simply send the data to our API endpoint and continue with your process.

async function processPayment(paymentEngine, purchaseData, userData) {
// You should add some logic to validate purchaseData and userData
// before continuing with the payment process.
try {
const paymentEngineResponse = await paymentEngine.processPayment(purchaseData);
if (paymentEngineResponse.status === "failed") {
const payload = buildPaymentFailedPayload(
paymentEngineResponse,
purchaseData,
userData
);
// `Reserhub.track` is an asynchronous function you need to implement
// to send tracking data to our API endpoint.
// Ensure it handles errors gracefully.
Reserhub.track(payload).catch(error => {
console.error("Error tracking payment failed event", error);
});
// And then continue handling the payment failure as you actually do.
}
// If the payment is successful you can continue with your actual flow.
} catch (error) {
// Here you handle the errors that you get from the payment engine.
console.error("Error processing payment", error);
}
};
function buildPaymentFailedPayload(
paymentEngineResponse,
purchaseData,
userData
) {
// Suppose that when the payment fails you can get the reason
// from a field called `reason`
return {
event_name: "payment_failed",
event_data: {
distinct_id: userData.id,
email: userData.email,
error_message: paymentEngineResponse.reason,
passenger_count: purchaseData.passengers.length,
payment_engine: purchaseData.payment_engine,
payment_type: purchaseData.payment_type,
phone: userData.phone,
product: "web",
tickets: purchaseData.tickets.length,
total: purchaseData.total_amount,
// `tripsList` is a helper function that you made
// to format the trips as needed
trips: tripsList(purchaseData.trips),
trips_count: purchaseData.trips_count
}
};
};

Purchase Complete

The Purchase Complete event is the most relevant to track, as it provides insights into various sales metrics.

Similar to the example above, you can track this event without interrupting your actual flow.

async function completePurchase(purchaseData, userData) {
// You should add some logic to validate purchaseData and userData
// before continuing with the ticket dispatch process.
try {
const payload = buildPurchaseCompletePayload(purchaseData, userData);
// `Reserhub.track` is an asynchronous function you need to implement
// to send tracking data to our API endpoint.
// Ensure it handles errors gracefully.
Reserhub.track(payload).catch(error => {
console.error("Error tracking purchase complete event", error);
});
// Call to your function to generate tickets for the customer
// or the final step of your purchase process.
generateTickets(purchaseData, userData);
} catch (error) {
// Handle any error of your final step in the purchase flow.
console.error("Error completing purchase", error);
}
}
function buildPurchaseCompletePayload(purchaseData, userData) {
return {
event_name: "purchase_complete",
event_data: {
distinct_id: userData.id,
email: userData.email,
insurance: purchaseData.has_insurance,
operation_id: purchaseData.id,
passenger_count: purchaseData.passengers.length,
payment_type: purchaseData.payment_type,
phone: userData.phone,
product: "web",
tickets: purchaseData.tickets.length,
total: purchaseData.total_amount,
// `tripsList` is a helper function that you made
// to format the trips as needed.
trips: tripsList(purchaseData.trips),
trips_count: purchaseData.trips_count
}
};
};

Conclusion

As demonstrated, implementing calls to our API endpoint can be straightforward.

These examples assume you have developed a library to manage requests to our API, adhering to the best practices outlined in other sections.

By incorporating helper functions, you can ensure data is correctly formatted, simplifying implementation while maintaining clean and readable code.

Moreover, adhering to these standards allows for easier troubleshooting and guidance when issues arise with data submission to our endpoint, enabling more efficient support and error correction.