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

# Targeting Settings

> Learn how to configure targeting in the Flutter SDK.

## Overview

Through targeting configuration, you can display customized ads to specific user groups. The Adrop SDK provides **Audience Targeting**.

* **Audience Targeting**: Displays ads based on user properties (attributes).

<Note>
  To collect targeting data, you must set properties before loading ads.
</Note>

***

## UID Setup (Optional)

Set a user identifier (UID) to share targeting data across platforms. UID is optional and is not required for property-based or event-based targeting.

### Usage

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// After user login (optional)
await Adrop.setUID('user_123');

// On user logout
await Adrop.setUID('');
```

### Parameters

| Parameter | Type     | Description                                      |
| --------- | -------- | ------------------------------------------------ |
| `uid`     | `String` | User unique identifier (e.g., service member ID) |

<Warning>
  UID is hashed with SHA-256 before transmission. Do not pass personal information (email, phone number, etc.) directly.
</Warning>

<Note>
  When logging out, pass an empty string (`''`) to reset the UID.
</Note>

***

## Audience Targeting

Collect user property information to display ads to specific user groups.

### Setting Properties

Use the `AdropMetrics.setProperty()` method to collect user property information.

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// String property
await AdropMetrics.setProperty('membership_level', 'premium');

// Numeric property
await AdropMetrics.setProperty('booking_count', 15);

// Boolean property
await AdropMetrics.setProperty('is_subscriber', true);

// Passing null (removes property)
await AdropMetrics.setProperty('membership_level', null);
```

### Parameters

| Parameter | Type      | Description                                      |
| --------- | --------- | ------------------------------------------------ |
| `key`     | `String`  | Property key (max 64 characters)                 |
| `value`   | `dynamic` | Property value (String, int, double, bool, null) |

<Warning>
  * Property keys can be up to 64 characters.
  * String values can be up to 256 characters.
  * Numeric values can be up to 9007199254740991.
  * You can set up to 256 properties.
</Warning>

### Default Properties

The Adrop SDK provides default properties for targeting.

#### Age (Birth Date)

When you pass birth date information, age is automatically calculated.

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// Year only (yyyy)
await AdropMetrics.setProperty('BIRTH', '1990');

// Year and month (yyyyMM)
await AdropMetrics.setProperty('BIRTH', '199003');

// Year, month, and day (yyyyMMdd)
await AdropMetrics.setProperty('BIRTH', '19900315');
```

**Date Formats**

| Format     | Example    | Description          |
| ---------- | ---------- | -------------------- |
| `yyyy`     | "1990"     | Year only            |
| `yyyyMM`   | "199003"   | Year and month       |
| `yyyyMMdd` | "19900315" | Year, month, and day |

#### Gender

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// Male
await AdropMetrics.setProperty('GDR', 'M');

// Female
await AdropMetrics.setProperty('GDR', 'F');

// Other
await AdropMetrics.setProperty('GDR', 'O');

// Unknown
await AdropMetrics.setProperty('GDR', 'U');
```

**Gender Values**

| Value | Description |
| ----- | ----------- |
| `M`   | Male        |
| `F`   | Female      |
| `O`   | Other       |
| `U`   | Unknown     |

<Tip>
  You can also use the `AdropProperties` and `AdropGender` enums provided by the SDK:

  ```dart theme={null}
  import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

  await AdropMetrics.setProperty(AdropProperties.birth.code, '19900315');
  await AdropMetrics.setProperty(AdropProperties.gender.code, AdropGender.male.code);
  await AdropMetrics.setProperty(AdropProperties.age.code, 35);
  ```
</Tip>

#### Setting Age Directly

You can also set age directly instead of birth date.

```dart theme={null}
await AdropMetrics.setProperty('AGE', 30);
```

<Note>
  You only need to set either `BIRTH` or `AGE`. If both are set, `BIRTH` takes precedence.
</Note>

### Custom Properties

You can set custom properties that fit your service. Custom properties must be defined first in the **Targeting** menu of [Ad Control Console](https://console.adrop.io).

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// Local activity app example
await AdropMetrics.setProperty('region', 'Seoul');
await AdropMetrics.setProperty('booking_count', 5);

// Shopping mall app example
await AdropMetrics.setProperty('membership_tier', 'gold');
await AdropMetrics.setProperty('total_purchase_amount', 1500000);

// Media app example
await AdropMetrics.setProperty('favorite_genre', 'drama');
await AdropMetrics.setProperty('is_premium_subscriber', true);
```

<Warning>
  Custom property names must exactly match the names defined in the console. They are case-sensitive.
</Warning>

***

## Event Tracking

You can send events to track user behavior.

### Sending Events

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

// Event name only
await AdropMetrics.sendEvent('click');

// With parameters
await AdropMetrics.sendEvent('purchase', {
  'item_id': 'SKU_001',
  'price': 29900,
  'currency': 'KRW',
});
```

### Parameters

| Parameter | Type                    | Description                                                             |
| --------- | ----------------------- | ----------------------------------------------------------------------- |
| `name`    | `String`                | Event name                                                              |
| `params`  | `Map<String, dynamic>?` | Event parameters (optional). Supports String, int, double, bool values. |

<Note>
  You can set up to 20 parameters per event.
</Note>

<Warning>
  `logEvent()` is deprecated since version 1.8.0. Use `sendEvent()` instead.
</Warning>

***

## Retrieving Properties

You can retrieve all stored properties.

```dart theme={null}
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

final properties = await AdropMetrics.properties();
debugPrint('Stored properties: $properties');
```

***

## Usage Examples

### Setting Properties on Login

```dart theme={null}
import 'package:flutter/material.dart';
import 'package:adrop_ads_flutter/adrop_ads_flutter.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  Future<void> _onLoginSuccess(User user) async {
    // Set default properties
    if (user.birthDate != null) {
      await AdropMetrics.setProperty('BIRTH', user.birthDate);
    }
    if (user.gender != null) {
      await AdropMetrics.setProperty('GDR', user.gender);
    }

    // Set custom properties
    await AdropMetrics.setProperty('membership_level', user.membershipLevel);
    await AdropMetrics.setProperty('total_booking_count', user.bookingCount);

    // Send sign-up event
    await AdropMetrics.sendEvent('sign_up', {
      'method': 'email',
    });

    // Navigate to main screen
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(builder: (_) => const MainScreen()),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // ...
    );
  }
}
```

### Resetting on Logout

```dart theme={null}
Future<void> _onLogout() async {
  // Reset properties if needed
  await AdropMetrics.setProperty('membership_level', null);
  await AdropMetrics.setProperty('total_booking_count', null);
}
```

### Updating Properties

```dart theme={null}
class UserProfile extends StatelessWidget {
  void _onPurchaseComplete(int purchaseAmount) async {
    // Update property on purchase completion
    final currentTotal = await _getCurrentTotalPurchase();
    await AdropMetrics.setProperty(
      'total_purchase_amount',
      currentTotal + purchaseAmount,
    );

    // Send purchase event
    await AdropMetrics.sendEvent('purchase', {
      'amount': purchaseAmount,
      'currency': 'KRW',
    });
  }

  void _onMembershipUpgrade(String newTier) async {
    // Update property on membership upgrade
    await AdropMetrics.setProperty('membership_tier', newTier);

    // Send upgrade event
    await AdropMetrics.sendEvent('membership_upgrade', {
      'new_tier': newTier,
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
```

***

## Best Practices

### 1. Set Properties Before Loading Ads

```dart theme={null}
// ✅ Correct example
await AdropMetrics.setProperty('membership_level', 'premium');
// Load ad...
bannerView.load();

// ❌ Incorrect example
bannerView.load();
await AdropMetrics.setProperty('membership_level', 'premium'); // Setting after ad load won't apply targeting
```

### 2. Update Properties When Changed

```dart theme={null}
class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // Set initial properties on app start
    _updateUserProperties();
  }

  Future<void> _updateUserProperties() async {
    final user = await getUserInfo();
    await AdropMetrics.setProperty('membership_level', user.membershipLevel);
  }
}
```

### 3. Reset Properties on Logout

```dart theme={null}
Future<void> onUserLogout() async {
  // Reset properties if needed
  await AdropMetrics.setProperty('membership_level', null);
  await AdropMetrics.setProperty('total_booking_count', null);
}
```

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Create Audience Targeting" icon="users" href="/targeting/audience">
    Create audience targeting in the console
  </Card>

  <Card title="Sell Targeting" icon="won-sign" href="/targeting/sell">
    Configure targeting category sales
  </Card>

  <Card title="Banner Ads" icon="rectangle-ad" href="/sdk/flutter/banner">
    Implement banner ads
  </Card>

  <Card title="Native Ads" icon="mobile" href="/sdk/flutter/native">
    Implement native ads
  </Card>
</CardGroup>

***

## FAQ

<AccordionGroup>
  <Accordion title="Will targeted ads not be displayed if I don't set UID?">
    UID is optional. Property-based and event-based targeting work normally without setting UID. UID is used for sharing targeting data across platforms.
  </Accordion>

  <Accordion title="I set properties but don't see data in the console.">
    Property data is reflected in the console within 24 hours after collection. If data is not showing:

    1. Check that the property is correctly defined in the console.
    2. Verify that the property key passed in the SDK exactly matches the console (case-sensitive).
    3. Confirm that `AdropMetrics.setProperty()` is called before ad loading.
  </Accordion>

  <Accordion title="How do I delete property values?">
    Pass `null` to the property value to delete that property:

    ```dart theme={null}
    await AdropMetrics.setProperty('membership_level', null);
    ```
  </Accordion>

  <Accordion title="Are there type restrictions for property values?">
    Supported types are `String`, `int`, `double`, and `bool`. Arrays and complex objects are not supported. Convert to string if needed.
  </Accordion>
</AccordionGroup>
