How to open an external link using an event handler

Hi all,

I have the following requirement on my wix site: I want to open an external site in a new tab using a button. At the same time, I want to track this as an event in google analytics

The problem is:

  • When I set the onClick event handler on the button for inserting the tracking code, the link and target attribute on the button get ignored
  • wixlocation function “to” does not support setting a target to open a link in a new tab

Is there any way I can achieve this in wix?

Thanks and regards
Felix

I tried it (not the google analytics but other code), and they were both working: it opened the link and executed the onClick event handler, so I’m not sure why it doesn’t work for you.
But anyway, I’d try 2 things (if it doesn’t work for you):

  1. Put the button inside a transparent box and use the box onClick.

  2. To create a button of your own using custom element and set the event handler to run the code + open a new tab.

I have tried workaround 1) but with no luck:
Even if the box overlaying the button is invisible, the button doesn’t work underneath it. So only the onClick eventhandler is firing but the button becomes useless…

Do you have an example code for a custom button? Or even better, an idea why our site is behaving differently than yours?

Maybe you should post your code here.

I don’t think it has anything to do with the site code IMHO. I am experimenting on a playground site, the code is pretty much just an onClick event handler with a console.log debug output

Yet, the only thing we can do is to see maybe there’s an error somewhere in the code.

Well I am happy to share the code here:


$w.onReady(function () {
 // TODO: write your page related code here...

});

export function box1_click(event) {
 // Add your code for this event here: 
    console.log("click!");
}

The design looks as following:


If I hover over the area of the invisible box, the button looses its focus and the link of the button doesn’t work anymore, only the log message shows up in the console.

In case this is working for anyone: Could you post your code and your site layout so I can try and replicate it?