Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm adding a PHP answer - you may be able to adjust or convert it to garb / ruby code.</p> <p>You should be able to use Analytics with service accounts now. You will indeed have to use a private key instead of an access token.</p> <p><strong>Create an app in the API Console</strong><br> Basically, you go to the Google API Console and create an App.<br> Enable Google Analytics in the services tab.<br> In the API Access tab, create a new OAuth ID (Create another client ID... button), select <strong>service account</strong> and download your private key (Generate new key... link). You'll have to upload the key to your web server later. </p> <p>On the API Access page, in the Service account section, copy the email address (@developer.gserviceaccount.com) and add a new user with this email address to your Google Analytics profile. <strong>If you do not do this, you'll get some nice errors</strong> </p> <p><strong>Code</strong><br> Download the latest Google PHP Client off SVN (from the command line <code>svn checkout http://google-api-php-client.googlecode.com/svn/trunk/ google-api-php-client-read-only</code>).</p> <p>You can now access the Analytics API in code:</p> <pre><code>require_once 'Google_Client.php'; require_once 'contrib/Google_AnalyticsService.php'; $keyfile = 'dsdfdss0sdfsdsdfsdf44923dfs9023-privatekey.p12'; // Initialise the Google Client object $client = new Google_Client(); $client-&gt;setApplicationName('Your product name'); $client-&gt;setAssertionCredentials( new Google_AssertionCredentials( '11122233344@developer.gserviceaccount.com', array('https://www.googleapis.com/auth/analytics.readonly'), file_get_contents($keyfile) ) ); // Get this from the Google Console, API Access page $client-&gt;setClientId('11122233344.apps.googleusercontent.com'); $client-&gt;setAccessType('offline_access'); $analytics = new Google_AnalyticsService($client); // We have finished setting up the connection, // now get some data and output the number of visits this week. // Your analytics profile id. (Admin -&gt; Profile Settings -&gt; Profile ID) $analytics_id = 'ga:1234'; $lastWeek = date('Y-m-d', strtotime('-1 week')); $today = date('Y-m-d'); try { $results = $analytics-&gt;data_ga-&gt;get($analytics_id, $lastWeek, $today,'ga:visits'); echo '&lt;b&gt;Number of visits this week:&lt;/b&gt; '; echo $results['totalsForAllResults']['ga:visits']; } catch(Exception $e) { echo 'There was an error : - ' . $e-&gt;getMessage(); } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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