Целевой бланк (откроется в новом окне) для отслеживания кликов в Google Analytics - PullRequest
0 голосов
/ 23 января 2020

Этот код успешно отслеживает клики в Google Analytics. Однако, несмотря на то, что target = "_ blank" в ссылке, она не открывается в новом окне или вкладке.

Существует ли простая настройка скрипта GA, который открывается в отдельном окне или вкладке?

(Я только что заметил, что аналогичный вопрос задавался еще в 2014 году, когда предлагалось удалить обратный вызов (hitCallback) в js; надеюсь, что ответ fre sh есть?)

В шапке :

<script>
/**
* Function that captures a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var captureOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>   

В содержании:

<a target="_blank" href="https://example.com" onclick="captureOutboundLink('https://example.com'); return false;">Test for click to Example</a>

1 Ответ

0 голосов
/ 30 января 2020

Изменить 'hitCallback': function(){document.location = url;}
на 'hitCallback': function(){window.open(url);}
например

<script>
/**
* Function that captures a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var captureOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){window.open(url);}
   });
}
</script>  
...