agentleFS
arcbaslowarcbaslow/google-analytics-agent/skills/ga4-segment-defs/SKILL.md

ga4-segment-defs

Skill0 starsChanged 6 months agoMITView on GitHub

Saved GA4 segment definitions stored as local JSON. GA4 has no stored Segment resource - segments only exist inside Explorations. This skill saves filter expressions to ~/.claude/ga4-definitions/segments/ and applies them as dimensionFilter on Data API runReport calls. Use for reusable cohorts on the public API. For stored cohorts that work in GA4 reports and Google Ads, use ga4-audiences. For funnel drop-off cohort analysis, use ga4-segments.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    No API call is made by this skill.

    line 23
  2. Distinctive:

    Run a saved custom report with a segment applied:

    line 58

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

GA4: Saved Segment Definitions

GA4 has no public Segment API. We persist filter expressions locally and inject them into runReport calls - same effect for analytics, but they do not propagate to the GA4 UI or to Google Ads. Users who need cohorts visible inside GA4 should use the ga4-audiences skill instead. For funnel drop-off cohort analysis (read-only), use ga4-segments.

Storage: ~/.claude/ga4-definitions/segments/<slug>.json

No API call is made by this skill; auth is only needed when a segment is applied to a runReport via ga4-custom-report.

Commands

IntentCommand
Save shorthandpython scripts/ga4_definitions.py --save-segment "branded-traffic" --field sessionSource --op CONTAINS --value brand
Save IN-listpython scripts/ga4_definitions.py --save-segment "us-and-canada" --field country --op IN_LIST --values "United States,Canada"
Save raw expressionpython scripts/ga4_definitions.py --save-segment-json "complex" path/to/filter.json
Listpython scripts/ga4_definitions.py --list-segments
Deletepython scripts/ga4_definitions.py --delete-segment "branded-traffic"

Shorthand operators

EXACT, CONTAINS, BEGINS_WITH, IN_LIST. For NOT, AND, OR groups, save as raw JSON.

Raw expression shape

A Data API FilterExpression dict, e.g.

{
  "and_group": {
    "expressions": [
      {"filter": {"field_name": "deviceCategory", "string_filter": {"value": "mobile", "match_type": "EXACT"}}},
      {"filter": {"field_name": "sessionSource", "string_filter": {"value": "brand", "match_type": "CONTAINS"}}}
    ]
  }
}

Applying a segment

Run a saved custom report with a segment applied:

python scripts/ga4_definitions.py --run-report channel-by-week --property 123 --segment "branded-traffic" --format html --output channel-branded.html

PII scrubbing also applies to Admin responses, cached data, context, MCP and rendered reports. Sensitive camelCase/custom-dimension keys and encoded email values are redacted; numeric metrics and GA resource names are retained. Scrubbing is a defensive filter, not a guarantee that arbitrary free text is anonymous. Saved-segment reports use one normal cached, scrubbed Data API call.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.