toolman Posted June 23, 2015 Share Posted June 23, 2015 Hi there, I have the following code which I would like to use to track click events: <script type="text/javascript"> function trackOutboundLink(link, category, action) { try { dataLayer.push({'event':'interaction','eventCategory': category,'eventAction':action,'eventLabel': ''}); } catch(err){} setTimeout(function() { document.location.href = link.href; }, 100); } </script> <input type="submit" name="submit" value="Submit" class="submit-button" onClick="trackOutboundLink(this, 'Contact', 'Signup', 'New Signup');" /> However, when the form is submitted, it causes an error by going to a dynamically generated URL. I've been told this is because the setTimeout function is looking for a link, not a button. I have removed the setTimout and the form submits properly. Is there a way I can have the setTimeout function to use an input instead of a link? Or if I just remove the function, will the event still be tracked? Thanks! Link to comment https://forums.phpfreaks.com/topic/296981-submit-button-issue-with-event-tracking/ Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2015 Share Posted June 23, 2015 You should be coding temporal functions to use callbacks. If you're not familiar/confident with callbacks have a look here and here (the first one is easier on the eyes, but I find the second uses better language). Link to comment https://forums.phpfreaks.com/topic/296981-submit-button-issue-with-event-tracking/#findComment-1514691 Share on other sites More sharing options...
CroNiX Posted June 23, 2015 Share Posted June 23, 2015 You're looking for an href here (in the timeout): document.location.href = link.href; <inputs> don't have an href attribute. Here's the input you're passing to your js function: <input type="submit" name="submit" value="Submit" class="submit-button" onClick="trackOutboundLink(this, 'Contact', 'Signup', 'New Signup');" /> Link to comment https://forums.phpfreaks.com/topic/296981-submit-button-issue-with-event-tracking/#findComment-1514762 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.