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! Quote Link to comment 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). Quote Link to comment 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');" /> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.