Google Analytics

Goals: list

Requires authorization

Lists goals 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/accounts/accountId/webproperties/webPropertyId/profiles/profileId/goals

Parameters

Parameter name Value Description
Required parameters
accountId string Account ID to retrieve goals for. Can either be a specific account ID or '~all', which refers to all the accounts that user has access to.
profileId string View (Profile) ID to retrieve goals for. Can either be a specific view (profile) ID or '~all', which refers to all the views (profiles) that user has access to.
webPropertyId string Web property ID to retrieve goals for. Can either be a specific web property ID or '~all', which refers to all the web properties that user has access to.
Optional parameters
max-results integer The maximum number of goals to include in this response.
start-index integer An index of the first goal 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 is a collection that contains one Goal resource for each requested goal.

{
  "kind": "analytics#goals",
  "username": string,
  "totalResults": integer,
  "startIndex": integer,
  "itemsPerPage": integer,
  "previousLink": string,
  "nextLink": string,
  "items": [
    management.goals Resource
  ]
}
Property name Value Description Notes
kind string Collection type. Value is "analytics#goals".
username string Email ID of the authenticated user
totalResults integer The total number of results for the query, regardless of the number of resources in the result.
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 goals.

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).

Java

Uses the Java client library

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

/**
 * Example #1:
 * Requests goals for a single view (profile) with account ID 12345,
 * web property ID UA-12345-1, and profile ID 523.
 */
try {
  Goals goals = analytics.management.goals.list("12345", "UA-12345-1", "523").execute();

} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}


/**
 * Example #2:
 * Retrieves all goals for all views (profiles) for the user, using a
 * wildcard ~all as the profile ID.
 */
Goals goals = analytics.management.goals.list("12345", "UA-12345-1", "~all").execute();


/**
 * Example #3:
 * The results of the list method are stored in the Goals object. The following
 * code shows how to iterate through them.
 */
for (Goal goal : goals.getItems()) {
  System.out.println("Account ID: " + goal.getAccountId());
  System.out.println("Web Property ID: " + goal.getWebPropertyId());
  System.out.println("Web Property Internal Id: " + goal.getInternalWebPropertyId());
  System.out.println("View (Profile) ID: " + goal.getId());

  System.out.println("Goal Number: " + goal.getId());
  System.out.println("Goal Name: " + goal.getName());
  System.out.println("Goal Value: " + goal.getValue());
  System.out.println("Is Goal Active: " + goal.getActive());
  System.out.println("Goal Type: " + goal.getType());

  System.out.println("Goal Created: " + goal.getCreated());
  System.out.println("Goal Updated: " + goal.getUpdated());

  if (goal.getUrlDestinationDetails() != null) {
    printGoalUrlDestinationDetails(goal.getUrlDestinationDetails());

  } else if (goal.getVisitTimeOnSiteDetails() != null) {
    printGoalVisitTimeOnSiteDetails(goal.getVisitTimeOnSiteDetails());

  } else if (goal.getVisitNumPagesDetails() != null) {
    printGoalVisitNumPagesDetails(goal.getVisitNumPagesDetails());

  } else if (goal.getEventDetails() != null) {
    printGoalEventDetails(goal.getEventDetails());
  }
}

/**
 * Example #4:
 * How to iterate through URL_DESTINATION goals. 
 */
private static void printUrlDestinationDetails(UrlDestinationDetails destinationDetails) {
  System.out.println("Goal Url: " + destinationDetails.getUrl());
  System.out.println("Case Sensitive: " +  destinationDetails.getCaseSensitive());
  System.out.println("Match Type: " +  destinationDetails.getMatchType());
  System.out.println("First Step Required: " +  destinationDetails.getFirstStepRequired());

  if (destinationDetails.getSteps() != null) {
    System.out.println("Goal Steps: ");
    for (Steps step : destinationDetails.getSteps()) {
      System.out.println("Step Number: " + step.getNumber());
      System.out.println("Name: " + step.getName());
      System.out.println("URL: " + step.getUrl());
    }

  } else {
    System.out.println("No Steps Configured");
  }
}


/**
 * Example #5:
 * How to access a VISIT_TIME_ON_SITE goal. 
 */
private static void printVisitTimeOnSiteDetails(
    VisitTimeOnSiteDetails visitTimeOnSiteDetails) {

  System.out.println("Goal Type:  VISIT_TIME_ON_SITE");
  System.out.println("VISIT_TIME_ON_SITE - Comparison Type: "
      + visitTimeOnSiteDetails.getComparisonType());
  System.out.println("VISIT_TIME_ON_SITE - Comparison Value: "
      + visitTimeOnSiteDetails.getComparisonValue());
}


/**
 * Example #6:
 * How to access a VISIT_NUM_PAGES goal. 
 */
private static void printVisitNumPagesDetails(VisitNumPagesDetails visitNumPagesDetails) {
  System.out.println("Goal Type:  VISIT_NUM_PAGES");
  System.out.println("VISIT_NUM_PAGES - Comparison Type: "
      +  visitNumPagesDetails.getComparisonType());
  System.out.println("VISIT_NUM_PAGES - Comparison Value: "
      +  visitNumPagesDetails.getComparisonValue());
}


/**
 * Example #7:
 * How to iterate through EVENT goals. 
 */
private static void printGoalEventDetails(EventDetails eventDetails) {
  System.out.println("EVENT - Use Event Value: " +  eventDetails.getUseEventValue());

  if (eventDetails.getEventConditions() != null) {
    System.out.println("Goal Conditions: ");
    for (EventConditions conditions : eventDetails.getEventConditions()) {
      System.out.println("Type: " + conditions.getType());

      if (conditions.getType().equals("VALUE")) {
        System.out.println("Comparison Type: " + conditions.getComparisonType());
        System.out.println("Comparison Value: " + conditions.getComparisonValue());
      } else {
        System.out.println("matchType: " + conditions.getMatchType());
        System.out.println("expression: " + conditions.getExpression());
      }
    }
  }
}

PHP

Uses the PHP client library

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

/**
 * Example #1:
 * Requests goals for a single view (profile) with account ID 12345,
 * web property ID UA-12345-1, and profile ID 523.
 */
try {
  $goals = $analytics->management_goals
                     ->listManagementGoals('12345',
                                           'UA-12345-1',
                                           '523');

} 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:
 * Retrieves all goals for all views (profiles) for the user, using a
 * wildcard ~all as the profile ID.
 */
$goals = $analytics->management_profiles
                   ->listManagementGoals('12345', 'UA-12345-1', '~all');


/**
 * Example #3:
 * The results of the list method are stored in the Goals object. The following
 * code shows how to iterate through them.
 */
$html = '';
$items = $goals->getItems();

if (count($items) != 0) {
  foreach ($items as &$goal) {
    $html .= <<<HTML
<pre>

Account ID               = {$goal->getAccountId()}
Web Property ID          = {$goal->getWebPropertyId()}
Internal Web Property ID = {$goal->getInternalWebPropertyId()}
View (Profile) ID        = {$goal->getProfileId()}


Goal Number = {$goal->getId()}
Goal Name   = {$goal->getName()}
Goal Value  = {$goal->getValue()}
Goal Active = {$goal->getActive()}
Goal Type   = {$goal->getType()}

Created = {$goal->getCreated()}
Updated = {$goal->getUpdated()}

</pre>
HTML;

    // Now get the HTML for the type of goal.
    switch($goal->getType()) {
      case 'URL_DESTINATION':
        $html .= $this->getUrlDestinationDetailsHtml(
            $goal->getUrlDestinationDetails());
        break;

      case 'VISIT_TIME_ON_SITE':
        $html .= $this->getVisitTimeOnSiteDetailsHtml(
            $goal->getVisitTimeOnSiteDetails());
        break;

      case 'VISIT_NUM_PAGES':
        $html .= $this->getVisitNumPagesDetailsHtml(
            $goal->getVisitNumPagesDetails());
        break;

      case 'EVENT':
        $html .= $this->getEventDetailsHtml(
            $goal->getEventDetails());
        break;
  }
}
print $html;

/**
 * Example #4:
 * How to iterate through URL_DESTINATION goals. 
 */
private function getUrlDestinationDetailsHtml(&$details) {
  $html .= <<<HTML

<pre>

Goal URL            = {$details->getUrl()}
Case Sensitive      = {$details->getCaseSensitive()}
Match Type          = {$details->getMatchType()}
First Step Required = {$details->getFirstStepRequired()}

</pre>
HTML;

  $html .= '<h4>Destination Goal Steps</h4>';
  $steps = $details->getSteps();
  if (count($steps) == 0) {
    $html .= '<p>No Steps Configured</p>';

  } else {
    foreach ($steps as &$step) {
      $html .= <<<HTML
  <pre>

  Step Number = {$step->getNumber()}
  Step Name   = {$step->getName()}
  Step URL    = {$step->getUrl()}

  </pre>
  HTML;
    }
  }
  return $html;
}


/**
 * Example #5:
 * How to iterate through VISIT_TIME_ON_SITE goals. 
 */
private function getVisitTimeOnSiteDetailsHtml(&$details) {
  $html = '<h4>Visit Time On Site Goal</h4>';
  $html .= <<<HTML
<pre>

Comparison Type  = {$details->getComparisonType()}
Comparison Value = {$details->getComparisonValue()}

</pre>
HTML;
  return $html;
}


/**
 * Example #6:
 * How to iterate through VISIT_NUM_PAGES goals. 
 */
private function getVisitNumPagesDetailsHtml(&$details) {
  $html = '<h4>Visit Num Pages Goal</h4>';
  $html .= <<<HTML
<pre>

Comparison Type  = {$details->getComparisonType()}
Comparison Value = {$details->getComparisonValue()}

</pre>
HTML;
  return $html;
}


/**
 * Example #7:
 * How to iterate through EVENT goals. 
 */
private function getEventDetailsHtml(&$details) {
  $html = '<h4>Event Goal</h4><pre>' .
          'Use Event Value = ' . $details->getUseEventValue();

  // Get all the event goal conditions.
  $conditions = $details->getEventConditions();

  foreach ($conditions as &$condition) {
    $html .= "Event Type = $condition->getEventType()";

    $eventType = $condition->getType();
    if ($condition->getType() == 'VALUE') {
      // Process VALUE.
      $html .= "Comparison Type  = $condition->getComparisonType()" .
               "Comparison Value = $condition->getComparisonValue()";

    } else {
      // Process CATEGORY, ACTION, LABEL.
      $html .= "Match Type = $condition->getMatchType()" .
               "Expression = $condition->getExpression()";
    }
  }

  return $html . '</pre>';
}

Python

Uses the Python client library

# Note: This code assumes you have built an authorized Analytics service object.
# See the Goal Dev Guide for details.


# Example #1:
# Requests goals for a single view (profile) with account ID 12345,
# web property ID UA-12345-1, and profile ID 523.
try:
  goals = analytics.management().goals().list(
      accountId='12345',
      webPropertyId='UA-12345-1',
      profileId='523').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 ('Arg, there was an API error : %s : %s' %
         (error.resp.status, error._get_reason()))


# Example #2:
# Retrieves all goals for all views (profiles) for the user, using a
# wildcard ~all as the profile ID.
goals = analytics.management().goals().list(accountId='12345',
                                            webPropertyId='UA-12345-1',
                                            profileId='~all').execute()


# Example #3:
# The results of the list method are stored in the Goals object. The following
# code shows how to iterate through them.
for goal in goals_response.get('items', []):
  print 'Account ID               = %s' % goal.get('accountId')
  print 'Web Property ID          = %s' % goal.get('webPropertyId')
  print 'Internal Web Property ID = %s' % goal.get('internalWebPropertyId')
  print 'View (Profile) ID               = %s' % goal.get('profileId')

  print 'Goal Number = %s' % goal.get('id')
  print 'Goal Name   = %s' % goal.get('name')
  print 'Goal Value  = %s' % goal.get('value')
  print 'Goal Active = %s' % goal.get('active')
  print 'Goal Type   = %s' % goal.get('type')

  print 'Created     = %s' % goal.get('created')
  print 'Updated     = %s' % goal.get('updated')

  # Print the goal details depending on the type of goal.
  if goal.get('urlDestinationDetails'):
    print_url_destination_goal_details(goal.get('urlDestinationDetails'))

  elif goal.get('visitTimeOnSiteDetails'):
    print_visit_time_on_site_goal_details(goal.get('visitTimeOnSiteDetails'))

  elif goal.get('visitNumPagesDetails'):
    print_visit_num_pages_goal_details(goal.get('visitNumPagesDetails'))

  elif goal.get('eventDetails'):
    print_event_goal_details(goal.get('eventDetails'))
# Example #4: # How to access a URL_DESTINATION goals. def print_url_destination_goal_details(goal_details): print 'Goal URL = %s' % goal_details.get('url') print 'Case Sensitive = %s' % goal_details.get('caseSensitive') print 'Match Type = %s' % goal_details.get('matchType') print 'First Step Required = %s' % goal_details.get('firstStepRequired') print '------ Url Destination Goal Steps -------' for goal_step in goal_details.get('steps', []): print 'Step Number = %s' % goal_step.get('number') print 'Step Name = %s' % goal_step.get('name') print 'Step URL = %s' % goal_step.get('url') else: print 'No Steps Configured' # Example #5: # How to access a VISIT_TIME_ON_SITE goal. def print_visit_time_on_site_goal_details(goal_details): print '------ Visit Time On Site Goal -------' print 'Comparison Type = %s' % goal_details.get('comparisonType') print 'comparison Value = %s' % goal_details.get('comparisonValue') # Example #6: # How to iterate through VISIT_NUM_PAGES goals. def print_visit_num_pages_goal_details(goal_details): print '------ Visit Num Pages Goal -------' print 'Comparison Type = %s' % goal_details.get('comparisonType') print 'comparison Value = %s' % goal_details.get('comparisonValue') # Example #7: # How to iterate through EVENT goals. def print_event_goal_details(goal_details): print 'Use Event Value = %s' % goal_details.get('useEventValue') for event_condition in goal_details.get('eventConditions', []): event_type = event_condition.get('type') print 'Type = %s' % event_type if event_type == 'VALUE': print 'Comparison Type = %s' % event_condition.get('comparisonType') print 'Comparison Value = %s' % event_condition.get('comparisonValue') else: # CATEGORY, ACTION, LABEL types. print 'Match Type = %s' % event_condition.get('matchType') print 'Expression = %s' % event_condition.get('expression')

Try it!

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

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.