Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@delirehberi
Created December 9, 2018 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delirehberi/e293dcb1eb24391299f9a666026072b4 to your computer and use it in GitHub Desktop.
Save delirehberi/e293dcb1eb24391299f9a666026072b4 to your computer and use it in GitHub Desktop.
i3blocks analytics
#!/usr/bin/php
<?php
$view_id = "";
$token = "";
$key = "";
echo "Realtime: " . request($view_id, $token, $key);
function request($id, $token, $key)
{
$ci = curl_init();
$params = [
"ids" => "ga:" . $id,
"metrics" => "rt:activeUsers",
"key" => $key
];
curl_setopt_array($ci, [
CURLOPT_HTTPHEADER => [
"Host: content.googleapis.com",
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0",
"Accept: */*",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate, br",
"X-ClientDetails: appVersion=5.0%20(X11)&platform=Linux%20x86_64&userAgent=Mozilla%2F5.0%20(X11%3B%20Ubuntu%3B%20Linux%20x86_64%3B%20rv%3A63.0)%20Gecko%2F20100101%20Firefox%2F63.0",
"Authorization: Bearer " . $token,
"X-Requested-With: XMLHttpRequest",
"X-JavaScript-User-Agent: apix/3.0.0 google-api-javascript-client/1.1.0",
"X-Origin: https://explorer.apis.google.com",
"X-Referer: https://explorer.apis.google.com",
"X-Goog-Encode-Response-If-Executable: base64",
"Connection: keep-alive",
"TE: Trailers",
],
CURLOPT_URL => "https://content.googleapis.com/analytics/v3/data/realtime?" . http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
]);
curl_setopt($ci, CURLOPT_ENCODING, "gzip");
$resp = curl_exec($ci);
$result = json_decode($resp, true);
curl_close($ci);
return $result['rows'][0][0];
}
exit(0);
@delirehberi
Copy link
Author

You can fetch token and key from google's api explorer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment