# `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 ``. 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 ``), 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=` 2. **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@.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**). 3. **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`. 4. **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](https://console.cloud.google.com/), 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](https://search.google.com/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](https://analytics.google.com/) -> **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](https://tagmanager.google.com/) -> **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": "marketing-bot@your-gcp-project-id.iam.gserviceaccount.com", "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 " ``` 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": "ceo@acme.com", "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/*`) | Method | Endpoint | Description | | :--- | :--- | :--- | | `GET` | `/v1/google-marketing/setup-guide` | Interactive setup playbook for Ads, GSC, GA4, and GTM | | `POST` | `/v1/google-marketing/verify` | Verifies credentials and connectivity across Ads, GSC, GA4, and GTM | | `GET` | `/v1/google-marketing/ads/campaigns` | Lists Google Ads campaigns and performance metrics | | `GET` | `/v1/google-marketing/gsc/sites` | Lists verified Google Search Console properties | | `GET` | `/v1/google-marketing/ga4/realtime` | Runs a GA4 Realtime active-users smoke test | | `GET` | `/v1/google-marketing/gtm/container` | Inspects 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 ```