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
# 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-marketingfor my GCP project<YOUR_GCP_PROJECT_ID>. Followplugins/valstorm-plugin-google-marketing/README.mdto enable the Ads, GSC, GA4, and GTM APIs viagcloud, 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 intoplatform.credentials.save('google_marketing', ...), and rungoogle_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:
- 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>
- 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 avalstorm-marketing-bot@<PROJECT_ID>.iam.gserviceaccount.comservice account. - If using a Service Account, output the
client_emailclearly so the user can add it to GSC (Settings -> Users and permissions), GA4 (Admin -> Property Access Management), and GTM (Admin -> User Management).
- Auto-Discover Property & Container IDs:
- Call
gsc_list_sites(GET https://www.googleapis.com/webmasters/v3/sites) to auto-detectgsc_site_url. - Call the GA4 Admin API (
GET https://analyticsadmin.googleapis.com/v1beta/accountSummaries) to auto-detect the numericga4_property_id. - Call the GTM API (
GET https://tagmanager.googleapis.com/tagmanager/v2/accounts) to auto-detectgtm_account_idandgtm_container_id.
- Save Unified Encrypted Credential & Verify:
- Save the complete dictionary via
await platform.credentials.save("google_marketing", {...}). - Run
google_marketing_verify_connection(andga4_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:
- Google Ads API (
googleads.googleapis.com) - Google Search Console API (
searchconsole.googleapis.com) - Google Analytics Data API (
analyticsdata.googleapis.com) - Google Analytics Admin API (
analyticsadmin.googleapis.com) - Tag Manager API (
tagmanager.googleapis.com)
gcloud services enable \
googleads.googleapis.com \
searchconsole.googleapis.com \
analyticsdata.googleapis.com \
analyticsadmin.googleapis.com \
tagmanager.googleapis.com \
--project=YOUR_GCP_PROJECT_IDStep 2: Configure Service Permissions Across the 4 Platforms
A. Google Ads
- In your Google Ads Manager Account (MCC), go to Tools and Settings -> Setup -> API Center and copy your Developer Token (
developer_token). - Note your 10-digit Manager Account ID (
login_customer_id, without hyphens) and Client Account ID (customer_id, without hyphens). - Generate an OAuth 2.0 Client (
client_id,client_secret) andrefresh_tokenwith thehttps://www.googleapis.com/auth/adwordsscope (or grant your Service Account access via Domain-Wide Delegation).
B. Google Search Console (GSC)
- Open Google Search Console.
- Select your property (e.g.
sc-domain:valstorm.comorhttps://www.valstorm.com/). - 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)
- Open Google Analytics -> Admin -> Property Details and copy your numeric Property ID (
ga4_property_id, e.g.123456789). - 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)
- Open Google Tag Manager -> Admin.
- Copy your numeric Account ID (
gtm_account_id) and Container ID (gtm_container_id). - 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:
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
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:
VALSTORM_PLUGINS=*
# or explicitly:
VALSTORM_PLUGINS=google-marketing,gcp4. Python Usage Examples (platform.google_marketing)
# 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/*)
| 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_guidegoogle_marketing_verify_connectiongoogle_ads_list_campaignsgoogle_ads_audit_wastegoogle_ads_add_negative_keywordsgoogle_ads_create_search_campaigngoogle_ads_upload_customer_matchgsc_list_sitesgsc_query_search_analyticsgsc_inspect_urlgsc_submit_sitemapga4_get_engagement_metricsga4_verify_conversion_eventsgtm_list_tags
7. Running Tests
cd apps/open-api
.venv/bin/pytest tests/test_plugin_google_marketing.py -v