2011-12-12 4 views
2

Google 웹 로그 분석 '코드 내 클라이언트 측이 구글 익명 함수를 호출하는 방법구글 웹 로그 분석 자바 스크립트 여기

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-20366831-2']); 
_gaq.push(['_trackPageview']); 

(function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); 

입니까? 익명 함수는 당신이 볼 수 있듯이 ()

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); // <--- The() calls the anonymous code 

로 끝나는 때문에

답변

5

이 그것은라고,이 코드는 기본적으로 브라우저에 의해 실행됩니다 DOM을,로 script 태그를 삽입합니다.

0

스 니펫은 이미 자체 호출합니다.

(function() { 
 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
 
})();
실제로이 유사하다 당신의 페이지에 의 ga.js 포함되어 무엇을

:

<script src="//google-analytics.com/ga.js" />

나머지 _gaq (Google 애널리틱스)에 이벤트를 추가하는 것은 귀하에게 달려 있습니다. tic 큐). 그러면 이벤트가 자동으로 처리됩니다.