0

I have added a custom url to the widget image in the new WP 4.9. Now I want to add an onclick event to the link.

The a tag looks like this

<a href="http://domain.com/this-is-a-page" class="" rel="" target=""></a>

And I want to have it like this

<a href="http://domain.com/this-is-a-page" onclick="ga('send', 'event', 'Sidebar Image', 'Promo Image', 'Clicked');"></a>

How can I do this?

0

I would use the Google Tag Manager firing options:

Or

Old school jQuery solution:

 function($){
      $('.widget_class').click(function() {
          ga('send', 'event', 'Sidebar Image', 'Promo Image', 'Clicked');
       });
   })(jQuery);

Now you just add a class name to your links.

0

Each widget do have their own unique IDs assigned to them. You may use jQuery to access that particular widget box with respective ID and perform the task accordingly.

Additional note: If your widget don't have any unique id then go to your functions.php and modify the code of widget declaration. Here is the WP codex link for your reference https://codex.wordpress.org/Sidebars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.