Google Analytics仅显示1位用户的数据

时间:2016-04-01 18:34:07

标签: php google-analytics oauth-2.0 clientid

我正在尝试创建一个页面,其中将显示有关个人GA帐户的数据。目前,无论谁登录,都只显示1个用户数据。我不明白为什么会这样,因为我使用客户端ID与oauth2.0进行身份验证。我调用了getFirstprofileId,但无论谁登录谷歌,结果总是返回相同的id

    info.php
<?php
    // Load the Google API PHP Client Library.
    require_once realpath(dirname(__FILE__).'/google-api-php-client/src/Google/autoload.php');

    // Start a session to persist credentials.
    session_start();
     $client_id = 'XXXXXXXXX';

     $client_secret = 'XXXXXXX';

     $redirect_uri = 'http://XXXXX.info/info.php';

    // Create the client object and set the authorization configuration
    // from the client_secretes.json you downloaded from the developer console.

    $client = new Google_Client();

    $client->setClientId($client_id);

    $client->setClientSecret($client_secret);

    $client->setRedirectUri($redirect_uri);

    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

    $analytics = new Google_Service_Analytics($client);

    /************************************************

      If we're logging out we just need to clear our

      local access token in this case

     ************************************************/

    if (isset($_REQUEST['logout'])) {

      unset($_SESSION['access_token']);

    }

    /************************************************

      If we have a code back from the OAuth 2.0 flow,

      we need to exchange that with the authenticate()

      function. We store the resultant access token

      bundle in the session, and redirect to ourself.

     ************************************************/

    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 we have an access token, we can make

      requests, else we generate an authentication URL.

     ************************************************/

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

      $client->setAccessToken($_SESSION['access_token']);

    } else {

      $authUrl = $client->createAuthUrl();


    }


    function getFirstprofileId(&$analytics) {
      // Get the user's first view (profile) ID.

      // Get the list of accounts for the authorized user.
      $accounts = $analytics->management_accounts->listManagementAccounts();

      if (count($accounts->getItems()) > 0) {
        $items = $accounts->getItems();
        $firstAccountId = $items[0]->getId();

        // Get the list of properties for the authorized user.
        $properties = $analytics->management_webproperties
            ->listManagementWebproperties($firstAccountId);

        if (count($properties->getItems()) > 0) {
          $items = $properties->getItems();
          $firstPropertyId = $items[0]->getId();

          // Get the list of views (profiles) for the authorized user.
          $profiles = $analytics->management_profiles
              ->listManagementProfiles($firstAccountId, $firstPropertyId);

          if (count($profiles->getItems()) > 0) {
            $items = $profiles->getItems();

            // Return the first view (profile) ID.
            return $items[0]->getId();

          } else {
            throw new Exception('No views (profiles) found for this user.');
          }
        } else {
          throw new Exception('No properties found for this user.');
        }
      } else {
        throw new Exception('No accounts found for this user.');
      }
    }

    function getResults(&$analytics, $profileId) {
      // Calls the Core Reporting API and queries for the number of sessions
      // for the last seven days.
      return $analytics->data_ga->get(
          'ga:' . $profileId,
          '7daysAgo',
          'today',
          'ga:sessions');
    }

    function printResults(&$results) {
      // Parses the response from the Core Reporting API and prints
      // the profile name and total sessions.
      if (count($results->getRows()) > 0) {

        // Get the profile name.
        $profileName = $results->getProfileInfo()->getProfileName();

        // Get the entry for the first entry in the first row.
        $rows = $results->getRows();
        $sessions = $rows[0][0];

        // Print the results.
        print "<p>First view (profile) found: $profileName</p>";
        print "<p>Total sessions: $sessions</p>";
      } else {
        print "<p>No results found.</p>";
      }
    }
    $profile = getFirstProfileId($analytics);
    $results = getResults($analytics, $profile);
    printResults($results)
    ?>

0 个答案:

没有答案