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.
In this repository13 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ga4-audiencesSkill
- ga4-auditSkill
- ga4-contextSkill
- ga4-custom-defsSkill
- ga4-custom-reportSkill
- ga4-events-editSkill
- ga4-eventsSkill
- ga4-funnelSkill
- ga4-key-eventsSkill
- ga4-segmentsSkill
- ga4Skill
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
- Very specific: line 23
No API call is made by this skill.
- Distinctive: line 58
Run a saved custom report with a segment applied:
≈ “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.
Files
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
| Intent | Command |
| Save shorthand | python scripts/ga4_definitions.py --save-segment "branded-traffic" --field sessionSource --op CONTAINS --value brand |
| Save IN-list | python scripts/ga4_definitions.py --save-segment "us-and-canada" --field country --op IN_LIST --values "United States,Canada" |
| Save raw expression | python scripts/ga4_definitions.py --save-segment-json "complex" path/to/filter.json |
| List | python scripts/ga4_definitions.py --list-segments |
| Delete | python 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.htmlPII 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.
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
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.

