2

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.com/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>
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Apr 18, 2013 at 20:13
0

3 Answers 3

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.com/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>
answered Apr 18, 2013 at 20:26
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jakub, it looks ok, but I still cannot see the event being registered in Google Analytics. Not sure if there's some kind of delay before it shows up.
Check out Other question, there is an answer with GA native callback function to avoid setTimeout practice, which might be the problem. Also, speaking from my own experience, did you check today's stats? :D
1

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>
answered Oct 24, 2013 at 10:56

Comments

0

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.com' 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.

enter image description here

answered Apr 30, 2014 at 18:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.