Track event on a button click which redirects to a web page


I have integrated Piwik in my web site, where I want to track successful and unsuccessful logins.
The successful login will redirect the user to Home page, where as on unsuccessful it will redirect to Login Page.
Now, If I am trying to send request to Piwik, but mean while it redirects the page to Home /Login.

Can anyone tell me, how I can handle this situation ?