Jump to content

submit button issue with event tracking


toolman

Recommended Posts

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

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');" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.