google分析区域使用google-api-php-client库从谷歌anaytics获取

时间:2015-09-30 09:18:45

标签: php google-api google-analytics-api google-api-php-client

我想从谷歌分析中获取区域目前我正在获得乡村城市但是想要区域

这是我的代码我使用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 / - 艾哈迈达巴德 - 印度 - Linux - i686 - Chrome - 42.0.2311.135

/ MM / bi_analysis / - 艾哈迈达巴德 - 印度 - Windows - 8.1 - Chrome - 45.0.2454.101

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

/ MM / bi_analysis / - 艾哈迈达巴德 - 印度 - Windows - 7 - Chrome - 45.0.2454.101

我想在这个

区域

1 个答案:

答案 0 :(得分:1)

您是否尝试过请求ga:region

  

ga:region
  UI名称:区域
  用户区域,源自IP地址或地理ID。在美国,一个地区是一个州,如纽约州。