2012-05-29 5 views
1

jQuery를 사용하여 웹 페이지에서 다운로드 및 기타 이벤트를 추적하려고합니다. 이것을 디버그하는 것은 어렵습니다. 아래 코드는 현재 작동하지 않습니다. 페이지에 jQuery 라이브러리가로드되어 있고 모든 XXXX가 올바른 정보로 바뀌 었다고 가정합니다. 어떤 아이디어? 매우 감사!jQuery Google Analytics 추적

<script type="text/javascript"> 
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXX']); 
    _gaq.push(['_setDomainName', 'XXXXX.com']); 
    _gaq.push(['_addIgnoredRef', 'XXXXX.com']); 
    _gaq.push(['_trackPageview']); 
    if (jQuery) { 
        jQuery(document).ready(function() { 
            jQuery('a').click(function() { 
                var $a = jQuery(this); 
                var href = ($a.attr('href')) ? $a.attr('href') : ''; 
                if ((href.match(/^http/i)) && (!href.match(document.domain))) { 
                    var category = 'outgoing - XXXX Landing'; 
                    var event = 'click - XXXX Landing'; 
                    _gaq.push(['_trackEvent', category, event, href]); 
                } else { 
                    if (href.match(/.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/i)) { 
                        var category = 'download - XXXX Landing'; 
                        var event = 'click - XXXX Landing'; 
                        _gaq.push(['_trackEvent', category, event, href]); 
                    } else { 
                        if (href.match(/^mailto:/i)) { 
                            var category = 'mailto - XXXX Landing'; 
                            var event = 'click - XXXX Landing'; 
                            _gaq.push(['_trackEvent', category, event, href]); 
                        } 
                    } 
                } 
            }); 
        }); 
    } 

    (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'; 
        if ('http:' == document.location.protocol) { 
            ga.src = 'http://www.google-analytics.com/ga.js'; 
        } else { 
            ga.src = 'https://ssl.google-analytics.com/ga.js'; 
        } 
        var s = document.getElementsByTagName('script')[0]; 
        s.parentNode.insertBefore(ga, s); 
    })(); 
</script> 
+1

너무 끔찍한 형식이므로 부분적으로 디버깅하기가 어렵습니다. 이 코드를 복사하여 붙여 넣은 경우 질문과 실제 코드에서이 문제를 해결해야합니다. – Bojangles

+0

'_gaq.push' 대신'console.log'를 사용하여 코드를 시험해 보았습니다. 정상적으로 작동했습니다. 아마도'_gaq.push ([ '_ setDomainName', 'XXXXX.com']);은 document.domain 이외의 다른 도메인을 포함하고있을 것입니다 (예 : localhost에서 테스트 중입니다). 이러한 불일치로 인해 Google 애널리틱스 추적이 전혀 중단 될 수 있습니다. – CodingHamster

+0

을 디버깅하려면 우선 ** _ gaq.push() ** 호출을 ** console.log() 또는 alert(); **로 바꿔야 jQuery 코드가 제대로 작동하는지 확인해야합니다 – shershen

답변

0

Google 웹 로그 분석은 이벤트 추적이 내장되어

예를 들어 사용자가 파일 다운로드를 추적하려는 경우, 당신은 같은 것을 할 수 -

HTML :.

<a href="sample.pdf" class="download">Click to download.</a> 

JS :

$("a.download").click(function() { 
    _gaq.push(['_trackEvent', 'Files', 'Downloaded']) 
}); 

다음은 Google의 공식 Event Tracking Guide입니다. 이 안내서는 모든 것을 자세히 설명합니다.

0

나는 $ .getScript를 사용하여 GA 코드를 일반 텍스트로 제공하지 않고 DOM에 다운로드하여 적용 할 수 있습니다. 또한 추적 코드, 도메인 등을 설정하는 물건을 실행하기 전에 DOM에 필요합니다. 또는 _gaq는 아무 것도하지 않습니다.

관련 문제