> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/guang-gao/googleadmob.showappsintossadmob.md).

# GoogleAdMob.showAppsInTossAdMob

### 功能说明

`GoogleAdMob.loadAppsInTossAdMob`会向用户展示通过该方法预先加载的 AdMob 广告。广告展示过程中发生的事件会 `onEvent` 按顺序通过回调传递。

以事件风格运行，并返回一个用于解除 AppBridge 回调的函数。广告关闭后请务必解除。

调用前 `GoogleAdMob.showAppsInTossAdMob.isSupported()`可通过它确认是否支持。

### 类型

```ts
GoogleAdMob.showAppsInTossAdMob(params: ShowAdMobParams): () => void;
```

**参数**

```ts
interface ShowAdMobParams {
  options: ShowAdMobOptions;
  onEvent: (event: ShowAdMobEvent) => void;
  onError: (error: unknown) => void;
}

type ShowAdMobOptions = {
  /** 与 loadAppsInTossAdMob 中使用的广告组 ID 相同。 */
  adGroupId: string;
};

type ShowAdMobEvent =
  | { type: "requested" }
  | { type: "show" }
  | { type: "impression" }
  | { type: "clicked" }
  | { type: "dismissed" }
  | { type: "failedToShow" }
  | {
      type: "userEarnedReward";
      data: { unitType: string; unitAmount: number };
    };
```

事件 `type` 值：

| type               | 说明               |
| ------------------ | ---------------- |
| `requested`        | 广告展示请求成功了        |
| `show`             | 广告已显示在屏幕上        |
| `impression`       | 广告曝光已记录（产生收益的时点） |
| `clicked`          | 用户点击了广告          |
| `dismissed`        | 用户关闭了广告          |
| `failedToShow`     | 广告展示失败了          |
| `userEarnedReward` | 用户获得了奖励（仅限奖励广告）  |

**响应**

用于解除 AppBridge 回调的函数（`() => void`）会被返回。

### 错误

如果广告展示失败 `failedToShow` 事件或 `onError` 会通过回调传递。奖励广告 `userEarnedReward` 只有在该事件发生时才应发放奖励。 `dismissed`仅凭这一点无法知道用户是否看完了广告。

### 示例代码

```js
import { Ads } from "@apps-in-toss/web-framework";

const button = document.querySelector("#show-ad");

button.addEventListener("click", () => {
  const cleanup = GoogleAdMob.showAppsInTossAdMob({
    options: { adGroupId: "AD_GROUP_ID" },
    onEvent: (event) => {
      switch (event.type) {
        case "userEarnedReward":
          console.log(
            "获得奖励：",
            event.data.unitType,
            event.data.unitAmount,
          );
          break;
        case "dismissed":
          console.log("广告已关闭。");
          cleanup();
          break;
        case "failedToShow":
          console.error("广告展示失败了。");
          cleanup();
          break;
      }
    },
    onError: (error) => {
      console.error(error);
      cleanup();
    },
  });
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/guang-gao/googleadmob.showappsintossadmob.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
