나는PHP에서 Google Analytics 실시간 active 사용자를 가져 오는 방법은 무엇입니까?
$client->setApplicationName("Analytics");
$client->setDeveloperKey("key");
$service = new Google_Service_Analytics($client);
$optParams = array(
'dimensions' => 'rt:medium');
try {
$results = $service->data_realtime->get(
'ga:profileid',
'rt:activeUsers',
$optParams);
// Success.
} catch (apiServiceException $e) {
// Handle API service exceptions.
$error = $e->getMessage();
}
print_r($results);
그것은 오류 (401)를 보여줍니다 ... https://github.com/google/google-api-php-client 에서 API 파일을 다운로드하고 코드를 아래에 노력 로그인 필요 '내가 https://console.developers.google.com/project/apps~rational-photon-578/apiui/api/analytics/method/analytics.data.realtime.get
I에서 통화를 시도
필요한 실시간 사용자가 profileid에 속해 있습니다.
그럼 어떻게 로그인 절차를 구현할 수 있습니까? (Google 사이트를 방문하지 않고도)이 작업을 수행 할 수 있습니까? 나는 아래의 코드를 시도
...
여기$client_id = 'xxxxxxxxxxxxxxx';
$service_account_name = '[email protected]';
$key_file_location = 'location of .p12 keyfile';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
echo $_SESSION['service_token'];
$service = new Google_Service_Analytics($client);
$optParams = array(
'dimensions' => 'rt:medium');
try {
$results = $service->data_realtime->get(
'ga:profileid',
'rt:activeUsers',
$optParams);
// Success.
} catch (apiServiceException $e) {
// Handle API service exceptions.
$error = $e->getMessage();
}
print_r($results);
내가 access_token과 및 세부 사항을 얻고있다 ...하지만 분석 데이터를 가져 오는 오류 때문에 "가 부족합니다 권한을"실패
은 내가 가진 결과는 내가 언급 한 프로필 ID와 Google 콘솔에서 테스트를 만들 때.
안녕하세요 덕분에 .. 정말 많이 도움이 :) 지금 내가 오류 "가 부족합니다 무엇입니까 실시간 사용자에게 액세스 할 수있는 "권한"이 있어야합니다. 아래 코드를 시도했습니다. 'code' $ optParams = array ( 'dimensions'=> 'rt : medium'); 시도 { $ 결과 = $ 서비스 -> data_realtime-> GET ( 'GA : 123123123가' 이 무엇을 실온 : activeUsers ', $ optParams를); // 성공. } catch (apiServiceException $ e) { // API 서비스 예외를 처리합니다. $ error = $ e-> getMessage(); } 'code' – jpk
실시간 API는 여전히 베타 버전이므로 액세스 권한을 요청해야합니다. Google에 지금 당장 PC에 액세스 할 수 없습니다. 일반적으로 액세스하려면 24 시간이 걸립니다. – DaImTo
서비스 계정에서 가져온 이메일 주소를 analytics 사용자 계정에 추가하고 사용 권한을 부여했습니다. 이메일 주소에 액세스하는 데 24 시간이 걸립니다. – jpk