> 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/ads/googleadmob.loadappsintossadmob.md).

# GoogleAdMob.loadAppsInTossAdMob

### 功能说明

预先加载 Google AdMob 广告，以便在需要广告时可以立即展示。广告成功加载后 `onEvent` 会通过回调 `type: 'loaded'` 会传递事件。

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

### 类型

```ts
GoogleAdMob.loadAppsInTossAdMob(params: LoadAdMobParams): () => void;
```

**Params**

```ts
interface LoadAdMobParams {
  options: LoadAdMobOptions;
  onEvent: (event: LoadAdMobEvent) => void;
  onError: (error: unknown) => void;
}

type LoadAdMobOptions = {
  /** 是广告组 ID。请填入在 Apps in Toss 控制台中获取的值。 */
  adGroupId: string;
  /** 用于区分广告请求来源的值。 */
  referrer?: string | null;
};

type LoadAdMobEvent = {
  type: "loaded";
  data: LoadAdMobResult;
};

type LoadAdMobResult = {
  /** 是广告组 ID。 */
  adGroupId: string;
  /** 是广告 ID。 */
  adUnitId: string;
  /** 是广告加载响应信息。 */
  responseInfo: {
    /** 是广告网络响应信息数组。 */
    adNetworkInfoArray: AdNetworkResponseInfo[];
    /** 是已加载的广告网络响应信息。 */
    loadedAdNetworkInfo: AdNetworkResponseInfo | null;
    /** 用于识别广告响应的 ID。 */
    responseId: string | null;
  };
};

type AdNetworkResponseInfo = {
  /** 是广告来源 ID。 */
  adSourceId: string;
  /** 是广告来源名称。 */
  adSourceName: string;
  /** 是广告来源实例 ID。 */
  adSourceInstanceId: string;
  /** 是广告来源实例名称。 */
  adSourceInstanceName: string;
  /** 是广告网络类名。 */
  adNetworkClassName: string | null;
};
```

**Response**

取消订阅函数(`() => void`)会返回。调用后，之后到达的事件和错误不会再通过回调传递。

### 错误

如果广告加载失败 `onError` 错误会通过回调传递。在非 Toss App WebView 环境中调用时，会在调用时发生错误。

### 示例代码

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

function loadAd() {
  if (!GoogleAdMob.loadAppsInTossAdMob.isSupported()) {
    console.warn("当前环境中无法使用 AdMob 广告。");
    return;
  }

  GoogleAdMob.loadAppsInTossAdMob({
    options: { adGroupId: "AD_GROUP_ID" },
    onEvent: (event) => {
      if (event.type === "loaded") {
        console.log("广告加载完成：", event.data.responseInfo.responseId);
      }
    },
    onError: (error) => {
      console.error("广告加载失败：", error);
    },
  });
}
```


---

# 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/ads/googleadmob.loadappsintossadmob.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.
