使用Google -api-php-client库从Google分析获取Google分析区域



我想从谷歌分析中获取区域,目前我正在获得国家城市,但想要区域

这是我的代码,我使用google-api-php-client库从谷歌分析获得数据但是不能从ga中得到任何检索区域的查询。我也试过Ga:维度上的区域,但是没有得到任何结果。如有任何帮助,不胜感激

  <?php
 session_start();
 require_once 'google-api-php-client-master/autoload.php';
set_include_path("../".PATH_SEPARATOR.get_include_path());
require_once 'google-api-php-client-master/src/Google/Client.php';

   require_once 'google-api-php-client-master/src/Google/Service/Analytics.php';

  $client_id = 'xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$client_secret = 'xxxxxxxxxxxxxxxxxxx';
$redirect_uri = 'http://php.vervesys.com/test/index.php';
$client = new Google_Client();
$client->setApplicationName('test project');
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

$client->addScope("https://www.googleapis.com/auth/analytics.readonly");

if (isset($_REQUEST['logout'])) {
 unset($_SESSION['access_token']);
 }

 if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

  if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
 $client->setAccessToken($_SESSION['access_token']);
  } else {
  $authUrl = $client->createAuthUrl();
  }
 ?>

 <?php if (isset($authUrl)): ?>
   <a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
 <?php  else: ?>
 <a class='logout' href='?logout'>Logout</a>
   <?php endif ?>
 <hr/>
 <?php

  if ($client->getAccessToken()) {

   $analytics = new Google_Service_Analytics($client);
   $_SESSION['access_token'] = $client->getAccessToken();

    $properties = $analytics->management_webproperties->listManagementWebproperties("~all");
  $items = $properties->getItems();
   if(is_array($items)){
  foreach($items as $key => $property)
  {
    $profile = $analytics->management_profiles->listManagementProfiles($property->accountId, $property->id)->getItems();
   echo '<a href="index.php?id=' . $profile[0]->id . '&domain=' . urlencode($property->websiteUrl) . '">' . $property->websiteUrl . '</a>      <br/>';
     }
     }
    }

   if(isset($_GET['id'])){
   $results_top_pages = $analytics->data_ga->get(
    'ga:'.$_GET['id'],
    date('Y-m-d',strtotime('-14 days')),
    date('Y-m-d',strtotime('-1 day')),
    'ga:pageviews',
    array(
        'dimensions' => 'ga:pagePath,ga:city,ga:country,ga:operatingSystem,ga:operatingSystemVersion,ga:browser,ga:browserVersion,
        'sort' => '-ga:pageviews',
        'max-results' => 50
    ));
  if(is_array($results_top_pages->getRows())){
    echo '<ol>';
    foreach($results_top_pages->getRows() as $top_page){
        echo '<li>';
        echo $top_page[0];
        echo ' - '.$top_page[1].' ';
 echo ' - '.$top_page[2].' ';
 echo ' - '.$top_page[3].' ';
 echo ' - '.$top_page[4].' ';
 echo ' - '.$top_page[5].' ';
 echo ' - '.$top_page[6].' ';

        echo '</li>';
     }
     echo '</ol>';
  }
}
?>
从这段代码中,我得到如下输出:

/MM/bi_analysis/- Ahmedabad - India - Linux - i686 - Chrome - 42.0.2311.135

/MM/bi_analysis/- Ahmedabad - India - Windows - 8.1 - Chrome - 45.0.2454.101

/MM/bi_analysis/-艾哈迈达巴德-印度- Macintosh - Intel 10.10 - Chrome - 46.0.2490.42

/MM/bi_analysis/- Ahmedabad - India - Windows - 7 - Chrome - 45.0.2454.101

我想在这个

您是否尝试请求ga:region ?


ga:区域用户界面名称:区域
用户所在的区域,由IP地址或地理id派生。在美国,一个地区是一个州,如纽约。

相关内容

  • 没有找到相关文章

最新更新