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

# Meta Ads

> Detect when a WhatsApp conversation comes from a Meta ad (Click to WhatsApp) and use that attribution in your Brain Studio flows.

**Click to WhatsApp (CTWA)** ads on Facebook and Instagram open a WhatsApp conversation when the user clicks. This guide explains **how to know if a chat comes from a Meta ad**: when the conversation originates from an ad, Jelou automatically injects a `_referral` object into memory with the source ad's data.

With that information you can **attribute the lead**, route by campaign, personalize the first reply, or log the source in your CRM—without manually reconciling data between Meta Ads Manager and the conversational channel.

## How it works

When a user clicks a **Click to WhatsApp** ad, WhatsApp opens with a starter message and Meta attaches the ad's referral data to that first interaction. Jelou detects that data and automatically inserts it into the conversation's memory under the `_referral` variable.

<Note>
  No Marketplace installation is required. The `_referral` object is injected automatically whenever the conversation originates from a Click to WhatsApp ad.
</Note>

## Detecting whether a chat comes from an ad

To know if a conversation arrived from a Meta ad, check whether the `_referral` variable exists in memory. When the conversation originates from an ad, Jelou inserts the following object:

```json theme={null}
{
  "_referral": {
    "source_url": "https://fb.me/XXXXX",
    "source_id": "120XXXXXXXXX387",
    "source_type": "ad",
    "body": "BODY_CONTENT",
    "media_type": "video",
    "video_url": "https://www.facebook.com/story.php?story_fbid=XXXXX",
    "thumbnail_url": "https://scontent.ftru6-2.fna.fbcdn.net/v/XXXXXXXXX",
    "ctwa_clid": "CTWA",
    "welcome_message": {
      "text": "WELCOME_MESSAGE"
    }
  }
}
```

If `_referral` does not exist in memory, the conversation did **not** originate from a Click to WhatsApp ad (for example, a user messaging the number directly).

Learn more about how to read these variables in: [Memory](/en/guides/variables/memory).

## Field reference

| Field                  | Description                                                                                          |
| ---------------------- | ---------------------------------------------------------------------------------------------------- |
| `source_url`           | Short URL of the source ad (for example, an `fb.me` link).                                           |
| `source_id`            | Identifier of the source ad or post in Meta.                                                         |
| `source_type`          | Source type. The value `ad` indicates the conversation came from an ad.                              |
| `body`                 | Main ad copy the user saw before clicking.                                                           |
| `media_type`           | Ad content type: `image` or `video`.                                                                 |
| `video_url`            | URL of the ad video. Present when `media_type` is `video`.                                           |
| `thumbnail_url`        | URL of the ad content's thumbnail.                                                                   |
| `ctwa_clid`            | *Click to WhatsApp Click ID*: unique identifier for the click, useful for attribution and reporting. |
| `welcome_message.text` | Welcome message configured in the ad, which starts the conversation.                                 |

<Note>
  Available fields depend on the ad. An image ad won't include `video_url`, and some fields may arrive empty depending on how the creative is configured.
</Note>

## Using it in your flow

Inside any node you can access the ad data with the memory syntax `{{$memory._referral.field}}`. For example:

* `{{$memory._referral.source_type}}` → `ad`
* `{{$memory._referral.ctwa_clid}}` → click identifier for attribution
* `{{$memory._referral.body}}` → the ad copy the user saw

A common way to leverage it is to **branch the flow based on the source**:

<Steps>
  <Step title="Check whether the conversation comes from an ad">
    At the start of the flow, evaluate whether `_referral` exists in memory. If it does, the conversation arrived from a Click to WhatsApp ad.
  </Step>

  <Step title="Save the attribution for reuse">
    Keep `ctwa_clid` and `source_id` in memory or send them to your CRM to attribute the lead to the source campaign.
  </Step>

  <Step title="Personalize or route based on the ad">
    Use `body`, `source_url`, or `source_id` to tailor the agent's first reply, route to the right team, or trigger an internal notification.
  </Step>
</Steps>

<Warning>
  The `_referral` object is only injected when the conversation **originates** from the ad. If the user already had a prior conversation with the number and messages again without going through the ad, the referral may not be available.
</Warning>

## Related articles

<CardGroup cols={2}>
  <Card title="Memory" href="/en/guides/variables/memory" icon="database">
    How to read and persist variables across conversations and nodes.
  </Card>

  <Card title="Google Ads" href="/en/guides/integraciones/marketing/google-ads" icon="google">
    Attribute WhatsApp conversations to your Google Ads campaigns.
  </Card>
</CardGroup>
