V1beta accounts.mediationGroups.patch on creating new mediationGroupLines in an exisitng mediation group

91 views
Skip to first unread message

Nguyen Viet Tung

unread,
Jun 30, 2024, 12:21:09 PMJun 30
to AdMob API Developer Forum
Hi team,

I'm using the v1beta "accounts.mediationGroups.patch" and it works fine when updating things like displayName or cpmMicros on existing mediationGroupLines. 
However, I cannot create a new mediationGroupLines by inputting a negative number in the line ID like the docs said even though I can do the same thing with "accounts.mediationGroups.create" normally.
It returned an error code 400 :  "The ad source adapter with id '0' referenced by the ad unit mapping '{displayName in my request body}' does not exist.\n\n"

Could you give me an example request for this?

Thank a lot!

Lin Yang

unread,
Jun 30, 2024, 12:22:21 PMJun 30
to AdMob API Developer Forum
Thanks for the question. Here's an end-to-end example for (a) setting up a mediation group using the POST MediationGroup API, (b) creating the ad unit mappings using the POST AdUnitMapping API, and (c) subsequently add mediation lines using the PATCH MediationGroup API.

(a) Create mediation group

Setup oauth

```
oauth2l reset && oauth2l header --json client_secret.json https://www.googleapis.com/auth/admob.monetization
```

Create mediation group using POST MediationGroup API.

A few notes:

* xxxxxxxxxxxxxxxx is your account id (can be fetched from https://developers.google.com/admob/api/reference/rest/v1beta/accounts/list).
* yyyyyyyyyy is your ad unit id (can be fetched from https://developers.google.com/admob/api/reference/rest/v1beta/accounts.adUnits/list).
* zzzzzzzzzzzzzzzz is your ad unit mapping id (can be fetched from https://developers.google.com/admob/api/reference/rest/v1beta/accounts.adUnits.adUnitMappings/list).
* ad_source_id is the id of one of the ad source that AdMob Mediation supports. See https://developers.google.com/admob/api/v1/ad-sources-reference for the full list. You can also obtain the list programatically using https://developers.google.com/admob/api/reference/rest/v1beta/accounts.adSources/list.

```
curl --http1.0 -X POST https://admob.googleapis.com/v1beta/accounts/pub-xxxxxxxxxxxxxxxx/mediationGroups -H "Content-Type: application/json" -H "$(oauth2l header --json client_secret.json https://www.googleapis.com/auth/admob.monetization)" --data @- << EOF
    {
      "displayName": "test mediation group",
      "targeting": {
        "platform": "ANDROID",
        "format": "BANNER",
        "adUnitIds": [
          "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy",
        ]
      },
      "state": "ENABLED",
      "mediationGroupLines": {
        "-1": {
          "displayName": "$0.1 line from ad source <ad_source_id>",
          "adSourceId": "<ad_source_id>",
          "cpmMode": "MANUAL",
          "cpmMicros": "100000",
          "state": "ENABLED",
          "adUnitMappings": {
            "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy":
            "accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings/zzzzzzzzzzzzzzzz",
          },
        },
      },
    }
EOF
```

(b) Create ad unit mappings

Before creating mediation lines, the new lines need to be linked to the ad source via ad unit mappings.

To create ad unit mappings, the request needs to specify the relevant configurations with a list of key-value pairs. The keys can be found from the Adapters resource (https://developers.google.com/admob/api/reference/rest/v1beta/accounts.adSources.adapters), programatically using the list adapters API: https://developers.google.com/admob/api/reference/rest/v1beta/accounts.adSources.adapters/list.

(b.1) List adapters

```
curl --http1.0 -X GET https://admob.googleapis.com/v1beta/accounts/pub-xxxxxxxxxxxxxxxx/adSources/<ad_source_id>/adapters -H "Content-Type: application/json" -H "$(oauth2l header --json client_secret.json https://www.googleapis.com/auth/admob.monetization)"
```

Look for the adapter entry that matches the platform and format of your mediation group. An example might look like this:

```
    {
      "name": "accounts/pub-xxxxxxxxxxxxxxxx/adSources/<ad_source_id>/adapters/<adapter_id>",
      "adapterId": "adapter_id",
      "title": "some ad source's sdk adapter name",
      "platform": "ANDROID",
      "formats": [
        "BANNER",
        "INTERSTITIAL",
        "BANNER_AND_INTERSTITIAL"
      ],
      "adapterConfigMetadata": [
        {
          "adapterConfigMetadataId": "adapter_key_id_1",
          "adapterConfigMetadataLabel": "some label name 1",
          "isRequired": true
        },
        {
          "adapterConfigMetadataId": "adapter_key_id_2",
          "adapterConfigMetadataLabel": "some label name 2",
          "isRequired": true
        }
      ]
    }
```

(b.2) Create ad unit mappings

Now we have enough information to create ad unit mappings. In this example, we'll create 3 new mediation lines with different manual CPMs. Therefore we'll need 3 new ad unit mappings. Repeat the command below 3 times, each with a different value for `ad_unit_configurations[<adapter_key_id_1>]` and `ad_unit_configurations[<adapter_key_id_2>]` pair.

```
curl --http1.0 -X POST https://admob.googleapis.com/v1beta/accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings -H "Content-Type: application/json" -H "$(oauth2l header --json client_secret.json https://www.googleapis.com/auth/admob.monetization)" --data @- << EOF
{
  "adapter_id": adapter_id,
  "state": "ENABLED",
  "ad_unit_configurations": {
    "adapter_key_id_1": "(placeholder for account id)",
    "adapter_key_id_2": "(placeholder for placement id)"
  }
}
EOF
```

API should return 3 ad unit mapping IDs. They look something like this: `accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings/zzzzzzzzzzzzzzzz`.

(c) Add mediation lines

Now we're ready to insert lines using the PATCH MediationGroup API:

```
curl --http1.0 -X PATCH https://admob.googleapis.com/v1beta/accounts/pub-xxxxxxxxxxxxxxxx/mediationGroups/<mediation_group_id>?updateMask=mediationGroupLines%5B%22-1%22%5D,mediationGroupLines%5B%22-2%22%5D,mediationGroupLines%5B%22-3%22%5D -H "Content-Type: application/json" -H "$(oauth2l header --json ~/admob.labpixies.prod.json https://www.googleapis.com/auth/admob.monetization)" --data @- << EOF
{
  "mediationGroupLines": {
    "-1": {
      "displayName": "0.15 cpm line",
      "adSourceId": "<ad_source_id>",
      "cpmMode": "MANUAL",
      "cpmMicros": "150000",
      "state": "ENABLED",
      "adUnitMappings": {
        "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy": "accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings/<ad unit mapping id 1 from step b.2>"
      }
  },
  "-2": {
      "displayName": "0.12 cpm line",
      "adSourceId": "<ad_source_id>",
      "cpmMode": "MANUAL",
      "cpmMicros": "120000",
      "state": "ENABLED",
      "adUnitMappings": {
        "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy": "accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings/<ad unit mapping id 1 from step b.2>"
      }
  },
  "-3": {
      "displayName": "0.13 cpm line",
      "adSourceId": "<ad_source_id>",
      "cpmMode": "MANUAL",
      "cpmMicros": "130000",
      "state": "ENABLED",
      "adUnitMappings": {
        "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy": "accounts/pub-xxxxxxxxxxxxxxxx/adUnits/yyyyyyyyyy/adUnitMappings/<ad unit mapping id 1 from step b.2>"
      }
    }
  }
}
EOF
```

(d) optionally, we can also verify the committed setup in the AdMob UI: https://apps.admob.com/v2/mediation/groups/<mediation group id>/edit

As for the validation error mentioned earlier in the original post, the AdMob API backend validation system checks the referential integrities across the entities covered by the mediation group, in order to prevent subsequent ads serving issues. For example, the ad unit mappings referenced by the mediation group are expected to be valid. The error message is possibly due to such validation checks. Hope the above example helps. If you're still hitting HTTP 400 validation errors, please feel free to reach out to your AdMob account manager, and we'd be happy to take a deeper look.

Nguyen Viet Tung

unread,
Jul 2, 2024, 12:08:31 PMJul 2
to AdMob API Developer Forum

Hi Lin Yang,

Many thanks for the prompt answer. Based on your example, I found where I did it wrong and can do the mediation group PATCH to create new mediationGroupLines now.

However, I have 2 more questions:

1. When creating a new mediation group, the state is always "ENABLED" even though I sent the request with "state": "DISABLED". How can I set the mediation group to disabled?
2. I set "idfaTargeting: "ALL" when creating the group, the responses were "ALL" and I used GET to double-check and it also returned "ALL", but when checking on AdMob dashboard, the idfaTargeting is "unspecified" as in my screenshot. Does this affect anything?

Screenshot 2024-07-02 at 11.30.46.png

Best,

Lin Yang

unread,
Jul 2, 2024, 12:15:30 PMJul 2
to AdMob API Developer Forum
For #1, unfortunately the mediation group APIs today don't support disabling the mediation group. It only supports pausing the mediation line items via the PATCH (aka update) mediation group API. We'll consider supporting this feature request.

For #2, correct, this doesn't affect ads serving behavior. We'll make a fix in the dashboard to avoid confusion. Thanks for flagging it.

Lin Yang

unread,
Jul 8, 2024, 11:19:11 PM (13 days ago) Jul 8
to AdMob API Developer Forum
JFYI we've implemented support for the aforementioned features and fixes now: support for deactivating MG upon creation with POST API, update MG status with PATCH API, and the dashboard fix. Please feel free to try again.
Reply all
Reply to author
Forward
0 new messages