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

# Meta Ads

> Send conversions to Facebook and Instagram with CAPI for improved attribution

The Meta Ads integration sends conversions to Facebook and Instagram using Meta's Conversions API (CAPI) for server-side tracking that captures conversions browser pixels can miss and improves attribution accuracy.

## What Gets Synced

**Ad Performance Data (From Meta):**

* Campaign, ad set, ad stats
* Spend, impressions, clicks
* CPM, CPC, CTR
* Conversions reported by Meta

**Conversion Data (To Meta):**

* Purchase events with revenue
* Lead gen events
* Signup/registration events
* Add to cart, begin checkout events
* Page view events
* Custom events you configure

## How It Works

### Conversion Flow

**1. User Clicks Meta Ad**

```
User clicks Facebook ad on mobile
Meta appends fbclid to URL
URL: https://yoursite.com/?fbclid=IwAR123...
```

**2. DATALYR Captures Click ID**

```
DATALYR tracking script captures:
- fbclid: IwAR123...
- _fbp cookie (browser ID)
- _fbc cookie (click tracking)
Stores in visitor profile: anon_abc123
```

**3. User Browses and Converts**

```
User views products, adds to cart
User completes purchase: $99.99
```

**4. Conversion Sent to Meta CAPI**

```
DATALYR sends server-side conversion:
- Event: Purchase
- Value: $99.99
- fbclid: IwAR123 (from Day 1)
- Enhanced matching: email, phone (hashed)
- Deduplication with browser pixel
```

**5. Meta Attributes Conversion**

```
Meta receives conversion
Matches fbclid to original ad click
Attributes $99.99 to Facebook ad campaign
Updates campaign performance metrics
```

**Result:** Purchase correctly attributed to Meta ad, even if it happened days later or on different device.

## Setup

### 1. Connect Meta Account

1. Navigate to Settings → Sources in DATALYR
2. Click "Connect" next to Meta Ads
3. Log in to Facebook/Meta account
4. Select Business Manager and ad account
5. Grant permissions:
   * Read ad insights
   * Manage ads
   * Access Conversions API
6. Click "Authorize"

### 2. Select Primary Ad Account

After connecting:

1. DATALYR shows all accessible ad accounts
2. Select your primary ad account for conversions
3. Select the Meta Pixel to use for CAPI events
4. Click "Save"

### 3. Configure Conversion Events

1. Go to Settings → Conversion Rules

2. Map your events to Meta standard events:
   * `purchase` → Purchase
   * `add_to_cart` → AddToCart
   * `begin_checkout` → InitiateCheckout
   * `signup` → CompleteRegistration
   * `lead` → Lead

3. Set conversion values (optional):
   * Use order total for purchases
   * Fixed value for leads
   * LTV for signups

### 4. Verify Integration

**Test Conversion:**

1. Click a Meta ad (use test ads if available)
2. Complete a conversion on your site
3. Go to Meta Events Manager
4. Check "Test Events" tab
5. Verify conversion appears with CAPI badge

**Check Attribution:**

1. View conversion in DATALYR Event Stream
2. Verify fbclid is present
3. Confirm conversion sent to Meta (check logs)

## Meta Conversions API (CAPI)

### Why CAPI Matters

**Browser Pixel Limitations:**

* iOS 14+ tracking restrictions
* Ad blockers prevent pixel loading
* Cookie consent requirements
* Browser privacy features

**CAPI Benefits:**

* Server-side tracking (no browser required)
* Works reliably where browser pixels are limited on iOS
* Immune to ad blockers
* More accurate attribution
* Better event matching

### How CAPI Works

**Browser Pixel:**

```javascript theme={null}
// Meta Pixel fires client-side
fbq('track', 'Purchase', {
  value: 99.99,
  currency: 'USD'
});
```

**CAPI (Server-Side):**

```javascript theme={null}
// DATALYR sends to Meta Conversions API
POST https://graph.facebook.com/v25.0/{pixel_id}/events
{
  "data": [{
    "event_name": "Purchase",
    "event_time": 1727634645,
    "event_id": "evt_123",
    "event_source_url": "https://example.com/checkout",
    "action_source": "website",
    "user_data": {
      "em": "hashed_email",
      "ph": "hashed_phone",
      "fbp": "_fbp_cookie",
      "fbc": "_fbc_click_cookie",
      "external_id": "user_456"
    },
    "custom_data": {
      "value": 99.99,
      "currency": "USD"
    }
  }]
}
```

### Deduplication

DATALYR automatically deduplicates between browser pixel and CAPI:

**Event ID Matching:**

* Browser pixel: `fbq('track', 'Purchase', {...}, {eventID: 'evt_123'})`
* CAPI: `event_id: 'evt_123'`
* Meta deduplicates if event\_id matches

**How DATALYR Handles It:**

```javascript theme={null}
// DATALYR uses consistent event IDs
Browser Pixel Event ID: evt_order_123_1727634645
CAPI Event ID:         evt_order_123_1727634645

Result: Meta counts as ONE conversion (not two)
```

## Enhanced Matching

DATALYR sends hashed customer data to improve match rates:

### Customer Data Sent

**Always Hashed (SHA-256):**

* Email address
* Phone number
* First name
* Last name
* City, state, zip code
* Country

**Example:**

```javascript theme={null}
// Original data (NEVER sent in plain text)
email: "user@example.com"

// Hashed with SHA-256 before sending
em: "b4c9a289323b21a01c3e940f150eb9b8c542587f1abfd8f0e1cc1ffc5e475514"
```

### Browser IDs

**\_fbp (Facebook Browser Pixel):**

* First-party cookie set by Meta Pixel
* Identifies browser/device
* Format: `fb.1.{timestamp}.{random}`

**\_fbc (Facebook Click):**

* Contains fbclid from ad click
* Tracks which specific ad was clicked
* Format: `fb.1.{timestamp}.{fbclid}`

**fbclid (Facebook Click ID):**

* Appended to URL by Meta ads
* Unique identifier for each ad click
* Used for attribution and conversion matching

## Attribution

### Cross-Device Attribution

DATALYR attributes conversions across devices using identity resolution:

**Example:**

**Mobile (Day 1):**

```
User clicks Facebook ad on mobile
fbclid captured: IwAR123
Visitor ID: anon_mobile_456
```

**Desktop (Day 7):**

```
User purchases on desktop
Different visitor ID: anon_desktop_789
BUT same email: user@example.com

DATALYR links:
1. anon_mobile_456 → user@example.com
2. anon_desktop_789 → user@example.com
3. Finds fbclid from anon_mobile_456
4. Sends conversion with original fbclid
```

**Result:** Desktop purchase attributed to mobile Facebook ad.

### Attribution Windows

**Meta Default Windows:**

* 7-day click attribution
* 1-day view attribution

**DATALYR Settings:**

* 30-day attribution window (default)
* Configurable per workspace

If user clicks ad on Day 1 and converts on Day 35, conversion is NOT sent to Meta (outside window).

## Event Mapping

### Standard Events

Map your events to Meta standard events for better optimization:

| Your Event         | Meta Event           | Use Case                |
| ------------------ | -------------------- | ----------------------- |
| `purchase`         | Purchase             | E-commerce checkout     |
| `add_to_cart`      | AddToCart            | Add item to cart        |
| `begin_checkout`   | InitiateCheckout     | Start checkout          |
| `signup`           | CompleteRegistration | User registration       |
| `lead`             | Lead                 | Lead form submission    |
| `view_content`     | ViewContent          | Product page view       |
| `search`           | Search               | Site search             |
| `add_payment_info` | AddPaymentInfo       | Payment details entered |

### Custom Events

Send custom events for specific business needs:

```javascript theme={null}
// Track custom milestone
datalyr.track('trial_started', {
  plan: 'pro',
  trial_days: 14
});

// Maps to Meta custom event
Event Name: trial_started
Custom Data: {plan: 'pro', trial_days: 14}
```

## Advanced Features

### Action Source

DATALYR automatically sets the correct `action_source`:

**website:** Web-based conversions (default)

```
User converts on website
action_source: "website"
```

**app:** Mobile app conversions

```
User converts in iOS/Android app
action_source: "app"
```

**physical\_store:** Offline conversions

```
User purchases in store
action_source: "physical_store"
```

### Limited Data Use (LDU)

For California (CCPA) and other privacy-restricted regions:

```javascript theme={null}
// DATALYR automatically sets LDU for California users
{
  "data_processing_options": ["LDU"],
  "data_processing_options_country": 1, // USA
  "data_processing_options_state": 1000 // California
}
```

### Test Events

Test conversions before going live:

1. Enable test mode in DATALYR
2. Create test conversion
3. Check Meta Events Manager → Test Events
4. Verify data is correct
5. Disable test mode for production

## Troubleshooting

### Conversions Not Appearing in Meta

**Check:**

1. Integration is connected and active
2. Meta Pixel ID is correct
3. Event has fbclid or \_fbp cookie
4. Event is within attribution window (30 days)
5. Check Events Manager for errors

**Common Issues:**

* Missing fbclid: User didn't come from Meta ad
* Duplicate event IDs: Check deduplication
* Invalid user data: Email/phone must be hashed

### Low Match Rate

**Improve matching by:**

1. Send email with every conversion
2. Include phone number when available
3. Send full address data (city, state, zip)
4. Ensure \_fbp and \_fbc cookies are captured

**Check:**

```
Meta Events Manager → Data Sources → Your Pixel → Match Quality
```

Target: 60%+ match rate

### Attribution Discrepancies

**DATALYR vs Meta Numbers:**

Common reasons for differences:

* Attribution windows differ (DATALYR: 30d, Meta: 7d)
* View-through attribution (Meta counts, DATALYR doesn't by default)
* Conversion time differences (DATALYR uses event time, Meta uses ad time)

**Best Practice:** Use DATALYR for truth, Meta for optimization signals.

## Data Privacy

**Customer Data:**

* All emails, phones, names are SHA-256 hashed
* No plaintext PII sent to Meta
* GDPR and CCPA compliant
* Opt-out respected

**Hashing Example:**

```javascript theme={null}
// Before hashing
Email: "user@example.com"

// After hashing (sent to Meta)
em: "b4c9a289323b21a01c3e940f150eb9b8c542587f1abfd8f0e1cc1ffc5e475514"

// Meta matches hash to their users without seeing email
```

## Next Steps

<CardGroup cols={3}>
  <Card title="Google Ads" icon="google" href="/integrations/google-ads">
    Send conversions to Google Ads
  </Card>

  <Card title="TikTok Ads" icon="tiktok" href="/integrations/tiktok-ads">
    Track TikTok ad performance
  </Card>

  <Card title="OpenAI Ads" icon="https://mintcdn.com/datalyr/lfO2N_Gfr0p_cPiJ/logos/openai-logo.png?fit=max&auto=format&n=lfO2N_Gfr0p_cPiJ&q=85&s=8f10f5c1fb139ba14740a9791d32fa5e" href="/integrations/openai-ads" width="1000" height="1000" data-path="logos/openai-logo.png">
    Send conversions to OpenAI's CAPI
  </Card>

  <Card title="Conversion Rules" icon="arrows-turn-to-dots" href="/integrations/conversion-rules">
    Configure custom conversion rules
  </Card>

  <Card title="Customer Journeys" icon="route" href="/features/customer-journeys">
    View complete customer paths
  </Card>
</CardGroup>

## Need Help?

Questions about Meta Ads integration? Check our [troubleshooting guide](/troubleshooting/integration-errors) or contact support.
