Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP and Google Analytics core reporting api v3 - get list of profile Ids - not account Ids
    primarykey
    data
    text
    <p>This questions is more to do with the mechanics of PHP versus the intricacies of Googles Core Reporting API </p> <p>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 <strong>profile ids</strong> from <strong>all</strong> of my respective google analytics accounts.</p> <p>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 <em>getFirstprofileId(&amp;$analytics)</em> function. As is, the code works fine.</p> <p>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.</p> <p>(i'll skip the credentials / login and just get the working logic)</p> <pre><code>if (!$client-&gt;getAccessToken()) { $authUrl = $client-&gt;createAuthUrl(); print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;"; } else { $analytics = new apiAnalyticsService($client); runMainDemo($analytics); } function runMainDemo(&amp;$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-&gt;getCode() . ' : ' . $e-&gt;getMessage(); } catch (Exception $e) { print 'There wan a general error : ' . $e-&gt;getMessage(); } } function getFirstprofileId(&amp;$analytics) { $accounts = $analytics-&gt;management_accounts-&gt;listManagementAccounts(); if (count($accounts-&gt;getItems()) &gt; 0) { $items = $accounts-&gt;getItems(); $firstAccountId = $items[1]-&gt;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-&gt;management_webproperties-&gt;listManagementWebproperties($firstAccountId); if (count($webproperties-&gt;getItems()) &gt; 0) { $items = $webproperties-&gt;getItems(); $firstWebpropertyId = $items[0]-&gt;getId(); $profiles = $analytics-&gt;management_profiles-&gt;listManagementProfiles($firstAccountId, $firstWebpropertyId); if (count($profiles-&gt;getItems()) &gt; 0) { $items = $profiles-&gt;getItems(); return $items[0]-&gt;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(&amp;$analytics, $profileId) { return $analytics-&gt;data_ga-&gt;get( 'ga:' . $profileId, '2010-03-03', '2011-03-03', 'ga:visits'); } function printResults(&amp;$results) { if (count($results-&gt;getRows()) &gt; 0) { $profileName = $results-&gt;getProfileInfo()-&gt;getProfileName(); $profileId = $results-&gt;getProfileInfo()-&gt;getProfileId(); $rows = $results-&gt;getRows(); $visits = $rows[0][0]; print "&lt;p&gt;First profile found: $profileName&lt;/p&gt;"; print "&lt;p&gt;First profileId found (not account id): $profileId&lt;/p&gt;"; print "&lt;p&gt;Total visits: $visits&lt;/p&gt;"; } else { print '&lt;p&gt;No results found.&lt;/p&gt;'; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload