PHP- Google Api 获取目标完成量

标签 php google-api google-api-client google-api-php-client

我正在使用来自 google api 的代码

我正在获取所有目标的名称和编号

但我看不到获得目标完成量的方法

这是我得到的:

            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

这是代码:

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

最佳答案

您似乎在使用管理 API。这仅用于管理目的。获得和设定目标。

如果您还需要来自目标的数据,则需要查看 Core Reporting API。

可在此处找到目标的可用数据: https://developers.google.com/analytics/devguides/reporting/core/dimsmets/goalconversions

可在此处找到实现指南: https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide

我没有提供完整的实现示例,因为您已经知道如何设置 Google API 的使用。

祝你任务顺利!

编辑: 添加了有关如何使用它的示例:

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

关于PHP- Google Api 获取目标完成量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479904/

相关文章:

javascript - Google API 多作用域

django - 如何在 django : Storage or database 中存储 google oauth token

Android GoogleApiClient.Builder().enableAutoManage 导致 IllegalArgumentException : Can only use lower 16 bits for requestCode

php - 检测哪个 webkit/moz/etc.要显示的前缀

php - 修改现有代码

php - fatal error : Uncaught Error: Call to a member function select() on null

php - Windows 上的 PEAR 目录问题

php - 使用Google服务帐户时是否需要刷新 token

javascript - 您无权执行此操作。无效的访问 token DialogFlow v2

java - 设置博士编辑错误