play-console-cli icon indicating copy to clipboard operation
play-console-cli copied to clipboard

Feature: AdMob API integration for ad monetization reporting (admob/v1)

Open tamtom opened this issue 8 months ago • 3 comments

Summary

Integrate the Google AdMob API (v1) into the gplay CLI to provide programmatic access to AdMob account data, ad unit inventory, and ad revenue reporting — giving developers a unified monetization view alongside existing Play Console financial reports, subscriptions, and in-app purchase commands.

Why This Is Applicable to Google Play

A significant number of Play Console developers monetize their Android apps with AdMob. Ad revenue from banners, interstitials, rewarded ads, and app-open ads is a critical metric alongside IAP and subscription earnings. The gplay CLI already covers IAP (gplay iap), subscriptions (gplay subscriptions), one-time products (gplay otp), and financial reports (gplay reports financial) — but ad monetization data from AdMob is completely missing. Adding AdMob closes this gap and gives developers a single CLI for all Google Play monetization workflows.

Problem

Today, developers who monetize with AdMob must:

  • Switch to the AdMob web console (admob.google.com) to check ad revenue, eCPM, fill rates, and impression data
  • Manually cross-reference AdMob earnings with Play Console financial reports
  • Cannot script or automate ad performance monitoring alongside their existing gplay CI/CD pipelines
  • Have no CLI-based way to list ad units, inspect mediation performance, or generate ad revenue reports

Goals

  • Provide read-only access to AdMob account data, apps, and ad units via the CLI
  • Generate network reports (ad revenue, impressions, CTR, RPM) with flexible date ranges, dimensions, and metrics
  • Generate mediation reports to analyze third-party ad source performance
  • Support the same output formats as existing commands (JSON, table, markdown)
  • Support --paginate for listing apps and ad units
  • Handle the OAuth2 authentication requirement (AdMob does not support service accounts — only OAuth2 desktop/web flows)
  • Store AdMob publisher ID in gplay config for seamless usage

Proposed Commands

Account

# Get AdMob account info (publisher ID, timezone, currency)
gplay admob account get

# List all accessible AdMob publisher accounts
gplay admob account list

Apps

# List apps linked to the AdMob account
gplay admob apps list [--paginate] [--output json|table|markdown]

# Output includes: name, appId, platform (ANDROID/IOS), appApprovalState, linkedAppInfo

Ad Units

# List ad units for the account
gplay admob ad-units list [--paginate] [--output json|table|markdown]

# Filter by app
gplay admob ad-units list --app-id "ca-app-pub-1234567890~0987654321"

# Output includes: adUnitId, displayName, appId, adFormat (BANNER, INTERSTITIAL, REWARDED, NATIVE, APP_OPEN), adTypes

Network Reports

# Generate a network report (ad revenue from AdMob network)
gplay admob reports network \
  --start-date 2026-03-01 \
  --end-date 2026-03-31 \
  --dimensions DATE,APP,COUNTRY \
  --metrics ESTIMATED_EARNINGS,IMPRESSIONS,CLICKS,AD_REQUESTS,MATCHED_REQUESTS,MATCH_RATE \
  [--sort ESTIMATED_EARNINGS:desc] \
  [--filter-country US,CA,GB] \
  [--currency USD] \
  [--output json|table|markdown]

# Quick: last 7 days earnings summary
gplay admob reports network --last 7d --metrics ESTIMATED_EARNINGS,IMPRESSION_RPM

Mediation Reports

# Generate a mediation report (performance across ad sources)
gplay admob reports mediation \
  --start-date 2026-03-01 \
  --end-date 2026-03-31 \
  --dimensions DATE,AD_SOURCE,APP \
  --metrics ESTIMATED_EARNINGS,IMPRESSIONS,OBSERVED_ECPM,CLICKS \
  [--sort OBSERVED_ECPM:desc] \
  [--output json|table|markdown]

# Analyze a specific ad source
gplay admob reports mediation --last 30d --dimensions AD_SOURCE --metrics ESTIMATED_EARNINGS,OBSERVED_ECPM

Implementation Details

API Endpoints

Resource Method Endpoint
Accounts GET GET /v1/accounts
Account GET GET /v1/{name=accounts/*}
Apps LIST GET /v1/{parent=accounts/*}/apps
Ad Units LIST GET /v1/{parent=accounts/*}/adUnits
Network Report POST POST /v1/{parent=accounts/*}/networkReport:generate
Mediation Report POST POST /v1/{parent=accounts/*}/mediationReport:generate

Base URL: https://admob.googleapis.com

Discovery document: https://admob.googleapis.com/$discovery/rest?version=v1

Authentication Requirements

Important: AdMob API does NOT support service accounts. It requires OAuth2 user-based authorization.

  • OAuth2 Scopes:
    • https://www.googleapis.com/auth/admob.readonly — full read access to account, inventory, mediation settings, and reports
    • https://www.googleapis.com/auth/admob.report — read access to ad performance and earnings reports only
  • Supported Flows: Desktop app (localhost redirect) or Web app (callback URL)
  • Implementation: New gplay admob auth subcommand to run the OAuth2 flow, store refresh token in config. This is separate from the existing Play Console service account auth.
# Authenticate with AdMob (opens browser for OAuth2 consent)
gplay admob auth login [--scope readonly|report]

# Check AdMob auth status
gplay admob auth status

New Packages

  • internal/cli/admob/ — Command group and subcommands (account, apps, ad-units, reports, auth)
  • internal/admobclient/ — AdMob API client wrapper using google.golang.org/api/admob/v1

Go Client Library

Package: google.golang.org/api/admob/v1

Key types:

  • admob.Service — root service
  • admob.AccountsService — account operations
  • admob.AccountsAppsService — app listing with pagination
  • admob.AccountsAdUnitsService — ad unit listing with pagination
  • admob.AccountsNetworkReportService — network report generation
  • admob.AccountsMediationReportService — mediation report generation
  • admob.NetworkReportSpec — date range, dimensions, metrics, filters, sort conditions
  • admob.MediationReportSpec — same structure for mediation data
  • admob.GenerateNetworkReportResponse — streaming response with Header, Row, Footer
  • admob.PublisherAccount — publisher ID, timezone, currency code

AdMob Account / Publisher ID Configuration

Add to ~/.gplay/config.yaml:

admob:
  publisher_id: "pub-1234567890123456"
  default_scope: "readonly"
  # OAuth2 tokens stored securely

New environment variable: GPLAY_ADMOB_PUBLISHER_ID

Report Generation Details

Network Report Dimensions: DATE, MONTH, WEEK, AD_UNIT, APP, AD_TYPE, COUNTRY, FORMAT, PLATFORM, MOBILE_OS_VERSION, GMA_SDK_VERSION, APP_VERSION_NAME, SERVING_RESTRICTION

Network Report Metrics: AD_REQUESTS, CLICKS, ESTIMATED_EARNINGS, IMPRESSIONS, IMPRESSION_CTR, IMPRESSION_RPM, MATCHED_REQUESTS, MATCH_RATE, SHOW_RATE

Mediation Report Dimensions: DATE, MONTH, WEEK, AD_SOURCE, AD_SOURCE_INSTANCE, AD_UNIT, APP, MEDIATION_GROUP, COUNTRY, FORMAT, PLATFORM, MOBILE_OS_VERSION, GMA_SDK_VERSION, APP_VERSION_NAME, SERVING_RESTRICTION

Mediation Report Metrics: AD_REQUESTS, CLICKS, ESTIMATED_EARNINGS, IMPRESSIONS, IMPRESSION_CTR, MATCHED_REQUESTS, MATCH_RATE, OBSERVED_ECPM

Date Ranges: Specified via --start-date / --end-date (YYYY-MM-DD) or convenience flag --last (7d, 30d, 90d)

Sorting: --sort METRIC:asc|desc (e.g., --sort ESTIMATED_EARNINGS:desc)

Filtering: --filter-country US,CA maps to DimensionFilter with MatchesAny

Timezone note: The API only supports the account default timezone and America/Los_Angeles

Streaming responses: Report endpoints return streaming JSON (header → rows → footer). The client must aggregate rows before formatting output.

Earnings format: ESTIMATED_EARNINGS and IMPRESSION_RPM are returned in micros (1,000,000 = $1.00). The CLI should convert to human-readable currency in table/markdown output.

Output Format Considerations

  • JSON output: raw API response (micros for monetary values, preserving full precision)
  • Table/markdown output: convert micros to currency (e.g., $6.50), format rates as percentages, use the account's CurrencyCode
  • Report headers should show the date range and any applied filters
  • Footer warnings (e.g., data delays) should be shown on stderr

Acceptance Criteria

  • [ ] gplay admob account get returns publisher ID, timezone, and currency code
  • [ ] gplay admob account list lists all accessible publisher accounts
  • [ ] gplay admob apps list lists AdMob apps with pagination support
  • [ ] gplay admob ad-units list lists ad units with optional app filter and pagination
  • [ ] gplay admob reports network generates network reports with configurable date range, dimensions, metrics, filters, and sort
  • [ ] gplay admob reports mediation generates mediation reports with the same configurability
  • [ ] gplay admob auth login runs OAuth2 desktop flow and persists refresh token
  • [ ] gplay admob auth status shows current authentication state
  • [ ] All commands support --output json|table|markdown
  • [ ] Monetary values in micros are converted to human-readable format in table/markdown output
  • [ ] --paginate works for apps and ad-units listing
  • [ ] --dry-run works for report generation (shows the request spec without executing)
  • [ ] Publisher ID configurable via --publisher-id flag, config file, or GPLAY_ADMOB_PUBLISHER_ID env var
  • [ ] Comprehensive test coverage including CLI flag validation, output formatting, and API client mocking
  • [ ] Commands registered in internal/cli/registry/registry.go
  • [ ] --help is self-documenting for all new commands and flags

References

tamtom avatar Apr 04 '26 18:04 tamtom

Implementation Checklist

Command & Registration

  • [ ] Command group gplay admob in internal/cli/admob/
  • [ ] Subcommands: accounts list/get, apps list, ad-units list, reports network, reports mediation, reports generate
  • [ ] Registered in internal/cli/registry/registry.go
  • [ ] UsageFunc: shared.DefaultUsageFunc set on all commands
  • [ ] Dedicated client wrapper for admob/v1 API
  • [ ] Required flags validated with stderr error messages

Tests

  • [ ] CLI black-box tests in internal/cmdtest/admob_test.go
  • [ ] Unit tests for report date range validation and metric formatting
  • [ ] Error paths: missing --account, invalid date range, unknown report type

Docs & Help

  • [ ] --help output follows existing conventions
  • [ ] Command docs regenerated: gplay docs generate

Validation (before opening PR)

make format
make lint
make test
make build
/tmp/gplay admob --help
/tmp/gplay admob accounts list --help
/tmp/gplay admob reports network --help
/tmp/gplay admob reports network           # missing required flags → stderr error

tamtom avatar Apr 05 '26 04:04 tamtom

Addendum to checklist above — docs CI gate:

The PR CI now runs make check-docs on every PR. This means:

  1. After implementing the command, run make docs to regenerate GPLAY.md
  2. Commit the updated GPLAY.md alongside your code changes
  3. If you forget, the PR check will fail with: "GPLAY.md is out of date"

This ensures every user-facing command, flag, and help text is always reflected in the reference docs.

tamtom avatar Apr 05 '26 04:04 tamtom

Consolidating individual feature requests into the roadmap so new visitors see a clean tracker. This item is on the roadmap: #245 — please 👍 there and comment which item you're waiting on. Reopen or file a fresh issue if you start on a PR.

tamtom avatar Jul 05 '26 10:07 tamtom