Php-google api getting goal completed amount

I am using this code from google api

I get all the names and number of targets

But I don't see any way to get the completed target.

This is what I get:

            Account ID               = xxx
            Web Property ID          = xxx
            Internal Web Property ID = xxx
            Profile ID               = xxx


            Goal Number = 1
            Goal Name   = Open User (MT Register)
            Goal Value  = 0
            Goal Active = 1
            Goal Type   = URL_DESTINATION

            Created = 2012-07-22T10:20:02.183Z
            Updated = 2012-08-15T12:43:06.045Z



Goal URL            = /04_thankyou.php
Case Sensitive      = 
Match Type          = REGEX
First Step Required = 1

Destination Goal Steps


Step Number = 1
Step Name   = abc
  Step URL    = /01_insert_phone.php



Step Number = 2
Step Name   = abcd
  Step URL    = /02_progress.step



Step Number = 3
Step Name   = abcde
  Step URL    = /03_insert_pincode.php

      

This is the code:

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>';
}

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;
}

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;
}

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;
}

    require_once 'google_api.php';

    if (isset($_SESSION['token'])) { 
        $client->setAccessToken($_SESSION['token']); 
    }

    if (!$client->getAccessToken()) {
      $authUrl = $client->createAuthUrl();
      print "<a class='login' href='$authUrl'>Connect Me!</a>";

    } else {
      $analytics = new Google_AnalyticsService($client);

        try {
          $goals = $analytics->management_goals
                             ->listManagementGoals('25788360',
                                                   'UA-25788360-20',
                                                   '~all');

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

        $html = '';
$items = $goals->getItems();
  foreach ($items as &$goal) {
    $html .= "
            <pre>
            Account ID               = {$goal->getAccountId()}
            Web Property ID          = {$goal->getWebPropertyId()}
            Internal Web Property ID = {$goal->getInternalWebPropertyId()}
            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>";

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

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

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

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

    echo $html;
}

      

+3


source to share


1 answer


It looks like you are using a management API. This is for management purposes only. Obtaining and setting goals.

If you want data from goals, you want to look at the Core Reporting API.

The data available for the purposes can be found here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets/goalconversions

The implementation guide can be found here: https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide



I have not provided a complete example implementation after seeing that you already know how to configure the use of Google APIs.

Good luck with your quest!

EDIT: Added example on how to use it:

<?php
$client = new apiAnalyticsService();

function queryCoreReportingApi() {
    $optParams = array( //OPTINAL SETTINGS
      'dimensions' => '', //A comma-separated list of Multi-Channel Funnels dimensions. E.g., 'mcf:source,mcf:medium'. (string)
      'sort' => '', //A comma-separated list of dimensions or metrics that determine the sort order for the Analytics data. (string)
      'filters' => '', //A comma-separated list of dimension or metric filters to be applied to the Analytics data. (string)
      'start-index' => '', //An index of the first entity to retrieve. Use this parameter as a pagination mechanism along with the max-results parameter. (integer, 1+)
      'fields' => '', //Selector specifying which fields to include in a partial response.
      'max-results' => '25'); //The maximum number of entries to include in this feed. (integer)

  return $service->data_mcf->get(
      $id, //Unique table ID for retrieving Analytics data. Table ID is of the form ga:XXXX, where XXXX is the Analytics profile ID. (string)
      '2010-01-01', //Start date for fetching Analytics data. All requests should specify a start date formatted as YYYY-MM-DD. (string)
      '2010-01-15', //End date for fetching Analytics data. All requests should specify an end date formatted as YYYY-MM-DD. (string)
      'ga:totalConversions', //A comma-separated list of Multi-Channel Funnels metrics. E.g., 'mcf:totalConversions,mcf:totalConversionValue'. At least one metric must be specified. (string)
      $optParams);
}

      

+1


source







All Articles