track()
The track() method records a custom event with a name and optional properties. Use it to capture user actions, conversions, and business events that matter to your analytics and marketing vendors.
Syntax
_df.track(eventName, properties?)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
eventName | string | Yes | The name of the event (e.g., 'Order Completed') |
properties | Record<string, unknown> | No | Key-value pairs describing the event |
Returns
void
Basic usage
_df.track('Button Clicked', { label: 'Sign Up' });E-commerce example
_df.track('Order Completed', {
orderId: '12345',
revenue: 99.99,
currency: 'USD',
products: [
{ id: 'SKU-001', name: 'Running Shoes', price: 79.99, quantity: 1 },
{ id: 'SKU-042', name: 'Sport Socks', price: 9.99, quantity: 2 },
],
coupon: 'SUMMER20',
tax: 8.00,
shipping: 5.99,
});Standard e-commerce events
Datafly recognises the following standard event names and maps them automatically to vendor-specific events during server-side delivery. You can use any event name you like, but using these standard names enables automatic mapping without additional pipeline configuration.
| Event name | Description | Key properties |
|---|---|---|
Product Viewed | User viewed a product detail page | productId, name, price, category |
Product List Viewed | User viewed a product listing or category page | listId, category, products[] |
Product Added | User added a product to cart | productId, name, price, quantity |
Product Removed | User removed a product from cart | productId, name, price, quantity |
Cart Viewed | User viewed their shopping cart | cartId, products[], cartTotal |
Checkout Started | User started the checkout flow | orderId, revenue, products[] |
Payment Info Entered | User entered payment information | orderId, paymentMethod |
Order Completed | User completed a purchase | orderId, revenue, currency, products[], tax, shipping |
Order Refunded | An order was refunded | orderId, revenue |
Vendor mapping
When you use standard event names, Datafly automatically maps them to vendor-specific events:
| Datafly event | GA4 event | Meta event | TikTok event |
|---|---|---|---|
Product Viewed | view_item | ViewContent | ViewContent |
Product Added | add_to_cart | AddToCart | AddToCart |
Checkout Started | begin_checkout | InitiateCheckout | InitiateCheckout |
Payment Info Entered | add_payment_info | AddPaymentInfo | AddPaymentInfo |
Order Completed | purchase | Purchase | CompletePayment |
Lead generation events
| Event name | Description | Key properties |
|---|---|---|
Form Submitted | User submitted a form | formId, formName, formFields |
Lead Generated | A new lead was created | leadId, value, source |
Signed Up | User completed registration | method, plan |
Logged In | User logged in | method |
Custom events
You are not limited to standard event names. Track any event relevant to your business:
_df.track('Video Played', {
videoId: 'vid-789',
title: 'Product Demo',
duration: 120,
percentWatched: 50,
});
_df.track('Search Performed', {
query: 'running shoes',
resultsCount: 42,
filters: { brand: 'nike', size: '10' },
});
_df.track('Subscription Upgraded', {
previousPlan: 'starter',
newPlan: 'professional',
mrr: 99,
});Custom events are delivered to vendors as custom/generic events and can be mapped to vendor-specific events using pipeline transformation rules.
Event payload
A track() call produces an event with type: "track":
{
"type": "track",
"event": "Order Completed",
"anonymousId": "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d",
"userId": "user-123",
"properties": {
"orderId": "12345",
"revenue": 99.99,
"currency": "USD"
},
"context": {
"page": {
"url": "https://example.com/checkout/confirmation",
"path": "/checkout/confirmation",
"referrer": "https://example.com/checkout/payment",
"title": "Order Confirmation",
"search": ""
},
"screen": { "width": 1920, "height": 1080 },
"locale": "en-US",
"timezone": "America/New_York",
"userAgent": "Mozilla/5.0 ...",
"library": { "name": "@datafly/collector", "version": "0.1.0" }
},
"timestamp": "2026-02-25T14:22:00.000Z",
"messageId": "df-b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e"
}Batching
Events are not sent individually. Datafly.js batches up to 10 events per request and auto-flushes every 1 second. If 10 events accumulate before the flush interval, the batch is sent immediately.
Events are also flushed when the user navigates away from the page (via visibilitychange and pagehide listeners), using navigator.sendBeacon to ensure delivery survives page unload.
To force an immediate flush:
_df.track('Order Completed', { orderId: '12345', revenue: 99.99 });
_df.flush();Property naming conventions
Use consistent property names across your events for easier reporting and vendor mapping:
- Use
camelCasefor property names (orderId, notorder_idorOrderId) - Use standard property names when they exist (see the e-commerce table above)
- Include
currencyas an ISO 4217 code whenever you send monetary values - Include
revenueorvaluefor conversion events to enable revenue attribution
Do not include personally identifiable information (PII) such as email addresses, phone numbers, or names in track() properties. Use the identify() method to associate PII with a user — Datafly hashes PII server-side before delivering it to vendors.