← All Plugins/platform.google_marketingPython
AI-Agent-First Documentation
Raw .md/SKILL.md
Prompt your Agent: "Set up valstorm-plugin-google-marketing on our Open Valstorm workspace using platform.google_marketing and verify its routes and AI tools."

valstorm-plugin-google-marketing — Google Marketing & Analytics Plugin (NF-485)

Unified multi-tenant Google Ads, Google Search Console (GSC), Google Analytics 4 (GA4), and Google Tag Manager (GTM) plugin for Open Valstorm, mounted at platform.google_marketing (VALSTORM_PLUGINS=google-marketing or *).

Replaces standalone local CLI scripts and YAML/JSON credential files (google-ads.yaml, gcp_sa_key.json) with tenant-isolated encrypted credentials (platform.credentials.get("google_marketing")), FastAPI routes, and 14 AI Agent tools:

  • Google Ads (platform.google_marketing.ads): Campaign listing, search term waste audits, negative keyword injection, BOFU search campaign creation, and SHA-256 normalized Customer Match audience uploads.
  • Google Search Console (platform.google_marketing.gsc): Verified site listing, sitemap submission, search analytics query/CTR performance, and URL index coverage inspection.
  • Google Analytics 4 (platform.google_marketing.ga4): Daily engagement metrics, conversion event verification, and realtime active-user smoke tests.
  • Google Tag Manager (platform.google_marketing.gtm): Container inspection, tag/trigger/variable listing, and tag creation.

🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up)

Setting up 4 separate Google marketing APIs (Ads, Search Console, GA4, and Tag Manager) manually can be tedious. If you authenticate gcloud locally (and optionally provide your Google Ads Developer Token), your Valstorm AI Agent can enable all 5 APIs, create/reuse a marketing Service Account or OAuth refresh token, discover your accessible GA4 properties, GSC sites, and GTM containers automatically, save the unified encrypted credential, and run live smoke tests across all 4 services.

1. One-Time Human Prerequisite

bash
# 1. Install gcloud CLI (if not already installed) and authenticate
brew install --cask google-cloud-sdk
gcloud auth login
gcloud config set project YOUR_GCP_PROJECT_ID

# 2. (Optional) Authenticate Application Default Credentials with Marketing scopes
gcloud auth application-default login \
  --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/adwords,https://www.googleapis.com/auth/webmasters,https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/tagmanager.edit.containers"

2. Copy-Paste Prompt for Your Valstorm Agent

Drop this prompt into Valstorm Chat:

"Set up valstorm-plugin-google-marketing for my GCP project <YOUR_GCP_PROJECT_ID>. Follow plugins/valstorm-plugin-google-marketing/README.md to enable the Ads, GSC, GA4, and GTM APIs via gcloud, discover my GSC site URL, GA4 property ID, and GTM account/container IDs (or use <OPTIONAL_IDS_AND_ADS_DEV_TOKEN>), save the unified credential into platform.credentials.save('google_marketing', ...), and run google_marketing_verify_connection."

3. Agent Execution Runbook (Instructions for the AI Agent)

When a user asks you to set up valstorm-plugin-google-marketing, execute this sequence autonomously:

  1. Enable All 5 Google Marketing APIs via gcloud:
  • Run:

gcloud services enable googleads.googleapis.com searchconsole.googleapis.com analyticsdata.googleapis.com analyticsadmin.googleapis.com tagmanager.googleapis.com --project=<PROJECT_ID>

  1. Collect or Provision Credentials:
  • Check if the user has Application Default Credentials (~/.config/gcloud/application_default_credentials.json) or a Service Account key, or create a valstorm-marketing-bot@<PROJECT_ID>.iam.gserviceaccount.com service account.
  • If using a Service Account, output the client_email clearly so the user can add it to GSC (Settings -> Users and permissions), GA4 (Admin -> Property Access Management), and GTM (Admin -> User Management).
  1. Auto-Discover Property & Container IDs:
  • Call gsc_list_sites (GET https://www.googleapis.com/webmasters/v3/sites) to auto-detect gsc_site_url.
  • Call the GA4 Admin API (GET https://analyticsadmin.googleapis.com/v1beta/accountSummaries) to auto-detect the numeric ga4_property_id.
  • Call the GTM API (GET https://tagmanager.googleapis.com/tagmanager/v2/accounts) to auto-detect gtm_account_id and gtm_container_id.
  1. Save Unified Encrypted Credential & Verify:
  • Save the complete dictionary via await platform.credentials.save("google_marketing", {...}).
  • Run google_marketing_verify_connection (and ga4_verify_conversion_events / gsc_list_sites) to confirm all configured services return "ok".

🛠️ End-to-End DIY Setup Guide (Manual / CLI)

Step 1: Enable Required Google Cloud APIs

In the Google Cloud Console, enable:

  1. Google Ads API (googleads.googleapis.com)
  2. Google Search Console API (searchconsole.googleapis.com)
  3. Google Analytics Data API (analyticsdata.googleapis.com)
  4. Google Analytics Admin API (analyticsadmin.googleapis.com)
  5. Tag Manager API (tagmanager.googleapis.com)
bash
gcloud services enable \
  googleads.googleapis.com \
  searchconsole.googleapis.com \
  analyticsdata.googleapis.com \
  analyticsadmin.googleapis.com \
  tagmanager.googleapis.com \
  --project=YOUR_GCP_PROJECT_ID

Step 2: Configure Service Permissions Across the 4 Platforms

A. Google Ads

  1. In your Google Ads Manager Account (MCC), go to Tools and Settings -> Setup -> API Center and copy your Developer Token (developer_token).
  2. Note your 10-digit Manager Account ID (login_customer_id, without hyphens) and Client Account ID (customer_id, without hyphens).
  3. Generate an OAuth 2.0 Client (client_id, client_secret) and refresh_token with the https://www.googleapis.com/auth/adwords scope (or grant your Service Account access via Domain-Wide Delegation).

B. Google Search Console (GSC)

  1. Open Google Search Console.
  2. Select your property (e.g. sc-domain:valstorm.com or https://www.valstorm.com/).
  3. Go to Settings -> Users and permissions -> Add user and add your Service Account client_email (or OAuth user) with Full or Owner permissions.

C. Google Analytics 4 (GA4)

  1. Open Google Analytics -> Admin -> Property Details and copy your numeric Property ID (ga4_property_id, e.g. 123456789).
  2. Go to Property Access Management and add your Service Account client_email (or OAuth user) with the Viewer or Analyst role.

D. Google Tag Manager (GTM)

  1. Open Google Tag Manager -> Admin.
  2. Copy your numeric Account ID (gtm_account_id) and Container ID (gtm_container_id).
  3. Under User Management, grant your Service Account client_email (or OAuth user) Read / Edit container permissions.

2. Saving Unified Encrypted Credentials in Open Valstorm

Store the unified google_marketing credential dictionary in Open Valstorm's encrypted CredentialContext:

python
await platform.credentials.save(
    "google_marketing",
    {
        # Authentication (OAuth 2.0 Refresh Token and/or GCP Service Account JSON)
        "auth_type": "oauth2",  # or "service_account"
        "client_id": "1234567890-abc.apps.googleusercontent.com",
        "client_secret": "GOCSPX-your-client-secret",
        "refresh_token": "1//0g-your-oauth-refresh-token",
        "service_account_json": {
            "type": "service_account",
            "project_id": "your-gcp-project-id",
            "client_email": "[email protected]",
            "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
        },
        # 1. Google Ads
        "developer_token": "YOUR_ADS_DEVELOPER_TOKEN",
        "login_customer_id": "1234567890",
        "customer_id": "0987654321",
        # 2. Google Search Console
        "gsc_site_url": "sc-domain:valstorm.com",
        # 3. Google Analytics 4
        "ga4_property_id": "345678901",
        # 4. Google Tag Manager
        "gtm_account_id": "600123456",
        "gtm_container_id": "12345678",
        "gtm_workspace_id": "1",
    },
)

Verify Connection & Smoke Test All 4 Services

bash
curl -X POST http://localhost:8000/v1/google-marketing/verify \
  -H "Authorization: Bearer <YOUR_JWT_TOKEN>"

Or ask the Valstorm AI Agent to run google_marketing_verify_connection or google_marketing_get_setup_guide.


3. Activating the Plugin

Set VALSTORM_PLUGINS in your .env:

env
VALSTORM_PLUGINS=*
# or explicitly:
VALSTORM_PLUGINS=google-marketing,gcp

4. Python Usage Examples (platform.google_marketing)

python
# 1. Google Ads: Audit wasted ad spend & add negative keywords
campaigns = await platform.google_marketing.ads.list_campaigns()
waste = await platform.google_marketing.ads.audit_search_terms(min_cost_micros=5_000_000)
await platform.google_marketing.ads.add_negative_keywords(
    campaign_id="1122334455",
    keywords=["free", "jobs", "open source tutorial"],
)

# 2. Google Ads: Upload Customer Match audience (SHA-256 normalized automatically)
await platform.google_marketing.ads.upload_customer_match(
    user_list_name="High-Intent Enterprise Leads",
    contacts=[{"email": "[email protected]", "phone": "+15552345678", "first_name": "Jane", "last_name": "Doe"}],
)

# 3. Google Search Console: Query organic search CTR & inspect URL indexing
gsc_rows = await platform.google_marketing.gsc.query_search_analytics(
    start_date="2026-10-01",
    end_date="2026-10-31",
    dimensions=["query", "page"],
)
index_status = await platform.google_marketing.gsc.inspect_url("https://valstorm.com/pricing")

# 4. Google Analytics 4: Run realtime smoke test & verify conversion events
realtime = await platform.google_marketing.ga4.run_realtime_smoketest()
conversions = await platform.google_marketing.ga4.verify_conversion_events(
    event_names=["generate_lead", "sign_up", "purchase"],
)

# 5. Google Tag Manager: Inspect container & list tags
container = await platform.google_marketing.gtm.get_container()
tags = await platform.google_marketing.gtm.list_tags()

5. REST API Endpoints (/v1/google-marketing/*)

MethodEndpointDescription
GET/v1/google-marketing/setup-guideInteractive setup playbook for Ads, GSC, GA4, and GTM
POST/v1/google-marketing/verifyVerifies credentials and connectivity across Ads, GSC, GA4, and GTM
GET/v1/google-marketing/ads/campaignsLists Google Ads campaigns and performance metrics
GET/v1/google-marketing/gsc/sitesLists verified Google Search Console properties
GET/v1/google-marketing/ga4/realtimeRuns a GA4 Realtime active-users smoke test
GET/v1/google-marketing/gtm/containerInspects the configured Google Tag Manager container

6. AI Agent Tools (get_google_marketing_tools())

  • google_marketing_get_setup_guide
  • google_marketing_verify_connection
  • google_ads_list_campaigns
  • google_ads_audit_waste
  • google_ads_add_negative_keywords
  • google_ads_create_search_campaign
  • google_ads_upload_customer_match
  • gsc_list_sites
  • gsc_query_search_analytics
  • gsc_inspect_url
  • gsc_submit_sitemap
  • ga4_get_engagement_metrics
  • ga4_verify_conversion_events
  • gtm_list_tags

7. Running Tests

bash
cd apps/open-api
.venv/bin/pytest tests/test_plugin_google_marketing.py -v