user1052888
user1052888

Reputation:

PHP and Google Analytics core reporting api v3 - get list of profile Ids - not account Ids

This questions is more to do with the mechanics of PHP versus the intricacies of Googles Core Reporting API

My goal is to modify the code below(taken from Google's HelloAnalyticsApi.php) so that it will loop through and "printf" a list of profile ids from all of my respective google analytics accounts.

Currently,the code below goes through and grabs the first profile ID of the first account it sees. It does so by first obtaining the Account Id, then the web property of that account Id, then finally, the account's profile Id. This is accomplished in the getFirstprofileId(&$analytics) function. As is, the code works fine.

In my case, I have to skip the first one (position 0 in the array) because although it is an account that gets listed, it doesn't have any webproperties. Therefore, I have to start counting from 1 instead of 0.

(i'll skip the credentials / login and just get the working logic)

if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {
    $analytics = new apiAnalyticsService($client);
    runMainDemo($analytics); 

}

function runMainDemo(&$analytics) {
  try {

    // Step 2. Get the user's first profile ID.
    $profileId = getFirstProfileId($analytics);

    if (isset($profileId)) {

      // Step 3. Query the Core Reporting API.
      $results = getResults($analytics, $profileId);

      // Step 4. Output the results.
      printResults($results);
    }

  } catch (apiServiceException $e) {
    // Error from the API.
    print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();

  } catch (Exception $e) {
    print 'There wan a general error : ' . $e->getMessage();
  }
}

function getFirstprofileId(&$analytics) {

  $accounts = $analytics->management_accounts->listManagementAccounts();


  if (count($accounts->getItems()) > 0) {

    $items = $accounts->getItems();
    $firstAccountId = $items[1]->getId(); 
    //item 0 is an account that gets listed but doesn't have any webproperties.  
    //This account doesn't show in our analytics

    $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

    if (count($webproperties->getItems()) > 0) {

      $items = $webproperties->getItems();
      $firstWebpropertyId = $items[0]->getId();

      $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

      if (count($profiles->getItems()) > 0) {

        $items = $profiles->getItems();
        return $items[0]->getId();

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



function getResults(&$analytics, $profileId) {

   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '2010-03-03',
      '2011-03-03',
       'ga:visits');
}


function printResults(&$results) {

  if (count($results->getRows()) > 0) {

    $profileName = $results->getProfileInfo()->getProfileName();
    $profileId = $results->getProfileInfo()->getProfileId();

    $rows = $results->getRows();
    $visits = $rows[0][0];

    print "<p>First profile found: $profileName</p>";
    print "<p>First profileId found (not account id): $profileId</p>";
    print "<p>Total visits: $visits</p>";

  } else {
    print '<p>No results found.</p>';
  }
}

?>

Upvotes: 1

Views: 2933

Answers (1)

user1052888
user1052888

Reputation:

Easy...Modified function - added for loop. Looped by number of items in account:

function getProfileId(&$analytics) {

    //function getFirstprofileId(&$analytics) {

      $accounts = $analytics->management_accounts->listManagementAccounts();

        $num = count($accounts->getItems());


        for ($i=1; $i < $num; $i++) { 

          if (count($accounts->getItems()) > 0) {

            $items = $accounts->getItems();
            $firstAccountId = $items[$i]->getId(); 
            //item 0 is an account that gets listed but doesn't have any webproperties.  
            //This account doesn't show in our analytics

            $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId);

            if (count($webproperties->getItems()) > 0) {

              $items = $webproperties->getItems();
              $firstWebpropertyId = $items[0]->getId();

              $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId);

              if (count($profiles->getItems()) > 0) {

                $items = $profiles->getItems();

                $profileId = $items[0]->getId();

                print "<p>Profile ID (not account id): ".  $profileId."</p>";



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

      }

    }

Upvotes: 3

Related Questions