javascript - Why :target=>"_blank" is not working in my code? -


from google support page https://support.google.com/analytics/answer/1136920?hl=en#ga

i have added below code.

<script> /** * function tracks click on outbound link in google analytics. * function takes valid url string argument, , uses url string * event label. */ var trackoutboundlink = function(url) {    ga('send', 'event', 'outbound', 'click', url, {'hitcallback':      function () {      document.location = url;      }    }); } </script> 

now want modify onclick attribute of image link tracked have used below code

<%= link_to image_tag("banner.png", :alt => "ad"),            "https://www.someexternalsite.com",            :target=>"_blank" ,           :onclick=> "trackoutboundlink('https://www.seomexternalsite.com'); return false;" %> 

i have not checked if tracking working yet, :target=>"_blank" not seem working, link opening on same page. wrong in code?

you have 2 issues:

  1. return false not allow anchor follow link, first fix remove code onclick
  2. in trackoutboundlink telling in callback change current document location new url (the anchor url) remove callback:

    var trackoutboundlink = function(url) { ga('send', 'event', 'outbound', 'click', url); }