Analytics api ile yapılıyor, daha önce yaptım. Bitane de kendimce class yazmıştım. Php bilmiyorsan fuzuli uğraşma, iyi derece php bilmen gerekiyor. Yazdığım class da aşağıda, kendine örnek alabilir, kodları inceleyebilir, kendininkini yapabilirsin.
class Analytics
{
var $client;
var $analytics;
var $realTimeResults;
var $results;
var $visitors;
var $name;
var $config;
public function __construct($config)
{
$this->config = $config;
require DIR_CLASSES . "/google-api-php-client/vendor/autoload.php";
$KEY_FILE_LOCATION = DIR_ENGINE . 'config/' . $this->config->get("api_analytics_account");
// Create and configure a new client object.
$this->client = new Google_Client();
$this->client->setUseBatch(true);
$this->batch = new Google_Http_Batch($this->client);
$this->client->setApplicationName("Hello Analytics Reporting");
$this->client->setAuthConfig($KEY_FILE_LOCATION);
$this->client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$this->analytics = new Google_Service_Analytics($this->client);
$this->profileId = $this->config->get("api_analytics_id");
$this->printResults();
}
public function getResults()
{
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$this->results = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'today',
'today',
'ga:sessions,ga:pageviews,ga:avgsessionduration');
$this->batch->add($this->results, "results");
$this->searchResults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'today',
'today',
'ga:organicSearches',
array("dimensions" => "ga:pagePath", "sort" => "-ga:organicSearches", "max-results" => 15));
$this->batch->add($this->searchResults, "searchResults");
$this->todayRefresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'today',
'today',
'ga:sessions,ga:pageviews',
array("dimensions" => "ga:channelGrouping")
);
$this->batch->add($this->todayRefresults, "todayRefresults");
$this->weekresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'6daysAgo',
'today',
'ga:sessions,ga:pageviews,ga:avgsessionduration');
$this->batch->add($this->weekresults, "weekresults");
$this->searchWeekResults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'6daysAgo',
'today',
'ga:organicSearches',
array("dimensions" => "ga:pagePath", "sort" => "-ga:organicSearches", "max-results" => 15));
$this->batch->add($this->searchWeekResults, "searchWeekResults");
$this->weekRefresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
'6daysAgo',
'today',
'ga:sessions,ga:pageviews',
array("dimensions" => "ga:channelGrouping")
);
$this->batch->add($this->weekRefresults, "weekRefresults");
$this->monthresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
date("Y-m-01"),
date("Y-m-d", time()),
'ga:sessions,ga:pageviews,ga:avgsessionduration');
$this->batch->add($this->monthresults, "monthresults");
$this->searchMonthResults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
date("Y-m-01"),
date("Y-m-d", time()),
'ga:organicSearches',
array("dimensions" => "ga:pagePath", "sort" => "-ga:organicSearches", "max-results" => 15));
$this->batch->add($this->searchMonthResults, "searchMonthResults");
/*yıllık referer kanal*/
$this->yearRefresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
date("Y-01-01"),
date("Y-m-d", time()),
'ga:sessions,ga:pageviews',
array("dimensions" => "ga:channelGrouping")
);
$this->batch->add($this->yearRefresults, "yearRefresults");
/*yıllık referer kanal*/
/*realtime*/
$this->realTimeResults = $this->analytics->data_realtime->get(
'ga:' . $this->profileId,
'rt:activeUsers',
array('dimensions' => 'rt:medium')
);
$this->batch->add($this->realTimeResults, "realTimeResults");
/*realtime*/
$this->monthRefresults = $this->analytics->data_ga->get(
'ga:' . $this->profileId,
date("Y-m-01"),
date("Y-m-d", time()),
'ga:sessions,ga:pageviews',
array("dimensions" => "ga:channelGrouping")
);
$this->batch->add($this->monthRefresults, "monthRefresults");
$results = $this->batch->execute();
$this->results = $results["response-results"];
$this->todayRefresults = $results["response-todayRefresults"];
$this->weekresults = $results["response-weekresults"];
$this->searchResults = $results["response-searchResults"];
$this->searchWeekResults = $results["response-searchWeekResults"];
$this->weekRefresults = $results["response-weekRefresults"];
$this->monthRefresults = $results["response-monthRefresults"];
$this->monthresults = $results["response-monthresults"];
$this->searchMonthResults = $results["response-searchMonthResults"];
$this->realTimeResults = $results["response-realTimeResults"];
}
public function printResults()
{
$this->getResults();
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
//var_dump($this->realTimeResults->rows);
if (isset($this->realTimeResults->rows) and count($this->realTimeResults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->realTimeResults->rows;
@$this->visitors->realtime = $rows[0][1];
// Print the results.
} else {
@$this->visitors->realtime = 0;
}
if (isset($this->results->rows) and count($this->results->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->results->rows;
@$this->visitors->today->unique = $rows[0][0];
@$this->visitors->today->views = $rows[0][1];
@$this->visitors->today->avgSessionDuration = gmdate("i:s", $rows[0][2]);
if (isset($this->todayRefresults->rows) and count($this->todayRefresults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->todayRefresults->rows;
@$this->visitors->today->channels->direct->unique = (isset($rows[0][1]) ? $rows[0][1] : 0);
@$this->visitors->today->channels->direct->views = (isset($rows[0][2]) ? $rows[0][2] : 0);
@$this->visitors->today->channels->organic->unique = (isset($rows[1][1]) ? $rows[1][1] : 0);
@$this->visitors->today->channels->organic->views = (isset($rows[1][2]) ? $rows[1][2] : 0);
@$this->visitors->today->channels->referal->unique = (isset($rows[2][1]) ? $rows[2][1] : 0);
@$this->visitors->today->channels->referal->views = (isset($rows[2][2]) ? $rows[2][2] : 0);
@$this->visitors->today->channels->social->unique = (isset($rows[3][1]) ? $rows[3][1] : 0);
@$this->visitors->today->channels->social->views = (isset($rows[3][2]) ? $rows[3][2] : 0);
// Print the results.
} else {
@$this->visitors->today->channels->direct->unique = 0;
@$this->visitors->today->channels->direct->views = 0;
@$this->visitors->today->channels->organic->unique = 0;
@$this->visitors->today->channels->organic->views = 0;
@$this->visitors->today->channels->referal->unique = 0;
@$this->visitors->today->channels->referal->views = 0;
@$this->visitors->today->channels->social->unique = 0;
@$this->visitors->today->channels->social->views = 0;
}
// Print the results.
} else {
@$this->visitors->today->unique = 0;
@$this->visitors->today->views = 0;
@$this->visitors->today->avgSessionDuration = 0;
@$this->visitors->today->channels->direct->unique = 0;
@$this->visitors->today->channels->direct->views = 0;
@$this->visitors->today->channels->organic->unique = 0;
@$this->visitors->today->channels->organic->views = 0;
@$this->visitors->today->channels->referal->unique = 0;
@$this->visitors->today->channels->referal->views = 0;
@$this->visitors->today->channels->social->unique = 0;
@$this->visitors->today->channels->social->views = 0;
}
//WEEK WEEK WEEK
if (isset($this->weekresults->rows) and count($this->weekresults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->weekresults->rows;
@$this->visitors->week->unique = $rows[0][0];
@$this->visitors->week->views = $rows[0][1];
@$this->visitors->week->avgSessionDuration = gmdate("i:s", $rows[0][2]);
if (isset($this->weekRefresults->rows) and count($this->weekRefresults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->weekRefresults->rows;
@$this->visitors->week->channels->direct->unique = (isset($rows[0][1]) ? $rows[0][1] : 0);
@$this->visitors->week->channels->direct->views = (isset($rows[0][2]) ? $rows[0][2] : 0);
@$this->visitors->week->channels->organic->unique = (isset($rows[1][1]) ? $rows[1][1] : 0);
@$this->visitors->week->channels->organic->views = (isset($rows[1][2]) ? $rows[1][2] : 0);
@$this->visitors->week->channels->referal->unique = (isset($rows[2][1]) ? $rows[2][1] : 0);
@$this->visitors->week->channels->referal->views = (isset($rows[2][2]) ? $rows[2][2] : 0);
@$this->visitors->week->channels->social->unique = (isset($rows[3][1]) ? $rows[3][1] : 0);
@$this->visitors->week->channels->social->views = (isset($rows[3][2]) ? $rows[3][2] : 0);
// Print the results.
} else {
@$this->visitors->week->channels->direct->unique = 0;
@$this->visitors->week->channels->direct->views = 0;
@$this->visitors->week->channels->organic->unique = 0;
@$this->visitors->week->channels->organic->views = 0;
@$this->visitors->week->channels->referal->unique = 0;
@$this->visitors->week->channels->referal->views = 0;
@$this->visitors->week->channels->social->unique = 0;
@$this->visitors->week->channels->social->views = 0;
}
// Print the results.
} else {
@$this->visitors->week->unique = 0;
@$this->visitors->week->views = 0;
@$this->visitors->week->avgSessionDuration = 0;
@$this->visitors->week->channels->direct->unique = 0;
@$this->visitors->week->channels->direct->views = 0;
@$this->visitors->week->channels->organic->unique = 0;
@$this->visitors->week->channels->organic->views = 0;
@$this->visitors->week->channels->referal->unique = 0;
@$this->visitors->week->channels->referal->views = 0;
@$this->visitors->week->channels->social->unique = 0;
@$this->visitors->week->channels->social->views = 0;
}
//WEEK WEEK WEEK
if (isset($this->monthresults->rows) and count($this->monthresults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->monthresults->rows;
@$this->visitors->month->unique = $rows[0][0];
@$this->visitors->month->views = $rows[0][1];
@$this->visitors->month->avgSessionDuration = gmdate("i:s", $rows[0][2]);
if (isset($this->monthRefresults->rows) and count($this->monthRefresults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->monthRefresults->rows;
//var_dump($this->monthRefresults->rows);
@$this->visitors->month->channels->direct->unique = (isset($rows[0][1]) ? $rows[0][1] : 0);
@$this->visitors->month->channels->direct->views = (isset($rows[0][2]) ? $rows[0][2] : 0);
@$this->visitors->month->channels->organic->unique = (isset($rows[1][1]) ? $rows[1][1] : 0);
@$this->visitors->month->channels->organic->views = (isset($rows[1][2]) ? $rows[1][2] : 0);
@$this->visitors->month->channels->referal->unique = (isset($rows[2][1]) ? $rows[2][1] : 0);
@$this->visitors->month->channels->referal->views = (isset($rows[2][2]) ? $rows[2][2] : 0);
@$this->visitors->month->channels->social->unique = (isset($rows[3][1]) ? $rows[3][1] : 0);
@$this->visitors->month->channels->social->views = (isset($rows[3][2]) ? $rows[3][2] : 0);
// Print the results.
} else {
@$this->visitors->month->channels->direct->unique = 0;
@$this->visitors->month->channels->direct->views = 0;
@$this->visitors->month->channels->organic->unique = 0;
@$this->visitors->month->channels->organic->views = 0;
@$this->visitors->month->channels->referal->unique = 0;
@$this->visitors->month->channels->referal->views = 0;
@$this->visitors->month->channels->social->unique = 0;
@$this->visitors->month->channels->social->views = 0;
}
// Print the results.
} else {
@$this->visitors->month->unique = 0;
@$this->visitors->month->views = 0;
@$this->visitors->month->avgSessionDuration = 0;
@$this->visitors->month->channels->direct->unique = 0;
@$this->visitors->month->channels->direct->views = 0;
@$this->visitors->month->channels->organic->unique = 0;
@$this->visitors->month->channels->organic->views = 0;
@$this->visitors->month->channels->referal->unique = 0;
@$this->visitors->month->channels->referal->views = 0;
@$this->visitors->month->channels->social->unique = 0;
@$this->visitors->month->channels->social->views = 0;
}
if (isset($this->searchResults->rows) and count($this->searchResults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->searchResults->rows;
@$this->visitors->searchVisits->today = $rows;
// Print the results.
} else {
@$this->visitors->searchVisits->today = NULL;
}
if (isset($this->searchWeekResults->rows) and count($this->searchWeekResults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->searchWeekResults->rows;
@$this->visitors->searchVisits->week = $rows;
// Print the results.
} else {
@$this->visitors->searchVisits->week = NULL;
}
if (isset($this->searchMonthResults->rows) and count($this->searchMonthResults->rows) > 0) {
// Get the entry for the first entry in the first row.
$rows = $this->searchMonthResults->rows;
@$this->visitors->searchVisits->month = $rows;
// Print the results.
} else {
@$this->visitors->searchVisits->month = NULL;
}
}
}