Segments: list

Requires authorization

Lists segments to which the user has access. Try it now or see an example.

In addition to the standard parameters, this method supports the parameters listed in the parameters table.

Request

HTTP request

GET https://www.googleapis.com/analytics/v3/management/segments

Parameters

Parameter name Value Description
Optional query parameters
max-results integer The maximum number of segments to include in this response.
start-index integer An index of the first segment to retrieve. Use this parameter as a pagination mechanism along with the max-results parameter.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/analytics
https://www.googleapis.com/auth/analytics.edit
https://www.googleapis.com/auth/analytics.readonly

Request body

Do not supply a request body with this method.

Response

The response contains one Segment resource for each advanced segment available to the user.

{
  "kind": "analytics#segments",
  "username": string,
  "totalResults": integer,
  "startIndex": integer,
  "itemsPerPage": integer,
  "previousLink": string,
  "nextLink": string,
  "items": [
    management.segments Resource
  ]
}
Property name Value Description Notes
kind string Collection type for segments.
username string Email ID of the authenticated user
totalResults integer The total number of results for the query, regardless of the number of results in the response.
startIndex integer The starting index of the resources, which is 1 by default or otherwise specified by the start-index query parameter.
itemsPerPage integer The maximum number of resources the response can contain, regardless of the actual number of resources returned. Its value ranges from 1 to 1000 with a value of 1000 by default, or otherwise specified by the max-results query parameter.
items[] list A list of segments.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

PHP

Uses the PHP client library.

/**
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Segments Developer Guide for details.
 */

/**
 * Example #1:
 * Requests a list of all Segments for the authorized user.
 */
try {
  $segments = $analytics->management_segments->listManagementSegments();
} catch (apiServiceException $e) {
  print 'There was an Analytics API service error '
      . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error '
      . $e->getCode() . ':' . $e->getMessage();
}


/*
 * Example #2:
 * The results of the list method are stored in the segments object.
 * The following code shows how to iterate through them.
 */
foreach ($segments->getItems() as $segment) {
  $html .= <<<HTML
<pre>

Segment ID = {$segment->getId()}
Kind       = {$segment->getKind()}
Self Link  = {$segment->getSelfLink()}
Name       = {$segment->getName()}
Definition = {$segment->getDefinition()}
Created    = {$segment->getCreated()}
Updated    = {$segment->getUpdated()}

</pre>
HTML;
  print $html;
}



Python

Uses the Python client library.

# Note: This code assumes you have an authorized Analytics service object.
# See the Segments Developer Guide for details.

# Example #1:
# Requests a list of segments to which the user has access.
try:
  segments = analytics.management().segments().list().execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

# Example #2:
# The results of the list method are stored in the segments object.
# The following code shows how to iterate through them.
for segment in segments.get('items', []):
  print 'Segment Id         = %s' % segment.get('id')
  print 'Segment kind       = %s' % segment.get('kind')
  print 'Segment segmentId  = %s' % segment.get('segmentId')
  print 'Segment Name       = %s' % segment.get('name')
  print 'Segment Definition = %s' % segment.get('definition')
  if segment.get('created'):
    print 'Created    = %s' % segment.get('created')
    print 'Updated    = %s' % segment.get('updated')
  print

JavaScript

Uses the JavaScript client library.

/*
 * Note: This code assumes you have an authorized Analytics client object.
 * See the Segment Developer Guide for details.
 */

/*
 * Example 1:
 * Requests a list of all Segments for the authorized user.
 */
function listSegments() {
  var request = gapi.client.analytics.management.segments.list();
  request.execute(printSegments);
}

/*
 * Example 2:
 * The results of the list method are passed as the results object.
 * The following code shows how to iterate through them.
 */
function printSegments(results) {
  if (results && !results.error) {
    var segments = results.items;
    for (var i = 0, segment; segment = segments[i]; i++) {
      console.log('Segment Id: ' + segment.id);
      console.log('Segment Kind: ' + segment.kind);
      console.log('Segment Name: ' + segment.name);
      console.log('Segment Definition: ' + segment.definition);

      // These fields are only set for custom segments and not default segments.
      if (segment.created) {
        console.log('Created: ' + segment.created);
        console.log('Updated: ' + segment.updated);
      }
    }
  }
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response. Alternatively, try the standalone Explorer.