Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.
Shell agent-teamsai-agentai-automationanthropicautonomous-agentawsbusiness-operationsclaudeclaude-codeclaude-code-plugindevopsecommerceinbox-zeroklaviyomarketingmcpmorning-briefingpr-automationshopifyterminal
fix(marketing): GA4 + Search Console read as "no data" instead of working (#939)
Aurora Capitald71248aca6c7
diff --git a/claude-ops/bin/ops-marketing-autopilot b/claude-ops/bin/ops-marketing-autopilotindex 27f9b1eb..29d1d283 100755--- a/claude-ops/bin/ops-marketing-autopilot+++ b/claude-ops/bin/ops-marketing-autopilot@@ -2830,8 +2830,13 @@ run_health_check() {# --- GA4 SA key validity ---local ga4_prop; ga4_prop="$(chan_cred "$proj" ga4 property_id)"if [ -n "$ga4_prop" ]; then- local ga4_tok- ga4_tok="$(gcloud auth application-default print-access-token 2>/dev/null || true)"+ # Probe with the SAME credential the data path uses (ga4_auth_token, which+ # prefers the project's provisioned SA key). Probing raw ADC here reported+ # every project unhealthy while GA4 pulls were in fact working — a false+ # alarm that hid the real per-project failures underneath it.+ local ga4_tok ga4_sa_ref+ ga4_sa_ref="$(resolve_cred "$(jq -r --arg p "$proj" '.marketing.projects[$p].ga4.sa_key_file_ref // empty' "$PREFS" 2>/dev/null)")"+ ga4_tok="$(GA4_SERVICE_ACCOUNT_KEY_FILE="$ga4_sa_ref" ga4_auth_token 2>/dev/null || true)"if [ -n "$ga4_tok" ]; thenlocal ga4_respga4_resp="$(curl -gsS --max-time 10 -X POST \@@ -2856,8 +2861,9 @@ run_health_check() {# --- GSC site authorization ---local gsc_site; gsc_site="$(chan_cred "$proj" gsc site_url)"if [ -n "$gsc_site" ]; then- local gsc_tok- gsc_tok="$(gcloud auth application-default print-access-token 2>/dev/null || true)"+ local gsc_tok gsc_sa_ref+ gsc_sa_ref="$(resolve_cred "$(jq -r --arg p "$proj" '.marketing.projects[$p].ga4.sa_key_file_ref // empty' "$PREFS" 2>/dev/null)")"+ gsc_tok="$(GA4_SERVICE_ACCOUNT_KEY_FILE="$gsc_sa_ref" ga4_auth_token 2>/dev/null || true)"if [ -n "$gsc_tok" ]; thenlocal gsc_respgsc_resp="$(curl -gsS --max-time 10 \diff --git a/claude-ops/bin/ops-marketing-dash b/claude-ops/bin/ops-marketing-dashindex 7fabb383..04d5c03e 100755--- a/claude-ops/bin/ops-marketing-dash+++ b/claude-ops/bin/ops-marketing-dash@@ -136,7 +136,14 @@ fi# channels.marketing.ga4.sa_json; export it so ga4_auth_token (ga4-data-api.sh)# picks it up. Expand a leading ~ to $HOME.if [ -z "${GA4_SERVICE_ACCOUNT_KEY_FILE:-}" ] && [ -f "$PREFS" ]; then- _ga4_sa="$(jq -r '.channels.marketing.ga4.sa_json // empty' "$PREFS" 2>/dev/null)"+ # Project tier first: marketing.projects.<key>.ga4.sa_key_file_ref is what+ # ops-marketing-provision actually writes (and what the autopilot reads).+ # The owner-level channels.marketing.ga4.sa_json below is the legacy name and+ # is absent on provisioned installs — reading only that made every GA4 pull+ # fall through to gcloud ADC, and return null the moment ADC expired.+ _ga4_sa="$(get_cred 'ga4.sa_key_file_ref' GA4_SERVICE_ACCOUNT_KEY_FILE ga4_sa_key_file 2>/dev/null || true)"+ [ -z "$_ga4_sa" ] && _ga4_sa="$(jq -r '.channels.marketing.ga4.sa_json // empty' "$PREFS" 2>/dev/null)"+ _ga4_sa="${_ga4_sa#file:}"[ -n "$_ga4_sa" ] && GA4_SERVICE_ACCOUNT_KEY_FILE="${_ga4_sa/#\~/$HOME}"export GA4_SERVICE_ACCOUNT_KEY_FILEfi@@ -383,7 +390,10 @@ gather_gsc() {returnfi- GSC_TOKEN=$(gcloud auth application-default print-access-token 2>/dev/null || echo "")+ # ga4_auth_token, not raw ADC: the service-account key is the credential that+ # is actually provisioned and it now carries webmasters.readonly. ADC remains+ # the last-resort fallback inside that helper.+ GSC_TOKEN=$(ga4_auth_token 2>/dev/null || echo "")if [ -z "$GSC_TOKEN" ]; thenecho "null"returndiff --git a/claude-ops/scripts/lib/ga4-data-api.sh b/claude-ops/scripts/lib/ga4-data-api.shindex f1af1e9a..12949dae 100644--- a/claude-ops/scripts/lib/ga4-data-api.sh+++ b/claude-ops/scripts/lib/ga4-data-api.sh@@ -14,6 +14,18 @@[ -n "${_GA4_DATA_API_LOADED:-}" ] && return 0_GA4_DATA_API_LOADED=1+# Scopes minted onto the service-account JWT.+#+# webmasters.readonly is NOT optional padding: callers share this one token+# between the GA4 Data API and the Search Console API (see the GSC paths in+# ops-marketing-dash and ops-marketing-autopilot). A token minted with only+# analytics.readonly authenticates fine and then fails every GSC call with+# HTTP 403 "Request had insufficient authentication scopes" — which the+# callers degrade to a null envelope, so SEO data silently reads as absent+# rather than broken. Requesting an unGRANTED scope is harmless; omitting a+# needed one is not.+GA4_SA_SCOPES="${GA4_SA_SCOPES:-https://www.googleapis.com/auth/analytics.readonly https://www.googleapis.com/auth/webmasters.readonly}"+# ga4_auth_token [sa_key_file]# Resolution order:# 1) explicit arg@@ -30,8 +42,8 @@ ga4_auth_token() {now=$(date +%s)exp=$((now + 3600))header=$(printf '{"alg":"RS256","typ":"JWT"}' | base64 | tr -d '=' | tr '+/' '-_' | tr -d '\n')- payload=$(printf '{"iss":"%s","scope":"https://www.googleapis.com/auth/analytics.readonly","aud":"https://oauth2.googleapis.com/token","exp":%d,"iat":%d}' \- "$(jq -r '.client_email' "$sa_key_file")" "$exp" "$now" \+ payload=$(printf '{"iss":"%s","scope":"%s","aud":"https://oauth2.googleapis.com/token","exp":%d,"iat":%d}' \+ "$(jq -r '.client_email' "$sa_key_file")" "$GA4_SA_SCOPES" "$exp" "$now" \| base64 | tr -d '=' | tr '+/' '-_' | tr -d '\n')sig=$(printf '%s.%s' "$header" "$payload" \| openssl dgst -sha256 -sign <(jq -r '.private_key' "$sa_key_file") 2>/dev/null \diff --git a/claude-ops/scripts/lib/ga4-resolve.sh b/claude-ops/scripts/lib/ga4-resolve.shindex 7cfb1c7c..7e4617e4 100755--- a/claude-ops/scripts/lib/ga4-resolve.sh+++ b/claude-ops/scripts/lib/ga4-resolve.sh@@ -36,6 +36,14 @@ resolve_cred() {local var="${ref#env:}"printf '%s' "${!var:-}";;+ file:*)+ # file:/abs/path — a path-valued ref, used by ga4.sa_key_file_ref. Without+ # this case the "file:" prefix survived into GA4_SERVICE_ACCOUNT_KEY_FILE,+ # so the key file never existed, every SA mint was skipped, and callers+ # fell through to gcloud ADC. Expand a leading ~ as well.+ local _p="${ref#file:}"+ printf '%s' "${_p/#\~/$HOME}"+ ;;doppler:*)local path="${ref#doppler:}"local proj="${path%%/*}"diff --git a/claude-ops/tests/test-resolve-cred-strict.sh b/claude-ops/tests/test-resolve-cred-strict.shindex 93cd6744..75097acb 100755--- a/claude-ops/tests/test-resolve-cred-strict.sh+++ b/claude-ops/tests/test-resolve-cred-strict.sh@@ -116,6 +116,43 @@ fiexport PATH="$_orig_path"+# ── Case 8: file: ref strips the prefix ──────────────────────────────────────+# Regression: resolve_cred had no file:* case, so a ga4.sa_key_file_ref of+# "file:/path/key.json" resolved to that literal string. The prefix survived+# into GA4_SERVICE_ACCOUNT_KEY_FILE, [ -f "$f" ] was false, the SA mint was+# skipped, and every GA4/GSC caller fell through to gcloud ADC — reading as+# "no data" the moment ADC expired.+val=""; rc=0+val="$(_strict "file:/tmp/some-key.json" 2>/dev/null)" || rc=$?+if [ "$rc" = "0" ] && [ "$val" = "/tmp/some-key.json" ]; then+ ok "file: ref → rc=0, prefix stripped"+else+ err "file: ref → rc=0, prefix stripped" 0 "$rc" "$val"+fi++# ── Case 9: file: ref expands a leading ~ ────────────────────────────────────+val=""; rc=0+val="$(_strict "file:~/k.json" 2>/dev/null)" || rc=$?+if [ "$rc" = "0" ] && [ "$val" = "$HOME/k.json" ]; then+ ok "file: ref → leading ~ expanded"+else+ err "file: ref → leading ~ expanded" 0 "$rc" "$val"+fi++# ── Case 10: the minted SA scope must cover Search Console ───────────────────+# GSC callers share ga4_auth_token's token. analytics.readonly alone yields+# HTTP 403 insufficient scopes on every GSC request.+_api_lib="$(cd "$(dirname "$0")/.." && pwd)/scripts/lib/ga4-data-api.sh"+unset _GA4_DATA_API_LOADED+# shellcheck disable=SC1090+. "$_api_lib"+case "${GA4_SA_SCOPES:-}" in+ *analytics.readonly*webmasters.readonly*|*webmasters.readonly*analytics.readonly*)+ ok "GA4_SA_SCOPES covers analytics + webmasters" ;;+ *)+ err "GA4_SA_SCOPES covers analytics + webmasters" "both scopes" "${GA4_SA_SCOPES:-<unset>}" ;;+esac+echo ""echo "Results: ${pass} passed, ${fail} failed"[ "$fail" -eq 0 ]