Yandex.Webvisor Setup on 1C-Bitrix
Webvisor doesn't work on HTTPS site with incorrect CSP headers — session recording via Yandex iframe, browser blocks it if Content-Security-Policy doesn't allow frame-ancestors. Second common cause: Metrica tag inserted via Google Tag Manager, GTM loads asynchronously, webvisor initializes after user made first actions — session start lost.
Correct Metrica Tag Placement
Yandex requires tag as high as possible in <head>. In Bitrix, standard way — insert via OnBeforeProlog event in init.php or directly in prologue file /bitrix/templates/TEMPLATE/header.php.
In /bitrix/php_interface/init.php:
AddEventHandler('main', 'OnBeforeProlog', function() {
ob_start();
?>
<!-- Yandex.Metrica -->
<script>
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
var z = m[i],d=e.createElement(t),n=e.getElementsByTagName(t)[0];
d.async=1;d.src=r;d.id='metrika-';
n.parentNode.insertBefore(d,n);
z(i,'init', { id: XXXXXXXX, webvisor: true, clickmap: true, trackLinks: true });
})(window, document, 'script', 'https://cdn.jsdelivr.net/npm/yandex-metrika@latest/metrika.js', 'ym');
</script>
<!-- /Yandex.Metrica -->
<?php
$html = ob_get_clean();
$GLOBALS['APPLICATION']->AddHeadString($html, true);
});
Or via standard bitrix:metrika component — available in fileman module, install from Marketplace.
Content-Security-Policy and Webvisor iframe
Webvisor records screen via <iframe> on webvisor.com domain. If server (nginx or via PHP headers) has strict CSP, recording won't start — console error Refused to frame ... because an ancestor violates the following Content Security Policy directive.
Minimum directive set for Webvisor:
Content-Security-Policy:
frame-src 'self' https://webvisor.com;
script-src 'self' 'unsafe-inline' https://mc.yandex.ru https://cdn.jsdelivr.net;
img-src 'self' data: https://mc.yandex.ru;
In Bitrix headers can be set in /bitrix/.htaccess or in init.php via header() — but only before first HTML output. If using ob_start() in prologue, not a problem.
Webvisor on SPA/AJAX Pages
Most modern Bitrix templates use AJAX navigation (BX.ajax, bitrix:main.ajaxlinks component). On page transition without reload Webvisor doesn't record URL change — whole visit looks like one page in recording.
Solution: call ym(XXXXXXXX, 'hit', window.location.href) after every AJAX transition. In Bitrix there's BX.addCustomEvent('onAjaxSuccess', ...) event — hook into it:
BX.addCustomEvent('onAjaxSuccess', function(event) {
if (typeof ym !== 'undefined') {
ym(XXXXXXXX, 'hit', window.location.href);
}
});
Without this Webvisor data incorrect: all catalog transitions recorded as single visit on entry page.
Verifying Recording Correctness
After setup — check "Webvisor" section "Monitoring." If sessions recording but video black — mixed content problem (HTTP resources on HTTPS site). If sessions not appearing at all — check Network tab DevTools for requests to mc.yandex.ru/watch/: status 200 means counter works but data not processed yet (up to 20 minutes delay for new sessions).







