Track Events
Events represent how users interact with your application.
Send Basic Events
Track a simple event with just a name:
B2MetricAnalytics.shared.logEvent(name: "button_clicked")
Send Events with Properties
Events can contain properties that give more context about the event:
let properties: [String: AnyCodable] = [
"item_id": AnyCodable("12345"),
"item_price": AnyCodable(299.99),
"currency": AnyCodable("USD")
]
B2MetricAnalytics.shared.logEvent(name: "add_to_cart", properties: properties)
Supported Property Types
B2Metric supports various data types for event properties via AnyCodable:
| Type | Swift Type | Example |
|---|---|---|
| String | String | "value" |
| Number | Int, Double | 42, 3.14 |
| Boolean | Bool | true, false |
| Date | Date | Date() |
B2MetricAnalytics.shared.logEvent(
name: "purchase_completed",
properties: [
"order_id": AnyCodable("ORD-123"),
"total_amount": AnyCodable(499.90),
"item_count": AnyCodable(3),
"is_gift": AnyCodable(false),
"timestamp": AnyCodable(Date())
]
)
Common Event Examples
E-commerce Events
// Product view
B2MetricAnalytics.shared.logEvent(
name: "product_viewed",
properties: [
"product_id": AnyCodable("SKU-123"),
"product_name": AnyCodable("Running Shoes"),
"category": AnyCodable("Footwear"),
"price": AnyCodable(299.90)
]
)
// Add to cart
B2MetricAnalytics.shared.logEvent(
name: "add_to_cart",
properties: [
"product_id": AnyCodable("SKU-123"),
"quantity": AnyCodable(1),
"price": AnyCodable(299.90)
]
)
// Purchase
B2MetricAnalytics.shared.logEvent(
name: "purchase_completed",
properties: [
"order_id": AnyCodable("ORD-789"),
"total_amount": AnyCodable(299.90),
"payment_method": AnyCodable("credit_card")
]
)
User Engagement Events
// Button click
B2MetricAnalytics.shared.logEvent(
name: "button_clicked",
properties: [
"button_text": AnyCodable("Sign Up"),
"button_location": AnyCodable("header"),
"screen": AnyCodable("home")
]
)
// Form submission
B2MetricAnalytics.shared.logEvent(
name: "form_submitted",
properties: [
"form_name": AnyCodable("contact_form"),
"field_count": AnyCodable(5),
"success": AnyCodable(true)
]
)
Full Event Payload Examples
Every event sent to B2Metric includes user_properties automatically.
product_viewed
{
"api_key": "sdk_xxxxxxxxxxxxxxxx",
"body": {
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"type": "product_viewed",
"timestamp": "2026-02-18T07:45:02.552Z",
"properties": {
"product_id": "SKU-123",
"product_name": "Running Shoes",
"category": "Footwear",
"price": 299.90
},
"user_properties": {
"app_name": "B2MetricAnalytics",
"app_bundle_id": "com.b2metric.analytics",
"app_version": "1.0.0",
"country_code": "TR",
"device_id": "a3f5b8c1-2d4e-4f6a-8b9c-0d1e2f3a4b5c",
"device_model": "iPhone 15 Pro",
"device_token": "abc123def456...apns_token_here",
"language": "tr_TR",
"os_version": "17.2",
"platform": "iOS",
"screen_resolution": "1179x2556",
"timezone": "Europe/Istanbul"
}
}
}
add_to_cart
{
"api_key": "sdk_xxxxxxxxxxxxxxxx",
"body": {
"id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
"type": "add_to_cart",
"timestamp": "2026-02-18T07:46:15.123Z",
"properties": {
"product_id": "SKU-123",
"quantity": 1,
"price": 299.90,
"currency": "TRY"
},
"user_properties": { "...": "same as above" }
}
}
purchase_completed
{
"api_key": "sdk_xxxxxxxxxxxxxxxx",
"body": {
"id": "c3d4e5f6-a7b8-9012-cdef-123456789012",
"type": "purchase_completed",
"timestamp": "2026-02-18T07:50:00.000Z",
"properties": {
"order_id": "ORD-789",
"total_amount": 599.80,
"currency": "TRY",
"item_count": 2,
"payment_method": "credit_card",
"is_gift": false
},
"user_properties": { "...": "same as above" }
}
}