How can I track a button click or pop-up with JavaScript?

Companies often use forms that are embedded in their page or create pop-ups where website visitors can subscribe to their newsletter. These forms or pop-ups are not tracked by Leadinfo because the URL does not change. But no need to worry! If you follow the below steps, you can still track companies that submitted a form or subscribed to a newsletter.


But before you start, you need to install the tracking code on your website and have access to the source code of your website!

How can I track a button click or pop-up with JavaScript?

Simply add a short script to the event you want to track on your website. Here's how it works.


Add the below script to the event you want to track. For example, if you want to track how which companies clicked on the login button, but you do not want to track them in the logged-in environment, you need to add the below script to the code of the button.


When a website visitor clicks on the website, Leadinfo will register the click as a page view. The page view will be shown as '/login'. You can easily adjust the register page view by replacing '/login' with '/login-button'. The page view will then be shown as '/login-button'.


Login example:

<script>
if(window.leadinfo) {
window.leadinfo('setCustomUrl', '/login');
window.leadinfo("trackPageView");
}
</script>


If you have a webshop, you might want to track the button "Add to cart". Simply adjust the script by changing the custom URL to '/add-to-cart'. Every time a website visitor clicks on the button, it will be shown in Leadinfo as a page view.


Add to cart example:

<script>
if(window.leadinfo) {
window.leadinfo('setCustomUrl', '/add-to-cart');
window.leadinfo("trackPageView");
}
</script>