How can I reach google analytics datas? - php

My goal is to show google analytics datas on the header of my site. Thats all I have until now (its from googleApiPhPClient/examples/analytics:
require_once 'library/GoogleApiPhpClient/apiClient.php';
$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");
// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('aaa');
$client->setClientSecret('bbb_gK');
$client->setRedirectUri('ccc');
$client->setDeveloperKey('dd');
require_once ('contrib/apiAnalyticsService.php');
$service = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$props = $service->management_webproperties->listManagementWebproperties("~all");
print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
$segments = $service->management_segments->listManagementSegments();
print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";
$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
but this only drops a "connect me" link. Am in the somewhat right way on getting analytic datas anyway?

I've been using this code.google.com/p/gapi-google-analytics-php-interface which only requires the google account user name and password and works pretty well
a sample code of mine where im checking visits $ga->requestReportData($ga_profile_id,array('browser','browserVersion','country'‌​),array('pageviews','visits'),null,"country==USA",$yesterday,$todate);
when you log in to your account, select the profile you already made for your web site . when you will click it, the url would be like google.com/analytics/web/#report/visitors-overview/… so the ga_profile_id would be the one written after "p" in the URL.. i see this way only to get this profile id

Related

Can't Get Google Client API simple.php example to work

Just trying to use the Google Client API to pull traffic data for my site and thought I would try the simple.php file on https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php
I've edited it appropriately for my credentials and I can get it to go from the "Connect Me" link to the request permission page, but after I accept I end up at "This webpage is not available". I'm wondering if there might be an issue with needing the token refreshed?
Any thoughts are greatly appreciated. Thanks!
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");
// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('MY_CLIENT_ID');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->setDeveloperKey('MY_DEVELOPER_KEY');
$client->setRedirectUri('http://localhost/google-analytics/simple.php');
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$props = $service->management_webproperties->listManagementWebproperties("~all");
print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
$segments = $service->management_segments->listManagementSegments();
print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";
$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

Google Calendar API/PHP

I have followed the directions exactly, copied code examples - I cannot get this to respond (write an event on a calendar).
Please help - if there is any help for me. AFter 12 hours of working on this with a huge deadline of tomorrow, I'm about to pull my hair out.
On Google Developer Console, created a project and generated keys required.
The keys below are altered for safety.
None of the echos are showing except for the first one with the order number in it.
My programmer got this code to work, so I know it's OK - it's something set up wrong on the console, but I need a clue:
Full code - like I said, it's been tested and works on another project.
if(isset($_GET['orderid']) )
{
$_SESSION['oid']= $_GET['orderid'];
}
echo $_GET['orderid'];
$client = new Google_Client();
$client->setApplicationName("API Project");
$client->setClientId('9999999999999.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxxxxx');
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client->setRedirectUri($scriptUri);
//$client->setDeveloperKey('AIxxxxxxxxxxxxx'); - SERVER APPS
$client->setDeveloperKey('AIxxxxxxxxxxxxxxx'); // - WEB APPS
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
echo $_GET['code'];
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
echo $_SESSION['token'];
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
//print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
// database access is here and like I said, it is tested and working
echo "<center>Event has successfully added to Your Google Calendar...</center>";
unset($_SESSION['oid']);
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Click Here for Google Authorization!</a>";
}
If you just want to add an event on Google Calendar (no other interactions), I think you should use the direct link :
https://www.google.com/calendar/render?action=TEMPLATE&text=Your+event+title&dates=20140325T090000Z/20140325T100000Z&details=Your+event+description&location=Room+12&pli=1&uid&sf=true&output=xml
You just have to customize variables and escape non-URI characters.
** EDIT **
Do you successfully connect the user through OAuth 2?
** EDIT 2 **
Your code is working for me without the DB access :
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
//print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
// DB ACCESS
echo "<center>Event has successfully added to Your Google Calendar...</center>";
unset($_SESSION['oid']);
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Click Here for Google Authorization!</a>";
}
Julian

Get post from Google+ without browser's authentication in google's account via PHP

I'm want to get google+ posts via PHP, but google requires my login via browser.
It's possible provide the account's authentication via PHP, allowing me to get the posts without login every time I want to get the posts?
The code is:
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('clientid');
$client->setClientSecret('clientsecret');
$client->setRedirectUri('http://localhost/OLA/google+/simple.php/b.html');
$client->setDeveloperKey('apikey');
$plus = new Google_PlusService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) { // login stuff <-------
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die("The session state did not match.");
}
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
//$me = $plus->people->get('me');
//print "Your Profile: <pre>" . print_r($me, true) . "</pre>";
$params = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public', $params);
//print "Your Activities: <pre>" . print_r($activities, true) . "</pre>";
$params = array(
'orderBy' => 'recent',
'maxResults' => '20'//20
);
$q = "tag";
$results = $plus->activities->search($q, $params);
//code ....
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
// This is the part that logs me in via browser <------
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Yep, just call as you would and use the simple API access key - you can retrieve public information, but you will have to supply to long numeric ID for the user you're retrieving posts for rather than using the string 'me'. Take a look at the latest version of the library as well: https://github.com/google/google-api-php-client as well. You need to setup your client as you were doing, with an API key from a project which has the Google+ enabled on https://developers.google.com/console
$client = new Google_Client();
$client->setApplicationName("Post Fetcher");
$client->setDeveloperKey('PUT_YOUR_API_KEY_HERE_OR_IT_WONT_WORK');
$plus = new Google_Service_Plus($client);
$activities = $this->plus->activities
->listActivities("118051310819094153327", "public");

Granting Access For Blogger in OAUTH 2

I am new to Google Oauth 2.I saw google documentation and got the below php library
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('...');
$client->setClientSecret('....');
$client->setRedirectUri('http://photoapp.biz/0/blogger/test.php');
$client->setDeveloperKey('....');
$plus = new Google_PlusService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}
?>
From above code My authentication works fine and i am able to get the Token.But This code allows access for Google Plus .I need To authenticat Blogger with Oauth.Google documentation did not help me.Can some one please guide me .Thanks.

Google API - forces to Grant Permission Every time

I am using Google API PHP Client. And every time i try to login I am forced to grant permissions to the apps.
Below is my code. I am basically accessing Google API for Analytics
require_once 'lib/apiClient.php';
require_once 'lib/contrib/apiAnalyticsService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Analytics");
$client->setClientId('7xxxx');
$client->setClientSecret('xxxx');
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');
$analytics = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$profileId = getProfileIds($analytics);
echo "<select>";
foreach ($profileId as $profiles) {
echo "<option value=\"" . $profiles['profileId'] . "\">" . $profiles['name'] .
"</option>";
}
echo "</select>";
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
What option can we pass to createAuthUrl() ??
By Setting
$client->setApprovalPrompt('auto');
It will automatically redirect if the account has access to the apis. By Default its 'force'

Categories