최신판 |
당신의 편집 |
2번째 줄: |
2번째 줄: |
| ;구글 Analytics API 시작하기 | | ;구글 Analytics API 시작하기 |
| ;구글 애널리틱스 API 튜토리얼 | | ;구글 애널리틱스 API 튜토리얼 |
| ;HelloAnalytics
| |
|
| |
|
| ==목적 / 목표== | | ==목적 / 목표== |
13번째 줄: |
12번째 줄: |
| *[[구글 API 시작하기]] | | *[[구글 API 시작하기]] |
|
| |
|
| ==API 활성화== | | ==본작업== |
| *https://console.developers.google.com/project 접속
| |
| *API Project 클릭
| |
| *왼쪽 메뉴(Sidebar)에서 APIs 클릭
| |
| *Analytics API 클릭
| |
| *[Enable API] 버튼 클릭
| |
| :"Enabled Analytics API"
| |
| | |
| ==P12 키 생성·업로드==
| |
| *왼쪽 메뉴(Sidebar)에서 Credentials 클릭
| |
| *[Generate new P12 key] 버튼 클릭
| |
| :API Project-7bq7391w37g9.p12 파일 다운로드됨
| |
| *파일명을 client_secrets.p12으로 변경
| |
| *서버에 업로드
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# ll *.p12
| |
| -rw-r--r-- 1 root root 1732 Jun 22 22:29 client_secrets.p12
| |
| </source>
| |
| | |
| ==예제 다운로드==
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# wget https://developers.google.com/analytics/resources/samples/service-php.txt
| |
| --2015-06-22 23:07:25-- https://developers.google.com/analytics/resources/samples/service-php.txt
| |
| Resolving developers.google.com... 58.123.102.118, 58.123.102.98, 58.123.102.119, ...
| |
| Connecting to developers.google.com|58.123.102.118|:443... connected.
| |
| HTTP request sent, awaiting response... 200 OK
| |
| Length: 3353 (3.3K) [text/plain]
| |
| Saving to: “service-php.txt”
| |
| | |
| 100%[================================================>] 3,353 --.-K/s in 0s
| |
| | |
| 2015-06-22 23:07:26 (12.4 MB/s) - “service-php.txt” saved [3353/3353]
| |
| </source>
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# head service-php.txt
| |
| <?php
| |
| | |
| function getService()
| |
| {
| |
| // Creates and returns the Analytics service object.
| |
| | |
| // Load the Google API PHP Client Library.
| |
| require_once 'google-api-php-client/src/Google/autoload.php';
| |
| | |
| // Use the developers console and replace the values with your
| |
| </source>
| |
| | |
| ==예제 복제·수정==
| |
| * service-php.txt를 HelloAnalytics.php로 복사
| |
| * 라이브러리 위치 수정
| |
| * API용 구글 이메일, p12 파일명 입력
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# cp service-php.txt HelloAnalytics.php
| |
| [root@zetawiki ~]# vi HelloAnalytics.php
| |
| </source>
| |
| <source lang='php'>
| |
| <?php
| |
| | |
| function getService()
| |
| {
| |
| // Creates and returns the Analytics service object.
| |
| | |
| // Load the Google API PHP Client Library.
| |
| #### require_once 'google-api-php-client/src/Google/autoload.php';
| |
| require_once 'Google/autoload.php';
| |
| | |
| // Use the developers console and replace the values with your
| |
| // service account email, and relative location of your key file.
| |
| #### $service_account_email = '<Replace with your service account email address.>';
| |
| #### $key_file_location = '<Replace with /path/to/generated/client_secrets.p12>';
| |
| $service_account_email = '871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma.apps.googleusercontent.com';
| |
| $key_file_location = 'client_secrets.p12';
| |
| ... (생략)
| |
| </source>
| |
| | |
| ==테스트 (실패)==
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# php HelloAnalytics.php
| |
| PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/analytics/v3/management/accounts: (403) User does not have any Google Analytics account.' in /usr/share/php/vendor/Google/Http/REST.php:110
| |
| Stack trace:
| |
| #0 /usr/share/php/vendor/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
| |
| ... (생략)
| |
| </source>
| |
| | |
| ==Analytics에 API 이메일 계정 등록==
| |
| *http://www.google.com/analytics/ 접속
| |
| *[로그인] 클릭
| |
| *[관리] 탭 선택
| |
| *[사용자 관리] 메뉴 클릭
| |
| *다음 사용자에게 권한 추가: 871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma.apps.googleusercontent.com<ref>계정 권한: 읽고 분석하기</ref><ref>자신의 구글 API 콘솔 Credentials에 있는 이메일 주소 입력</ref>
| |
| *[추가] 버튼 클릭
| |
| | |
| ==테스트 2 (성공)==
| |
| <source lang='console'>
| |
| [root@zetawiki ~]# php HelloAnalytics.php
| |
| First view (profile) found: zetawiki.com
| |
| Total sessions: 7
| |
| </source>
| |
|
| |
|
| ==같이 보기== | | ==같이 보기== |
116번째 줄: |
18번째 줄: |
| *[[구글 API 시작하기]] | | *[[구글 API 시작하기]] |
|
| |
|
| ==주석==
| | ==참고 자료== |
| <references/>
| | *https://developers.google.com/analytics/solutions/articles/hello-analytics-api Tutorial: Hello Analytics API |
| | | *https://developers.google.com/api-client-library/php/ Google APIs Client Library for PHP |
| ==참고== | |
| *https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php | |
| *http://stackoverflow.com/questions/12837748/analytics-google-api-error-403-user-does-not-have-any-google-analytics-account
| |
| *http://qiita.com/toru-om/items/6e7fb2881e97a542c74a | |
|
| |
|
| [[분류: 구글 API]] | | [[분류: 구글]] |
| [[분류: 튜토리얼]]
| |
| [[분류: 구글 애널리틱스]]
| |