I am trying to get real time users from google analytics API
I am following this : https://developers.google.com/analytics/devguides/reporting/core/v4/basics
I am posting the data like:
$url = 'https://analyticsreporting.googleapis.com/v4/reports:batchGet';
//Initiate cURL.
$ch = curl_init($url);
$jsonDataEncoded = '{
"reportRequests":
[
{
"viewId": "109200098",
"dateRanges": [{"startDate": "2014-11-01", "endDate": "2014-11-30"}],
"metrics": [{"expression": "ga:users"}]
}
]
}'
;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
print_r($result);
{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }
I know I should get some OAuth for this but I am not getting how to do this. Can you please help me with this? Thanks!
index.php and oauth2callback.php
same as here : https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-php
Working fine getting session count now i want to use https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get#auth for getting rt:activeUsers
I am editing index.php like :
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Create an authorized analytics service object.
$analytics = new Google_Service_AnalyticsReporting($client);
// Call the Analytics Reporting API V4.
$response = getReport($analytics);
$optParams = array(
'dimensions' => 'rt:medium');
try {
$results = $analytics->data_realtime->get(
'ga:56789',
'rt:activeUsers',
$optParams);
// Success.
} catch (apiServiceException $e) {
// Handle API service exceptions.
$error = $e->getMessage();
}
// Print the response.
printResults($response);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
getting error: Undefined property: Google_Service_AnalyticsReporting::$data_realtime