2011-07-05 3 views
3

내 페이지의 헤드 부분의 끝에서 구글 분석 async site tracking script를 사용하는 경우, 모든 것이 예상대로 작동합니다로딩 구글 분석 매개 변수화 비동기 페이지 추적 스크립트

<head> 
    <script type="text/javascript"> 

     var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', 'UA-XXXXX-X']); 
     _gaq.push(['_setDomainName', 'test.com']); 
     _gaq.push(['_trackPageview', '/title=ied&action=fire']); 

     (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); 
     })(); 

    </script> 
</head> 

피들러에서이 볼 수있는 2 개 요청 : 나는 스크립트를 필요로 몇 가지 이유를 들어 enter image description here

enter image description here는 매개 변수화 할, 그래서 사용자 정의 googleAnalytics 함수 내에서 그것을 포장하는 GE의 TS 2 개 매개 변수 :

function googleAnalytics(domain, queryString) { 
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXX-X']); 
    _gaq.push(['_setDomainName', domain]); 
    _gaq.push(['_trackPageview', queryString]); 

    (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); 
    })(); 
} 

Abowe 코드가 GoogleAnalytics.js 파일에 저장되었습니다. 나는 다음과 같은 헤드 섹션의 페이지를로드 :

<head> 
    <script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script> 
    <script type="text/javascript">googleAnalytics('test.com', '/title=ied&action=fire');</script> 
</head> 

하지만 이번에 들러는 1 요청을 보여줍니다

enter image description here

Google Analytics Tracking Code Debugger는 아무것도 보여주지합니다. ga.js 스크립트 만 다운로드되지만 Google 애널리틱스 보고서 데이터를 채우는 다른 요청은 없습니다. 이 접근법에서 무엇이 잘못되었으며 어떻게 해결 될 수 있습니까?

Btw :이 두 매개 변수와 추적 스크립트의 비동기 버전이 필요합니다.

감사합니다.

답변

2
당신의 문제는 당신의 _gaq 선언이 지역이다

것을 범위에 있습니다. ga.js이로드되고 전체 범위에서 _gaq을 찾습니다. (스크립트가 주입되면, 더 이상 기능의 범위에 액세스 할 수 없습니다.)

그것을 해결하려면, 당신은 함수의 var _gaq = _gaq || []; 외부를 배치 할 수 있습니다, 또는 당신이 가진 함수 내에서 교체 할 수 있습니다 :

window._gaq = window._gaq || []; 

이렇게하면 문제가 해결됩니다.

0

나는 이것을 만들었습니다. 다음

GoogleAnalytics.js 외모 :

function googleAnalytics(domain, queryString) {   
    _gaq.push(['_setDomainName', domain]); 
    _gaq.push(['_trackPageview', queryString]); 
} 

헤드 섹션 인 반면, 다음과 같이

<head> 
    <script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script> 
    <script type="text/javascript"> 
     var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', 'UA-XXXXX-X']); 

     (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); 
     })(); 

    </script> 
    <script type="text/javascript">googleAnalytics('com.dev.abb.com','');</script> 
</head> 

안부