簡體   English   中英

谷歌分析區域使用google-api-php-client庫從谷歌分析獲取

[英]google analytics region fetch from google anaytics using google-api-php-client library

我現在想從Google Analytics(分析)中獲取區域,但我正在獲取國家/地區城市,但想要區域

這是我的代碼,我已經使用google-api-php-client庫從Google Analytics(分析)獲取數據,但無法獲取從ga檢索區域的任何查詢。 我也嘗試過ga:region的維度,但從那一無所獲。

  <?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-鉻-42.0.2311.135

/ MM / bi_analysis /-艾哈邁達巴德-印度-Windows-8.1-鉻-45.0.2454.101

/ MM / bi_analysis /-艾哈邁達巴德-印度-Macintosh-英特爾10.10-鍍鉻-46.0.2490.42

/ MM / bi_analysis /-艾哈邁達巴德-印度-Windows-7-鉻-45.0.2454.101

我也想要這個

您是否嘗試請求ga:region

GA:區域
用戶界面名稱:地區
從IP地址或地理ID派生的用戶區域。 在美國,地區是州,例如紐約。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM