> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flashduty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced Configuration

> Configure advanced features of iOS RUM SDK, including custom events, user tracking, sampling control, and data security

<Note>
  **About Dependencies and Package Names**

  Flashduty iOS SDK is fully compatible with the Datadog open-source protocol. When adding dependencies via Swift Package Manager or CocoaPods, use names like `FlashcatCore` and `FlashcatRUM`, but in code, import modules like `DatadogCore` and `DatadogRUM`. You can seamlessly leverage Datadog ecosystem documentation, examples, and best practices while enjoying Flashduty platform services.
</Note>

Flashduty iOS RUM SDK provides rich advanced configuration options to help you customize data collection and context information based on business needs.

<Info>
  **Supported Configuration Scenarios:**

  * Enrich user sessions - Add custom views, actions, resources, and error information
  * Protect sensitive data - Mask personally identifiable information and sensitive data
  * Associate user sessions - Link user sessions with internal user identifiers
  * Control data volume - Optimize data collection through sampling and event filtering
  * Enhance context - Add custom attributes to data
</Info>

## Enrich User Sessions

### Custom Views

In addition to auto-tracked views, you can manually track custom views:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogRUM

    // Start tracking view
    RUMMonitor.shared().startView(
        key: "view-key",
        name: "View Name",
        attributes: [:]
    )

    // Stop tracking view
    RUMMonitor.shared().stopView(
        key: "view-key",
        attributes: [:]
    )
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogRUM;

    // Start tracking view
    [[RUMMonitor shared] startViewWithKey:@"view-key"
                                     name:@"View Name"
                               attributes:@{}];

    // Stop tracking view
    [[RUMMonitor shared] stopViewWithKey:@"view-key"
                              attributes:@{}];
    ```
  </Tab>
</Tabs>

<Note>
  **Parameter Description:**

  * `key` (String) - Unique identifier for the view, same key used for `startView` and `stopView`
  * `name` (String) - Name of the view
  * `attributes` (Dictionary) - Attributes attached to the view (optional)
</Note>

### Custom Actions

Track specific custom user actions:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogRUM

    RUMMonitor.shared().addAction(
        type: .tap,
        name: "Button Tapped",
        attributes: ["button_id": "submit"]
    )
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogRUM;

    [[RUMMonitor shared] addActionWithType:RUMActionTypeTap
                                      name:@"Button Tapped"
                                attributes:@{@"button_id": @"submit"}];
    ```
  </Tab>
</Tabs>

### Custom Resources

Manually track specific custom resources:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogRUM

    // Start resource tracking
    RUMMonitor.shared().startResource(
        resourceKey: "resource-key",
        httpMethod: .get,
        urlString: "https://api.example.com/data",
        attributes: [:]
    )

    // Stop resource tracking (success)
    RUMMonitor.shared().stopResource(
        resourceKey: "resource-key",
        statusCode: 200,
        kind: .native,
        size: 1024,
        attributes: [:]
    )

    // Stop resource tracking (error)
    RUMMonitor.shared().stopResourceWithError(
        resourceKey: "resource-key",
        message: "Network error",
        type: "NetworkError",
        response: nil,
        attributes: [:]
    )
    ```
  </Tab>
</Tabs>

### Custom Errors

Record specific errors:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogRUM

    RUMMonitor.shared().addError(
        message: "Network request failed",
        type: "NetworkError",
        source: .network,
        attributes: [
            "url": "https://api.example.com",
            "status_code": 500
        ]
    )
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogRUM;

    [[RUMMonitor shared] addErrorWithMessage:@"Network request failed"
                                        type:@"NetworkError"
                                      source:RUMErrorSourceNetwork
                                  attributes:@{
                                      @"url": @"https://api.example.com",
                                      @"status_code": @500
                                  }];
    ```
  </Tab>
</Tabs>

### Set User Information

Set user information for the current session:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogCore

    Datadog.setUserInfo(
        id: "user-123",
        name: "John Doe",
        email: "john.doe@example.com"
    )
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogCore;

    [DDDatadog setUserInfoWithId:@"user-123"
                            name:@"John Doe"
                           email:@"john.doe@example.com"
                       extraInfo:nil];
    ```
  </Tab>
</Tabs>

<Warning>
  Only standard user fields are supported: `id`, `name`, `email`, and `anonymous_id`. Other user attributes are not supported. If needed, configure them under `context`.
</Warning>

## Event and Data Management

### Clear All Data

Clear all unsent data currently stored in the SDK:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogCore

    Datadog.clearAllData()
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogCore;

    [DDDatadog clearAllData];
    ```
  </Tab>
</Tabs>

## Sampling

Control the percentage of sessions that collect RUM data:

```swift theme={null}
RUM.enable(
    with: RUM.Configuration(
        applicationID: "<RUM_APPLICATION_ID>",
        sessionSampleRate: 80  // Collect 80% of sessions
    )
)
```

<Warning>
  Sampled-out sessions will not collect any page views or related telemetry data.
</Warning>

## User Tracking Consent

To comply with privacy regulations like GDPR and CCPA, you can set user tracking consent state:

```swift theme={null}
// Set consent state during initialization
Datadog.initialize(
    with: configuration,
    trackingConsent: .pending
)

// Update consent state when user grants permission
Datadog.set(trackingConsent: .granted)

// Update consent state when user revokes permission
Datadog.set(trackingConsent: .notGranted)
```

| State         | Behavior                                        |
| ------------- | ----------------------------------------------- |
| `.granted`    | Start collecting data and send to Flashduty     |
| `.notGranted` | Do not collect any data                         |
| `.pending`    | Collect but do not send, wait for user decision |

## Global Context

Add global context attributes that will be attached to all RUM events:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    import DatadogRUM

    // Add attribute
    RUMMonitor.shared().addAttribute(forKey: "user_type", value: "premium")

    // Remove attribute
    RUMMonitor.shared().removeAttribute(forKey: "user_type")
    ```
  </Tab>

  <Tab title="Objective-C">
    ```objectivec theme={null}
    @import DatadogRUM;

    // Add attribute
    [[RUMMonitor shared] addAttributeForKey:@"user_type" value:@"premium"];

    // Remove attribute
    [[RUMMonitor shared] removeAttributeForKey:@"user_type"];
    ```
  </Tab>
</Tabs>

## Event Mappers

Use Event Mappers to modify or filter events before they are sent:

```swift theme={null}
RUM.enable(
    with: RUM.Configuration(
        applicationID: "<RUM_APPLICATION_ID>",
        viewEventMapper: { viewEvent in
            // Modify view event
            var modifiedEvent = viewEvent
            modifiedEvent.view.url = viewEvent.view.url?.replacingOccurrences(of: "sensitive", with: "redacted")
            return modifiedEvent
        },
        actionEventMapper: { actionEvent in
            // Filter specific actions
            if actionEvent.action.target?.name?.contains("password") == true {
                return nil  // Discard this event
            }
            return actionEvent
        },
        errorEventMapper: { errorEvent in
            // Modify error event
            return errorEvent
        },
        resourceEventMapper: { resourceEvent in
            // Modify resource event
            return resourceEvent
        }
    )
)
```

<Warning>
  View events cannot be discarded (returning nil has no effect).
</Warning>

## Related Documentation

<CardGroup cols={3}>
  <Card title="SDK Integration" icon="plug" href="/en/rum/sdk/ios/sdk-integration">
    Learn how to quickly integrate the RUM SDK
  </Card>

  <Card title="Data Collection" icon="database" href="/en/rum/sdk/ios/data-collection">
    Learn about data types and attributes collected by the SDK
  </Card>

  <Card title="Compatibility" icon="check" href="/en/rum/sdk/ios/compatible">
    Learn about SDK compatibility requirements
  </Card>
</CardGroup>
