Search code examples
google-analyticsgoogle-analytics-api

What is considered a segment in google analytics API?


I am querying the google analytics API and get an error response saying:

Selected dimensions and metrics cannot be queried together.

Using the dimensions-metrics-explorer I can find which dimensions and metrics can be queried together. https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

The dimension and metric I want to combine should be possible according to the explorer (CM Impressions and CM Campaign (CM Model))

However, if I tick the box Only show fields that are allowed in segments both those options disappear. So I assume that I am using segments in my request but I do not understand if I truly do or what a segment is.

These are the parts of my query:

auth: <auth>,
'max-results': 10000,
quotaUser: <quotaUserId>,
metrics: [ 'ga:dcmImpressions' ],
dimensions: [ 'ga:dcmLastEventCampaign', 'ga:date' ],
'start-date': <rangeStart>,
'end-date': <rangeEnd>,
id: [ <analyticsViewIds> ],

Am I using segments and if so, what is the segment?


Solution

  • Am I using segments?

    No, you're making a regular query

    what is the segment?

    Segments are a special data querying feature:

    • More advanced querying: regular queries work on a per-session/hit level just inspecting actual rows of data and returning what matches. With segments you can go further:
      • User Scope include data for users who meet certain conditions (regular queries are all session/hit based), thus allowing you to filter data on criteria which span across sessions
      • Session Sequencing include/exclude data based on a series of action users took during their session (first this, then that, then immediately that...)
      • Complement cohort by using dynamic segments and the not option, allowing you to compare to mutually exclusive segments of user (eg compare users who performed a goal vs users who didn't to understand what gets people to convert)
    • Available in both UI and API
    • Predefined or dynamic: you either create your segment in the UI and refer to it from the API via its segmentId or create a dynamic segment via the API directly
    • Sampling might apply so make sure you check for samplingSpaceSizes to see sampling rate (if any)

    Deciding whether to use segments per se is pretty simply:

    • if you can query what you want without segments don't use them
    • if you need them to query what you want, use them

    The challenging part is knowing what you need to query:

    • Business side: being able to formulate a hypothesis that makes sense
    • GA side: being able to translate that into a query, which implies an understanding of the scopes