dt3999 2013-01-23 12:35
浏览 69
已采纳

PHP-谷歌Api获得目标完成量

Im using this code from google api

I am getting all of the goals names and number

But I cant see a way to get the goal completed amount

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;
}
  • 写回答

1条回答 默认 最新

  • duanquezhan7268 2013-02-03 12:35
    关注

    It seems like you are using the Management API. This is meant for management purposes only. Getting and setting goals.

    If you want data from goals as well you want to be looking at the Core Reporting API.

    The available data for goals 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 did not provide a full implementation example seeing that you already know how to setup the use of a Google API.

    Good luck with you quest!

    EDIT: Added an 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图