Segmentation
Filter API results to specific subsets of your visitors.
Overview
Segmentation allows you to filter any API report to a subset of your audience. For example, you can request page views only for visitors from a specific country, or conversions only from mobile devices.
Add the segment parameter to any API request to filter results.
Basic Syntax
The segment parameter uses this format:
&segment=dimension==valueExample — visitors from the United States:
&segment=countryCode==USOperators
| Operator | Meaning | Example |
|---|---|---|
== | Equals | countryCode==US |
!= | Not equals | countryCode!=US |
<= | Less than or equal | actions<=5 |
< | Less than | actions<5 |
>= | Greater than or equal | visitDuration>=600 |
> | Greater than | visitDuration>600 |
=@ | Contains | pageUrl=@/blog/ |
!@ | Does not contain | pageUrl!@/admin/ |
=^ | Starts with | pageUrl=^https://example.com/services |
=$ | Ends with | pageUrl=$/contact |
Combining Segments
AND Logic
Use semicolon (;) to combine conditions with AND:
&segment=countryCode==US;browserCode==FFThis returns visitors from the US AND using Firefox.
OR Logic
Use comma (,) to combine conditions with OR:
&segment=countryCode==US,countryCode==CAThis returns visitors from the US OR Canada.
Combined AND/OR
OR takes precedence over AND:
&segment=browserCode==FF;countryCode==US,countryCode==CAThis returns: Firefox users AND (US OR Canada)
To be explicit: (browserCode==FF) AND (countryCode==US OR countryCode==CA)
URL Encoding
Segment values must be URL encoded. Special characters need encoding:
| Character | Encoded |
|---|---|
| Space | %20 |
/ | %2F |
= | %3D |
& | %26 |
, | %2C |
; | %3B |
Example — page URL containing “/blog/my post”:
&segment=pageUrl=@%2Fblog%2Fmy%20postEmpty/Not Empty Values
Value is empty (is null)
Leave the value blank after ==:
&segment=referrerKeyword==Returns visitors with no referrer keyword.
Value is not empty (is not null)
Leave the value blank after !=:
&segment=city!=Returns visitors where city is set.
Common Segments
Visitor Attributes
| Segment | Description | Example |
|---|---|---|
countryCode | Country (ISO code) | countryCode==US |
regionCode | Region/state | regionCode==CA |
city | City name | city==New%20York |
browserCode | Browser | browserCode==FF |
operatingSystemCode | OS | operatingSystemCode==WIN |
deviceType | Device type | deviceType==smartphone |
resolution | Screen resolution | resolution==1920x1080 |
browserLang | Browser language | browserLang=@en |
Visit Attributes
| Segment | Description | Example |
|---|---|---|
visitDuration | Visit length (seconds) | visitDuration>=300 |
actions | Actions per visit | actions>=5 |
visitorType | New or returning | visitorType==returning |
daysSinceFirstVisit | Days since first visit | daysSinceFirstVisit>30 |
daysSinceLastVisit | Days since last visit | daysSinceLastVisit<=7 |
visitCount | Number of visits | visitCount>=3 |
Traffic Sources
| Segment | Description | Example |
|---|---|---|
referrerType | Referrer type | referrerType==search |
referrerName | Referrer name | referrerName==Google |
referrerKeyword | Search keyword | referrerKeyword=@matomo |
referrerUrl | Referrer URL | referrerUrl=@facebook.com |
Referrer types: direct, search, website, campaign, social
Campaigns (UTM)
| Segment | Description | Example |
|---|---|---|
campaignName | utm_campaign | campaignName==spring_sale |
campaignSource | utm_source | campaignSource==google |
campaignMedium | utm_medium | campaignMedium==cpc |
campaignContent | utm_content | campaignContent==banner_a |
campaignKeyword | utm_term | campaignKeyword==analytics |
Page/Action
| Segment | Description | Example |
|---|---|---|
pageUrl | Page URL | pageUrl=@/services/ |
pageTitle | Page title | pageTitle=@Contact |
entryPageUrl | Entry page URL | entryPageUrl==https://example.com/ |
exitPageUrl | Exit page URL | exitPageUrl=@/thank-you |
siteSearchKeyword | Site search query | siteSearchKeyword=@pricing |
downloadUrl | Downloaded file | downloadUrl=@.pdf |
outlinkUrl | Outbound link clicked | outlinkUrl=@partner.com |
Events
| Segment | Description | Example |
|---|---|---|
eventCategory | Event category | eventCategory==Forms |
eventAction | Event action | eventAction==Submit |
eventName | Event name | eventName==Contact%20Form |
eventValue | Event value | eventValue>=100 |
Goals
| Segment | Description | Example |
|---|---|---|
visitConvertedGoalId | Converted goal ID | visitConvertedGoalId==1 |
goalConversionPageUrl | Page where goal converted | goalConversionPageUrl=@/thank-you |
Complete Examples
Mobile visitors from US with 3+ page views
&segment=deviceType==smartphone;countryCode==US;actions>=3Returning visitors from Google organic search
&segment=visitorType==returning;referrerType==search;referrerName==GoogleVisitors who viewed blog but didn’t convert
&segment=pageUrl=@/blog/;visitConvertedGoalId==Campaign visitors on desktop with long visits
&segment=referrerType==campaign;deviceType==desktop;visitDuration>=300Visitors from email campaign who completed a form
&segment=campaignMedium==email;eventCategory==Forms;eventAction==SubmitFull API Request Example
Get page views for mobile visitors from the US:
https://example.ghostmetrics.cloud/?module=API
&method=Actions.getPageUrls
&idSite=1
&period=month
&date=today
&format=JSON
&segment=deviceType==smartphone;countryCode==US
&token_auth=YOUR_TOKENGetting Available Segments
To get a list of all available segments for your site:
https://example.ghostmetrics.cloud/?module=API
&method=API.getSegmentsMetadata
&idSites=1
&format=JSON
&token_auth=YOUR_TOKENThis returns all segment names, accepted values, and documentation.
Performance Considerations
- Segments add processing time to API requests
- Complex segments with many conditions take longer
- Using segments on large date ranges may be slow
- Pre-defined segments (saved in Ghost Metrics) may be cached for better performance
Troubleshooting
No data returned
- Verify segment syntax is correct
- Check that segment values are URL encoded
- Confirm the dimension has data (e.g., campaign segments need campaign traffic)
- Try a simpler segment first
Unexpected results
- Check operator precedence (OR before AND)
- Verify case sensitivity (some segments are case-sensitive)
- Ensure values match exactly (use
=@for partial matches)
Segment not recognized
- Check spelling of segment name
- Use
API.getSegmentsMetadatato get valid segment names - Some segments require specific plugins to be enabled
Next Steps
- Common Methods — API methods to use with segments
- Code Examples — Segmentation in code
- Parameters Reference — Other filtering options