0

I have about 50 websites in my google analytics account. I want to do some research, create notification system and compare analytics data with data from other sources.

That means I want to get a dozen of reports for every site twice a day. I parse them and store in mysql. What's the simplest way do do that?

I registered an application and turned on analytics api in it, but there's no webmaster api. Also I have not a clear understanding of oAuth. Is there a way without redirecting and requesting new access token every time? That's something like granting permanent access for my application in my account from my ip without further confirmations.

So, is there a good tutorial for the beginner about retrieving data from analytics and webmaster written in php, perl or ruby?

Kasheftin
  • 7,509
  • 11
  • 41
  • 68
  • You will certainly use the Google Analytics API. With the oAuth framework, you should be retaining the "Refresh Token". This token can be used to get a new shortlived (60 minutes) "Access Token". I don't think you necessarily need to be concerned with registering your application; this would be used if you were to ask other people access to your Google Analytics. – M Schenkel Sep 13 '13 at 04:16
  • to add to @MSchenkel, although there is a redirect required as part of the oauth flow, once your user has approved your application, the redirection is invisible, in that it redirects back to your app. You could also grant offline access, in which case you get a storeable refresh token which you an use offline to get an access token without bothering the user. – pinoyyid Sep 13 '13 at 13:22

2 Answers2

0

Following code will help you to retrieve "refresh token" using offline access of oauth flow. you can use this refresh token to get an access token without bothering user.

Make sure that the Redirect Uri that you have mentioned in your API console should be same as the filename in which you will place the following code.

For eg. If the redirect uri is:-http://test.com/google_oauth.php then following script should be placed in :- google_oauth.php (path:http://test.com/google_oauth.php)

<?php
$OAuth = array(
    'oauth_uri' => 'https://accounts.google.com/o/oauth2/auth',
    'client_id' => '#clientId',
    'client_secret' => '#clientSecret',
    'access_type' => 'offline',
    'redirect_uri' => 'http://test.com/google_oauth.php',   //this url should be same as you had registered in your api console as redirect uri()
    'oauth_token_uri' => 'https://accounts.google.com/o/oauth2/token'

);
$token = array(
    'access_token' => '',
    'token_type' => '',
    'expires_in' => '',
    'refresh_token' => ''
);

$title = 'No Code';
$AuthCode = 'Null';

// see if error parameter exisits
$error = _get_url_param($_SERVER['REQUEST_URI'], 'error');
if ($error != NULL)
{   // this means the user denied api access to GWMTs
    $title = $error;
}
else
{   // does the code parameter exist?
    $AuthCode = _get_url_param($_SERVER['REQUEST_URI'], 'code');
    if ($AuthCode == NULL)
    {   // get authorization code
        $OAuth_request = _formatOAuthReq($OAuth, "https://www.googleapis.com/auth/analytics.readonly");

        header('Location: ' . $OAuth_request);
        exit; // the redirect will come back to this page and $code will have a value
    }
    else
    {
        $title = 'Got Authorization Code';
        // now exchange Authorization code for access token and refresh token
        $token_response = _get_auth_token($OAuth, $AuthCode);
        $json_obj = json_decode($token_response);
        $token['access_token'] = $json_obj->access_token;
        $token['token_type'] = $json_obj->token_type;
        $token['expires_in'] = $json_obj->expires_in;
        $token['refresh_token'] = $json_obj->refresh_token;
        echo 'access_token = ' . $json_obj->access_token;
    }
}

function _get_auth_token($params, $code)
{
    $url = $params['oauth_token_uri'];

    $fields = array(
        'code' => $code,
        'client_id' => $params['client_id'],
        'client_secret' => $params['client_secret'],
        'redirect_uri' => $params['redirect_uri'],
        'grant_type' => 'authorization_code'
    );
    $response = _do_post($url, $fields);
    return $response;
}

function _do_post($url, $fields)
{
    $fields_string = '';

    foreach ($fields as $key => $value)
    {
        $fields_string .= $key . '=' . $value . '&';
    }
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

function _formatOAuthReq($OAuthParams, $scope)
{
    $uri = $OAuthParams['oauth_uri'];
    $uri .= "?client_id=" . $OAuthParams['client_id'];
    $uri .= "&redirect_uri=" . $OAuthParams['redirect_uri'];
    $uri .= "&scope=" . $scope;
    $uri .= "&response_type=code";
    $uri .= "&access_type=offline";


    return $uri;
}

function _get_url_param($url, $name)
{
    parse_str(parse_url($url, PHP_URL_QUERY), $params);
    return isset($params[$name]) ? $params[$name] : null;
}

function _get_refresh_token($params, $code)
{
    $url = $params['oauth_token_uri'];

    $fields = array(
        'code' => $code,
        'client_id' => $params['client_id'],
        'client_secret' => $params['client_secret'],
        'refresh_token' => $token['refresh_token'],
        'grant_type' => 'refresh_token'
    );
    $response = _do_post($url, $fields);
    return $response;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?= $title; ?></title>
    </head>
    <body>
        <h1>OAuth2 Authorization Code</h1>
        <p>Authorization Code: <?= $AuthCode; ?></p>
        <p>access token: <?= $token['access_token']; ?></p>
        <p>expires in: <?= $token['expires_in']; ?></p>
        <p>refresh token: <?= $token['refresh_token']; ?></p>
        <p></p>

    </body>
</html>

Once you get your refresh token you can use following code to get data from google analytics:-

<?php
$refresh_token='#refresh-token';
$fields_string = "client_id=#ClientId&client_secret=#clientSecret&refresh_token=$refresh_token&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$token_response = curl_exec($ch);
$json_obj = json_decode($token_response);
$access_token = $json_obj->access_token;
curl_close($ch);



$url = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:30566906&start-date=2013-01-01&end-date=2013-04-16&dimensions=ga:medium&metrics=ga:visits,ga:bounces";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $access_token"));
curl_setopt($ch, CURLOPT_URL, html_entity_decode($url));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);
$json_obj = json_decode($output);
$test=$json_obj->columnHeaders;
foreach($test as $a){
    var_dump($a);

}
curl_close($ch);
?>

In above scripts:-
#clientId and #clientSecret should be replaced by the client id and client secret that you have received while registering your web application.

bummi
  • 27,123
  • 14
  • 62
  • 101
sanju
  • 14
  • 2
0

For your use case I would suggest using a Google Service Account rather than the OAuth flow that requires human confirmation.

There are Client Libraries available for several languages that can make the OAuth part simpler. For example, in the ruby library includes a sample script showing how to use a service account with Google Analytics API. Essentially it's this:

@client = Google::APIClient.new(
  :application_name => opts['application_name'],
  :application_version => opts['application_version'])

## Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)

@client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience => 'https://accounts.google.com/o/oauth2/token',
  :scope => 'https://www.googleapis.com/auth/analytics.readonly',
  :issuer => service_account_email,
  :signing_key => key)

## Request a token for our service account
@client.authorization.fetch_access_token!

query_data = @client.execute(:api_method => @analytics.data.ga.get, :parameters => {
    'ids' => "ga:" + @profileID,
    'start-date' => @startDate,
    'end-date' => @endDate,
    'dimensions' => dimension,
    'metrics' => metric,
    'sort' => sort
  })

There is a Webmaster API available although it does not have access to the query data. You can get that through this Google-published python script or through a similar one in PHP with more data.

jwadsack
  • 5,708
  • 2
  • 40
  • 50