Event Tracking
Purchase Event Tracking
Loomit SDK supports tracking purchase events. This feature allows you to send purchase information to the Loomit platform for analytics and segmentation purposes.
Purchase Event Structure
A purchase event consists of the following fields:
Field | Type | Required | Description |
---|---|---|---|
amount | number | Yes | The purchase amount |
currency | string | Yes | The currency code in ISO 4217 format (e.g., "USD", "EUR") |
sku | string | No | Product identifier, important for segmentation and tracking |
name | string | No | Product name, used for tracking purposes |
Best Practices
- Always include the
amount
andcurrency
fields as they are required - Use a consistent format for your
sku
values to ensure proper segmentation - Track all successful purchases to get comprehensive analytics
- Use the same currency code format across your application
Implementation
To track purchase events in React Native, use the useEvents
hook from the XMediator SDK.
Basic Usage
import React from 'react';
import { useEvents } from 'react-native-xmediator';
export default function MyComponent() {
const { trackPurchase } = useEvents();
const handlePurchase = () => {
trackPurchase({
amount: 9.99,
currency: 'USD',
sku: 'premium_upgrade',
name: 'Premium Upgrade'
});
};
return (
// Your component JSX
);
}
TypeScript Interface
The PurchaseEvent
interface provides type safety for purchase tracking:
interface PurchaseEvent {
/**
* The purchase amount (mandatory)
*/
amount: number;
/**
* The currency code - ISO 4217 currency code (mandatory)
*/
currency: string;
/**
* The SKU/product identifier - Used for tracking and segmentation (optional)
*/
sku?: string;
/**
* The product name - Used for tracking only (optional)
*/
name?: string;
}
Error Handling
The trackPurchase
method includes built-in validation and will throw errors for invalid parameters:
import React from 'react';
import { useEvents } from 'react-native-xmediator';
export default function PaymentComponent() {
const { trackPurchase } = useEvents();
const handlePurchaseWithErrorHandling = (purchaseData) => {
try {
trackPurchase({
amount: purchaseData.amount,
currency: purchaseData.currency,
sku: purchaseData.sku,
name: purchaseData.name
});
console.log('Purchase tracked successfully');
} catch (error) {
console.error('Purchase tracking failed:', error.message);
// Handle the error appropriately
}
};
return (
// Your component JSX
);
}
Common validation errors:
Amount is required and must be a positive number for purchase tracking
Currency is required and must be a non-empty string for purchase tracking