最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Analytics form submit event tracking - Stack Overflow

programmeradmin1浏览0评论

I have a form that submits to another domain I do not own and need to track the event in Google Analytics. I'd rather do it without jQuery to avoid a dependency but I'm failing to understand why this code doesn't work:

<form action='example/search' onsubmit='trackSubmit()' id='frm'>
    <button type='submit'>Search</button>
</form>

<script type='text/javascript'>
function trackSubmit(e) { 

  var bForm = document.getElementById('frm');

    bForm.addEventListener('submit', function(e){
    e.preventDefault();
    _gaq.push('_trackEvent', 'Foobar', 'Foobar Form Submit');
    setTimeout(function(){

        console.log('tracking foobar');
        bForm.submit();

    }, 1000);
  }, false);

}
</script>

I have a form that submits to another domain I do not own and need to track the event in Google Analytics. I'd rather do it without jQuery to avoid a dependency but I'm failing to understand why this code doesn't work:

<form action='example./search' onsubmit='trackSubmit()' id='frm'>
    <button type='submit'>Search</button>
</form>

<script type='text/javascript'>
function trackSubmit(e) { 

  var bForm = document.getElementById('frm');

    bForm.addEventListener('submit', function(e){
    e.preventDefault();
    _gaq.push('_trackEvent', 'Foobar', 'Foobar Form Submit');
    setTimeout(function(){

        console.log('tracking foobar');
        bForm.submit();

    }, 1000);
  }, false);

}
</script>
Share Improve this question edited Jan 9, 2020 at 19:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 18, 2013 at 20:13 LorenzoLorenzo 4,66811 gold badges47 silver badges54 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

onsubmit='trackSubmit()' and bForm.addEventListener('submit', function(e){

This seems to be the wrong part. If you call trackSubmit() on submit, then adding a listener inside this function is useless, it doesn't even fire. I believe it should be just:

<form action="http://example./search" onsubmit="return trackSubmit()" id="frm">
    <button type="submit">Search</button>
</form>

<script type="text/javascript">
function trackSubmit() {
    var frm = document.getElementById('frm');

    _gaq.push('_trackEvent', 'Foobar', 'Foobar Form Submit');
    setTimeout(function(){

        console.log('tracking foobar');
        frm.submit();

    }, 1000);

    return false;

}
</script>

This will be reusable for multiple submission tracking.

<script type="text/javascript">
    function trackSubmissions(form, category, name, value) {
        try {
            _gaq.push(['_trackEvent', category, name, value]);
        } catch(err){}
        setTimeout(function() {
            form.submit();
        }, 100);
    }
</script>

<form onsubmit="trackSubmissions(this, 'category', 'name', 'value'); return false;">
</form>

For the users who have stumbled to this question and are using new version of google universal analytics. This is the code for submit button and link click

<script type='text/javascript'>
        function trackSubmit() { 


        ga('send', 'event', 'button', 'click', 'searchsubmit', 4);
          alert('testing send event')
        }

        function trackClick() { 
            ga('send', 'event', 'button', 'click', 'This is coool');
        }
    </script>

    <body>
    Pushing data.......

        <form onsubmit='trackSubmit()' id='frm'>
            <button type='submit' id= 'searchsubmit'>Search</button>

            </br>
            <a href='http://cool.' onclick="trackClick(); return false;">cool</a>
        </form>

    </body>

I have put an alert to display that the events are being fired because when the page submits and you will look at console in debugger, your event will not be visible.

发布评论

评论列表(0)

  1. 暂无评论