> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Interstitial Ads — Logic xuất hiện (review)

> Tổng hợp **khi nào Interstitial hiện** để PO/dev check. Nguồn: code `src/core/sdk/ads.ts`,
> `ad-flow.ts`, các call-site + [ad-trigger-logic.md](/docs/ad-trigger-logic.md). SDK path:
> `getAdsService()` from `@core/ads` (business gating vẫn ở `ads.ts`). Cập nhật khi đổi logic.

## 🚩 Đang dùng Flow nào? → **Flow A** (default)

- `ad-flow.ts`: `activeFlow` mặc định = `AD_FLOW_A`.
- RC key **`fs_logic`** quyết định (0 = Flow A, 1 = Flow B); schema default = **0** (`src/core/config/rc-contract.ts` là nguồn duy nhất cho tên key / kiểu / mô tả).
- Feed qua `remote-config.store.ts` → `setAdFlow(config.fs_logic)` khi RC apply.
- ⇒ Trừ khi **PO đặt `fs_logic = 1`** trên Firebase Remote Config console, app chạy **Flow A**.
- (Xác nhận từ log device: interstitial `select_song` có bắn → chỉ Flow A mới bắn cái này.)

> Đổi sang Flow B: set `fs_logic = 1` trên Firebase RC console (template: `docs/firebase-remote-config.template.json`, sinh bằng `bun run rc:template`).

***

## ⛔ Luật chung (mọi placement, cả 2 flow): inter đầu tiên sau `song_end` thứ 2

User chỉ gặp interstitial **đầu tiên** sau khi đã có **2 `song_end`** trong đời app: tutorial là
bài 1, bài user tự chọn đầu tiên là bài 2. Trước đó mọi placement (`select_song`, `song_result`,
`pause`, `free_home`, `song_end`, `result_home`, Learn result) đều bị bỏ qua, và SDK không được gọi.

- Ví dụ PO báo (Flow A): xong tutorial → không nhận bài free → Home → bấm **Play** bài free →
  **không** dính `select_song`. Có `song_end` thứ 2 rồi thì các luật cũ mới chạy.
- Learn chỉ bắn `song_end` khi lượt Challenge của chặng cuối kết thúc. Learn Mode **khoá cho tới
  khi đủ 4 `song_end`** (tutorial tính; xem [features/songs.md](/docs/features/songs.md#learn-mode-opens-after-four-finished-songs)),
  nên khi user vào được Learn thì ngưỡng `song_end` thứ 2 đã qua từ lâu.
- Ở Flow B, ad `song_end` của bài 2 hiện được luôn: `fireSongEnd` đếm trước khi màn Result gọi ad.
- Số đếm = `accumulated_count` của event `song_end` (`getLifetimeEventCount("song_end")`, lưu MMKV,
  mất khi xoá app). Gate ở `maybeShowInterstitial` + `canShowFreeModeInterstitial` (`src/core/sdk/ads.ts`) khi slice
  chưa boot; sau đó là `fs.first_after_song_end` của arm (mặc định 2).

## Cơ chế A/B — 2 flow **loại trừ nhau**

Màn hình chỉ **báo hành động** (`reportAdAction`, [`ad-actions.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ad-actions.ts)); bộ quyết định chọn thời điểm theo Flow A/B: bảng dưới đây khi slice chưa boot, policy của arm sau khi boot (xem [ad-trigger-logic.md](/docs/ad-trigger-logic.md#monetization-slice-mon_pkg)). Flow A và Flow B **không bao giờ chạy cùng lúc**.

### 🅰️ Flow A — USER ACTION (fs_logic = 0, **đang active**)

Interstitial bắn khi user **bấm**:

| Placement     | Khi nào                                                                                                                                                                                                                              | Gating riêng                   | Call-site                                                                                           |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------ | --------------------------------------------------------------------------------------------------- |
| `select_song` | Bấm **Play** hoặc **Learn** 1 bài free (để vào gameplay / Learn Mode). Pill **Learn đang khoá** (chưa đủ 4 `song_end`) chỉ mở tooltip; **Claim** ở popup "Learn Mode unlocked!" vào thẳng Learn — cả hai **không** chạy interstitial | —                              | `SongsScreen.onPressPlay`, `SongsScreen.onPressLearn`                                               |
| `song_result` | Ở màn **Song Result** bấm **Home / Replay / Next**                                                                                                                                                                                   | —                              | `PlayResultScreen`; Learn result panel (`LearnModeScreen` `gateResultLaunchAd` / `leaveFromResult`) |
| `pause`       | Ở **Pause** bấm **Home / Replay**                                                                                                                                                                                                    | **Σ play-time cộng dồn > 10s** | `PlayScreen` (`reportPlaySessionEndTicks`)                                                          |

### 🅱️ Flow B — SONG_END (fs_logic = 1, variant — **đang TẮT**)

Interstitial bắn khi **bài kết thúc**:

| Placement               | Khi nào                                                    | Gating riêng                                           |
| ----------------------- | ---------------------------------------------------------- | ------------------------------------------------------ |
| `song_end`              | Bài chơi xong (game-over) → **trước** màn Result           | —                                                      |
| `result_home`           | Rời Result → **Home**                                      | **song_count ≥ 2** (số bài xong kể từ lần cuối ở Home) |
| `song_end` (Learn Mode) | Hoàn thành **run challenge của stage cuối** (xem bên dưới) | —                                                      |

Ở Flow B: **Replay / Next / recommendation KHÔNG** ad; `select_song` / `song_result` / `pause` **tắt**.

### Cả 2 flow (orthogonal)

\| `free_home` | Rời **tab Keyboard** (free mode) → **Home / Songs** | `KeyboardScreen` |

### `song_end` của Learn Mode (Flow B)

\| `song_end` (Learn Mode) | Hoàn thành **run challenge của stage cuối** — run xong khi **nốt cuối được chấm** (bấm trúng, hoặc hết cửa sổ (±150ms với loa điện thoại, ±250ms với tai nghe) mà không ai bấm), **không** chờ nhạc chạy tới cue marker → board đứng **3.5s**: nhạc **chơi tiếp 2s** sau nốt cuối rồi **fade 1.5s về im lặng** (`RUN_ENDING_MS`), rồi result panel lên và ad hiện **đè lên** nó (1 lần/run; panel vẽ trước khi xin ad nên đóng ad là thấy result; count-up + nút bấm chờ ad xong). Không có ad (subscriber / remove-ads): panel cũng chờ hết 3.5s đó rồi mới lên | `LearnModeScreen` (`useLearnResultInterstitial`) |

Learn result **Replay / Next**: Flow A chạy `song_result` rồi `waitForFullscreenAdDismissed` **trước** khi clear panel / mount count-in / replace route (giống `PlayResultScreen.launchPlay`). Flow B skip (đã có `song_end`). Song list: Flow A `song_result`+wait; Flow B `result_home`.

***

## ⚙️ Gating — interstitial chỉ hiện khi **TẤT CẢ** đúng

Khi slice chưa boot, thứ tự kiểm tra trong `ads.ts maybeShowInterstitial` (sau khi boot là policy của arm: `ConfigFullscreenLogic` + `FsSpacing`, các field `fs.*` của `mon_pkg`):

1. **Không phải subscriber** — premium / no-ads → skip (không ad).
2. **Placement đang bật** — `ads.shouldShow(placement)`.
3. **≥ 15s** kể từ interstitial gần nhất — `interstitialIntervalMs` (global frequency-cap).
4. **≥ 30s** kể từ rewarded gần nhất — `rewardedToInterstitialIntervalMs` (1 chiều: rewarded KHÔNG bị gate, chỉ ghi timestamp).
5. _(chỉ `pause`)_ **Σ play-time cộng dồn > 10s** — `playTimeThresholdS`. Reset = 0 khi ad thực sự hiện.
6. _(chỉ `result_home`, Flow B)_ **song_count ≥ 2** — `resultHomeSongCountThreshold`.

Nếu qua hết gate nhưng **SDK không có ad** (no-fill 509) → outcome `unavailable`.

## 🎛️ Remote Config keys (default)

| Key                                    | Default | Ý nghĩa                                       |
| -------------------------------------- | ------- | --------------------------------------------- |
| `fs_logic`                             | **0**   | 0 = Flow A, 1 = Flow B                        |
| `interstitial_interval_ms`             | 15000   | khoảng cách tối thiểu giữa 2 interstitial     |
| `rewarded_to_interstitial_interval_ms` | 30000   | khoảng cách tối thiểu rewarded → interstitial |
| `play_time_threshold_s`                | 10      | Σ play-time cho `pause` flow                  |
| `result_home_song_count_threshold`     | 2       | song_count cho `result_home` (Flow B)         |

***

## ⚠️ Interstitial ≠ Rewarded (đừng nhầm)

- **Interstitial** = ad **ép** ở các điểm trên (forced).
- **Rewarded** = user **chủ động** xem để **unlock bài locked-ads** (`unlock_song`). Rewarded KHÔNG bị gate 15s/30s.
- **Modal / video backfill** (✅ PO chốt): **Rewarded-unlock** và **interstitial no-fill** hiện **Skia video backfill** (CDN fallback). Video + đồng hồ 15s **pause khi app background**, resume khi foreground. Thanh progress theo video; nút **sound toggle** (góc trái, mặc định bật tiếng); nút **Close** (góc phải) xuất hiện sau **15s** foreground watch. RC key **`video_ads_backfill_url`**. Analytics: `videoads_backfill` / `fullads_backfill`.

## Ngoài scope

- **App-Open ads**: ✅ code, tách khỏi interstitial (format AdMob riêng, luật riêng, event `openads_*` như Guitar — xem [analytics-events.md](/docs/analytics-events.md)). Từ `song_end` trọn đời ≥ 4 (4 bài chơi xong, cộng dồn): hiện sau splash ở lần mở thứ 2+ (trước paywall mở app, chờ nạp tối đa 5s) và khi quay lại từ background trên màn menu (chỉ khi đã nạp sẵn). Cách ≥ 120s App Open trước và ≥ 60s interstitial / rewarded gần nhất; App Open **không** dời mốc 15s của interstitial. Chi tiết: [ad-trigger-logic.md §6](/docs/ad-trigger-logic.md).
- **Banner**: top tab Keyboard, subscriber ẩn.
- **No fill** (`googleMobileAds/no-fill`): inventory / tài khoản AdMob (unit mới chưa có fill, app chưa duyệt), không phải code — xem Ad Inspector ([admob-integration.md](/docs/admob-integration.md#9-qa--admob-ad-inspector)).

***

<small>Source: [`docs/interstitial-ads-logic.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/interstitial-ads-logic.md)</small>
