티스토리 뷰

발생일: 2015.01.16

키워드: Google Analytics, 구글 애널리틱스

문제:

구글 애널리틱스를 살펴보다가, 코드에 재밌는 부분이 있어 메모해둔다.ㅎㅎ


해결책:

애널리틱스 코드를 보면서 `script` 문자열을 왜 파라미터로 전달할까… 라고 생각했었는데,
풀어놓은 코드의 주석을 보니 변수 선언에 대한 바이트 절약 때문이란다.

오오… 최적화를 많이 고민했구나.


/**
 * Creates a temporary global ga object and loads analy  tics.js.
 * Paramenters o, a, and m are all used internally.  They could have been declared using 'var',
 * instead they are declared as parameters to save 4 bytes ('var ').
 *
 * @param {Window}      i The global context object.
 * @param {Document}    s The DOM document object.
 * @param {string}      o Must be 'script'.
 * @param {string}      g URL of the analytics.js script. Inherits protocol from page.
 * @param {string}      r Global name of analytics object.  Defaults to 'ga'.
 * @param {DOMElement?} a Async script tag.
 * @param {DOMElement?} m First script tag in document.
 */
(function(i, s, o, g, r, a, m){
  … 중략
  <script> 태그를 만들어서 비동기로 로드
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');


반응형
댓글
공지사항