2014-01-10 4 views
0

Google 애널리틱스 목표 추적을 사용하지 않는 것에 대해 많은 의문점이 있음을 알고 있습니다. 나는 나의 질문을 게시하기 전에 숙제를하고 그들 중 많은 것을 읽었다. https://developers.google.com/analytics/devguides/collection/analyticsjs/eventsGoogle 애널리틱스 목표 추적 안됨

나는 모두를 통해 코드를 구현하려

<script> 
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 

    ga('create', 'UA-xxxxxxxxxxx-x', 'mywebsite.com'); 
    ga('send', 'pageview'); 

</script> 

analytics.js 스 버전을 사용하고 있습니다 :

여기 이것은 내가 내 코드를 작성하는 데 사용되는 문서입니다 ... 내 문제입니다 순수한 자바 스크립트와 jQuery가 있지만 둘 다 작동하지 않는 것 같습니다.

링크를 추적 할 수있는 :

<a href="/contact-us-fivefive/" id="cta-footer-btn">Get In Touch Today</a> 

의 jQuery 방법 :

<script>  
var downloadLink = document.getElementById('cta-footer-btn'); 
addListener(downloadLink, 'click', function() { 
ga('send', 'event', 'CTA_footer', 'contact_footer'); 
});         
/** 
* Utility to wrap the different behaviors between W3C-compliant browsers 
* and IE when adding event handlers. 
* 
* @param {Object} element Object on which to attach the event listener. 
* @param {string} type A string representing the event type to listen for 
*  (e.g. load, click, etc.). 
* @param {function()} callback The function that receives the notification. 
*/ 

function addListener(element, type, callback) { 
if (element.addEventListener) element.addEventListener(type, callback); 
else if (element.attachEvent) element.attachEvent('on' + type, callback); 
} 
</script> 

을 둘 것 :

<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>        
<script type="text/javascript"> 
$(document).ready(function() { 
$('#cta-footer-btn').on('click', function() { 
ga('send', 'event', 'CTA_footer', 'contact_footer'); 
}); 
}); 
</script> 

가 나는 또한 JS 순수한 접근 방식은 제시되지 시도 다음은 내 코드입니다 링크 클릭 수를 추적하십시오.

이것이 작동하지 않는 이유나 제안은 크게 감사하겠습니다.

감사합니다.

답변

1

분석 추적 코드가 문서의 헤드 섹션에 삽입되어 있어야합니다 (필요하지 않지만 ga() 함수에 대한 참조를 만들기 전에 필요한 라이브러리가 ga 객체와 함께 완전히로드되는지 확인해야합니다). 또한 jQuery가 사용되기 전에로드되는지 확인하십시오. 브라우저 (일반적으로 브라우저에서 F12)를 사용하여 오류가 있는지 확인할 수 있습니다. 이는 트래킹이 제대로 설치되었는지 확인하는 일반적인 단계입니다.

방금 ​​설치 한 경우 데이터가 보고서에 나타나기까지 시간이 오래 걸릴 수 있으므로 걱정하지 마십시오. 추적 신호가 올바르게 전송되는지 확인하려면 GADebug extension for Chrome을 설치할 수 있습니다.

또한 jQuery의 click() 함수를 사용할 수 있습니다. 그것은 나를 위해 잘 작동합니다.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"> 
</script>        
<script type="text/javascript"> 
$(document).ready(function() { 
$('#cta-footer-btn').click(function() { 
ga('send', 'event', 'CTA_footer', 'contact_footer'); 
}); 
}); 
</script> 

희망 하시겠습니까? :)

+0

초기 페이지가로드 된 후 언젠가 # cta-footer-btn이 변경되는 경우 (내용은 아니지만 ID가있는 새 요소가 생성 된 경우), 함수를 클릭에 다시 바인딩해야합니다 id가있는 요소의 이벤트이거나 새 요소에 대해 작동하지 않습니다. – artfuldev

+0

모두 잘되고 잘 작동합니다. 대단히 감사합니다. 겐신! :) – BustedSanta

관련 문제