//--------------------------------------------------------------------
// GA. measurement protocol
//--------------------------------------------------------------------
구글에서 제공하는 js, sdk 를 사용하지 않고
직접 구글서버에 접속하여 ga 데이타를 보낼수 있는 프로토콜.
GET, POST 둘다 가능함

* 실제 url : http://www.google-analytics.com/메소드명
* 디버깅 url : https://www.google-analytics.com/debug/메소드명
measurement protocol 은 아무런 리턴코드를 주지 않는다.
그래서 디버깅url 을 이용해서 오류가 없는지 확인후, 실제url 로 바꿔서 적용해야 함.

* 메소드명
- collect : Hit 데이타 한건을 보낸다.
- batch : Hit 데이타 여러건을 보낸다.
           hit 의 최대건수는 20개.
           hit합계의 최대 사이즈는 16Kbyte.
           단일 hit의 최대 사이즈는 8Kbyte.
나는 collect 만 사용하였음.

* 파라메터들
v=1 // 이건 항상 1 이다.
tid=XXXXX // 추적ID, GA사이트에서 속성에 부여되어 있음.
cid=XXXXX // 클라이언트ID, 개발자맘대로 부여하면 됨.
t=XXXXX // Hit타입 (pageview, event...)
uip=XXX.XXX.XXX.XXX // 클라이언트IP (선택사항)
ul=XXXXX // 언어코드-국가코드 (kr-ko, en-us...) (선택사항)
ua=XXXXX // User Agent (선택사항)
dh=abc.com // 도메인 (선택사항)
dp=XXXXX // 페이지주소 (선택사항)
dt=XXXXX // 페이지제목 (선택사항)


//--------------------------------------------------------------------
// php 소스
//--------------------------------------------------------------------
/**
 * GA (Google Analytics) 에 데이타 전송하기
 * leesw. 2016.03.09
 */
protected function logGA()
{
  $class_name = get_class($this);
  $func_name = $this->uri->segment(3);

  // 필요한 데이타 조회
  $sql = "...";

  // 데이타 설정
  $ga_t   = "pageview";
  $ga_cid = $device_id;                                  // client ID. 개발자맘대로 넣으면 됨
  $ga_uip = $_SERVER['REMOTE_ADDR'];                     // client IP
  $ga_dp = "/".$class_name."/".$func_name;               // page url
  $ga_ul = $language_code."-".$location_code;            // 언어코드-국가코드
  $ga_tid = "XXXXX";                                     // ga 추적ID

/*
  // GA 설정. GET 방식
  $ga_url = "http://www.google-analytics.com/collect?v=1&tid=[[TID]]&cid=[[CID]]&t=[[T]]&dp=[[DP]]&uip=[[UIP]]&ul=[[UL]]";
  $ga_url = str_replace("[[TID]]", $ga_tid, $ga_url);
  $ga_url = str_replace("[[CID]]", $ga_cid, $ga_url);
  $ga_url = str_replace("[[T]]",   $ga_t,   $ga_url);
  $ga_url = str_replace("[[DP]]",  $ga_dp,  $ga_url);
  $ga_url = str_replace("[[UIP]]", $ga_uip, $ga_url);
  $ga_url = str_replace("[[UL]]",  $ga_ul,  $ga_url);

  // GA에 데이타 보내기
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $ga_url);
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 리턴값을 자동으로 출력안하도록
  curl_exec($ch);
  curl_close($ch);
*/

  // GA 설정. POST 방식
  $ga_url = "http://www.google-analytics.com/collect";
  $ga_post_data["v"]   = "1";
  $ga_post_data["tid"] = $ga_tid;
  $ga_post_data["cid"] = $ga_cid;
  $ga_post_data["t"]   = $ga_t;
  $ga_post_data["dp"]  = $ga_dp;
  $ga_post_data["uip"] = $ga_uip;
  $ga_post_data["ul"]  = $ga_ul;

  // GA에 데이타 보내기
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $ga_url);
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($ga_post_data));
  curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 리턴값을 자동으로 출력안하도록
  curl_exec($ch);
  curl_close($ch);
}



//--------------------------------------------------------------------
// 참고 링크
//--------------------------------------------------------------------

* 기본 레퍼런스
https://developers.google.com/analytics/devguides/collection/protocol/v1/reference

* 파라메터 리스트
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters

* Hit Builder 페이지
https://ga-dev-tools.appspot.com/hit-builder/



반응형

'잡다한 자료' 카테고리의 다른 글

mysql. 데이타파일. 로그파일 이동  (0) 2016.03.24
[번역] AWS_HIPAA_Compliance_Whitepaper  (0) 2016.03.14
apache. ssl. 인증서  (1) 2016.02.01
Apache. Ubuntu. SSL 설정  (0) 2016.01.06
aws. instance. clone. 복제  (2) 2016.01.05
Posted by 돌비
,