Attaching a Google event tracking code to a submit button
Hi, I've been asked to add a Google event tracking code to the Submit button on a specific form on my site (there's about ten differing forms in total) and I've no idea where to even begin looking to acheive this.
If I was putting the code on a normal link it would be something like this...
You don't need to use an inline event handler for that.
You should be able to catch the id of the button or it's parent and then be able to register the event on click. If you're using jquery in your project (Contour relies on it for validating form fields at least) then you should be able to write something like...
I saw you are solving event tracking issues. Have similar issue with my event tracking code. I am not a tech gig.. just trying hard to learn things. Googling "event tracking code for button", I found lots of tutorials and had implemented the code. I am not sure, its gonna work, as I can not see any "real-time" events being recorded on GA while clicking on the button.
The website is in wordpress. Earlier the code for button was
Yes it does (sort of!) how would I target the specific submit button though (the jQuery would need to be on the master template and therefore it would render on all pages). From examining the markup the HTML looks like this for the submit button:
Then you should be able to target using $('.contourNavigation > input') or like $('input[type=submit]').
But this will capture clicks on all forms not just the one specific form. So if the form is wrapped on a page or in a context where there is a unique id somewhere then I would build my selector up based on that id.
Thanks again for your time, I'm having trouble (!) targeting the actual button. I've wrapped the entire contour form in a div with a unique ID (contourTracking) but I can't seem to access it. The script I've written is
But seems like you're missing a dot when targeting the .contourNavigation class. And when using the > the element you're targeting must be a direct child, so it requires that you structure looks like this
Happy to hear that :-) Whenever you face an issue where you see someone adding inline eventhandlers then remember that it's possible to avoid that by using either vanilla javascript or jquery as you just did. It makes things easier to deal with in the end :)
Attaching a Google event tracking code to a submit button
Hi, I've been asked to add a Google event tracking code to the Submit button on a specific form on my site (there's about ten differing forms in total) and I've no idea where to even begin looking to acheive this.
If I was putting the code on a normal link it would be something like this...
<a onClick="ga('send','event','Go','Course Finder');" href="/myotherpage">Submit</a>
Could anyone point me in the right direction or even if what I'm trying to acheive is possible please?
Thanks,
Craig
Hi Craig
You don't need to use an inline event handler for that.
You should be able to catch the id of the button or it's parent and then be able to register the event on click. If you're using jquery in your project (Contour relies on it for validating form fields at least) then you should be able to write something like...
$('#idonbtn').on('click', function(){
ga('send','event','Go','Course Finder');
});
Does that make sense?
Hi Jan,
I saw you are solving event tracking issues. Have similar issue with my event tracking code. I am not a tech gig.. just trying hard to learn things. Googling "event tracking code for button", I found lots of tutorials and had implemented the code. I am not sure, its gonna work, as I can not see any "real-time" events being recorded on GA while clicking on the button.
The website is in wordpress. Earlier the code for button was
I had updated the code with event tracking as:
Please help me, where I did mistake..
Thanks.
Hi Jan,
Yes it does (sort of!) how would I target the specific submit button though (the jQuery would need to be on the master template and therefore it would render on all pages). From examining the markup the HTML looks like this for the submit button:
Thanks,
Craig
Hi Craig
Then you should be able to target using $('.contourNavigation > input') or like $('input[type=submit]').
But this will capture clicks on all forms not just the one specific form. So if the form is wrapped on a page or in a context where there is a unique id somewhere then I would build my selector up based on that id.
Does this still make sense?
/Jan
Hi jan,
Thanks again for your time, I'm having trouble (!) targeting the actual button. I've wrapped the entire contour form in a div with a unique ID (contourTracking) but I can't seem to access it. The script I've written is
<script>
$('#contourTracking > contourNavigation > input').on('click', function(){
alert("Hello");
});
</script>
Am I just being a general idiot?!?!
Hi Craig
I don't think you're being an idiot :)
But seems like you're missing a dot when targeting the .contourNavigation class. And when using the > the element you're targeting must be a direct child, so it requires that you structure looks like this
If it does that then using this should work (Just corrected your typo from above
If your structure is different then just use this without the > selector.
I Hope this helps.
/Jan
Genius!
Works a treat!
Thanks jan :)
Hi Craig
Happy to hear that :-) Whenever you face an issue where you see someone adding inline eventhandlers then remember that it's possible to avoid that by using either vanilla javascript or jquery as you just did. It makes things easier to deal with in the end :)
/Jan
Absolutely!
Never even occurred to me till you mentioned.
is working on a reply...