Search code examples
javascripthtmljquerymousedownonmousedown

"mousedown" is defaulting to click for both right click and middle click


I have a script I wrote to track conversions for Google Ads, and I've written it to execute whenever a specific href is clicked. For some reason though, when I click the link with any button, it just treats it as a left mouse click.

Some other users who have visited the site for the first time claim that it's working as intended, but that's not the case for me or anyone else on my team.

Here is the script below:

jQuery(document).ready(function ($) {
    $("[href='https://website.com']").on("mousedown", function () {
        console.log("The JS is working");
        return gtag_report_conversion('https://website.com');
        });
    });

Solution

  • So it turns out that the return part automatically launches the website URL and so there's no easy way to track conversions for middle and right clicks.

    I spoke with my manager and he said it was fine to just track left clicks, and so I followed @Gil's advice and changed the "mousedown" to a "click".