我的google分析跟踪事件以ajax形式出了什么问题?

时间:2014-02-22 14:26:38

标签: ajax google-analytics

我尝试使用Google Analytics(分析)在我的网站上跟踪ajax表单。 但它没有任何想法?

在我的脑子里,我有:

    <script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-11111111-1']);
  _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);
  })();

</script>

在我的js文件()

$('form :submit').click(function(e){
        e.preventDefault();
        e.stopPropagation();
        var retourtest = testform();
        if(retourtest==true) {

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-11111111-1']); // your ID/profile
            _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);
            })();

            _gaq.push(['_trackEvent', 'spada', 'contactus']);

另外,我附上了谷歌分析配置的视图 http://spadatest.be/stack/2014-02-22.png

提前致谢!

2 个答案:

答案 0 :(得分:0)

尝试将_gaq.push(['_trackPageview', '/merci.html']);放入request.done来电。

$('form :submit').click(function(e){
    e.preventDefault();
    e.stopPropagation();
    var retourtest = testform();
    if(retourtest==true) {          
        $('#validateoutput').show();
        // $('#validateoutput').html('Your message has been sent!');
        $('#validateoutput').removeClass('ko').addClass('ok');
        var request = $.ajax({
            url: "sendcontactform.php",
            type: "POST",
            data: { sendvar : $('#submitinput').val(),name: $('#nameinput').val(),email: $('#emailinput').val() , phone: $('#phoneinput').val(), comment: $('#commentinput').val() },
            dataType: "html"
        });
        request.done(function( msg ) {
            console.log('msg='+msg);
            _gaq.push(['_trackPageview', '/merci.html']);
            $( "#validateoutput" ).html( msg );
            $('#nameinput').val('');
            $('#emailinput').val('');
            $('#phoneinput').val('');
            $('#commentinput').val('');
        });
        request.fail(function( jqXHR, textStatus ) {
            alert( "Request failed: " + textStatus );
        });
    }
});

答案 1 :(得分:0)

_gaq.push(['_trackEvent', 'spada', 'contactus']); ? 

尝试:

_gaq.push(['_trackEvent', 'spada', 'contactus', 'label']);

标签替换为另一个字词......