The Tech Lead at my office is swearing up and down that the following is possible but doesn't know how specifically. My research is coming up empty but the outcome he's looking for is button clicks (not anchors) to be tracked automatically in Google Analytics without assigning a function to each button.
I've been in the school of thought that best practice for semantic HTML structure is that links (anchors) are used to go to new pages, while buttons (and other inputs) are used to change elements on an existing page (with a few exceptions). Link clicks are inherently registered as PageViews in Google Analytics without the use of additional JavaScript for each anchor; an undisputed fact. We know the user is going to a new page; that's why the anchor tag is used being semantically correct.
Buttons are not (to my knowledge, unless someone here shows me otherwise) tracked without pushing the event via a JavaScript function such as:
ga('send', 'event', 'Button Click', 'Open Menu');
Now using JavaScript (or in this case, jQuery) I could globally track every button click:
$('body').on('click', 'button', function(e){
e.preventDefault();
ga('send', 'event', 'Button Click', 'Open Menu');
});
But that's a ton of potentially meaningless data. I would then have to first set variables to read for every button so that the data sent was meaningful. For example:
$('body').on('click', 'button', function(e){
e.preventDefault();
//check if the data-event attribute was set
if($(this).data('event')){
ga('send', 'event', 'Button Click', $(this).data('event'));
};
});
But still he's adamant that there's something in my JavaScript code that is blocking buttons from inherently sending clicks where I shouldn't need to do all this extra work. Originally, I ended all button events with return false; which I later read was not a best practice when tracking, so all of that was removed. There was still no change. Is there anything else I should be checking for to get buttons to register without JavaScript or is this impossible as I originally suspect?