Zadejte hledaný výraz...

Google Analytics API Dashboard OAuth 2.0

kofy
verified
rating uzivatele
(11 hodnocení)
3. 11. 2016 08:52:42
Zdravím,
mám tu menší problém s google analytics. Zkouším se hodit nějaké Dashboardy na svůj web. Bez nutnosti aby uživatel se musel přihlásit atd....
Nemohu se dostat přes tuto chybu
Můj index.php vypadá takto:
ini_set("display_errors", 1);
error_reporting(E_ERROR | E_WARNING);
require_once 'google-api-php-client/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION) && $_SESSION) {
// Set the access token on the client.
$client->setAccessToken($_SESSION);
// Create an authorized analytics service object.
$analytics = new Google_Service_AnalyticsReporting($client);
// Call the Analytics Reporting API V4.
$response = getReport($analytics);
// Print the response.
printResults($response);
} else {
$redirect_uri = 'http://' . $_SERVER . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
function getReport($analytics) {
// Replace with your view ID. E.g., XXXX.
$VIEW_ID = "UA-66627096-2";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
return $analytics->reports->batchGet($body);
}
function printResults($reports) {
for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
$report = $reports;
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows;
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders . ": " . $dimensions . "n");
}
for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
$entry = $metricHeaders;
$values = $metrics;
print("Metric type: " . $entry->getType() . "n");
for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
$value = $values->getValues();
print($entry->getName() . ": " . $value . "n");
}
}
}
}
}
A oauth2callback
require_once './google-api-php-client/vendor/autoload.php';
session_start();
// Create the client object and set the authorization configuration
// from the client_secrets.json you downloaded from the Developers Console.
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER . '/oauth2callback.php');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// Handle authorization flow from the server.
if (! isset($_GET)) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET);
$_SESSION = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Nevíte někdo jak to rozchodím ? Zkoušel jsem snad vše ( je to kód přímo z googlu )
3. 11. 2016 08:52:42
https://webtrh.cz/diskuse/google-analytics-api-dashboard-oauth-2-0#reply1234724
node
verified
rating uzivatele
(5 hodnocení)
3. 11. 2016 09:42:55
Ty pouzivas: a ocividne to nestaci. Tu su vsetky mozne "povolenia":
3. 11. 2016 09:42:55
https://webtrh.cz/diskuse/google-analytics-api-dashboard-oauth-2-0#reply1234723
Pro odpověď se přihlašte.
Přihlásit