Files
videoreg.ru/web-front/public/metrika.js
T

80 lines
4.0 KiB
JavaScript

/* Videoreg analytics: one existing counter, public hosts only, no query payloads. */
(function () {
"use strict";
if (!/^(www\.)?videoreg\.ru$/.test(window.location.hostname) || window.__videoregMetrika) return;
window.__videoregMetrika = true;
var counter = 109268682;
window.ym = window.ym || function () { (window.ym.a = window.ym.a || []).push(arguments); };
window.ym.l = Number(new Date());
var tag = document.createElement("script");
tag.async = true;
tag.src = "https://mc.yandex.ru/metrika/tag.js";
document.head.appendChild(tag);
window.ym(counter, "init", {
webvisor: false, clickmap: true, trackLinks: true,
accurateTrackBounce: true, trackHash: false
});
function goal(name, params) { window.ym(counter, "reachGoal", name, params); }
var problems = new Set();
function problem(kind) {
if (problems.has(kind)) return;
problems.add(kind);
goal("content_problem", { problem: kind });
}
// Only actual, enabled document/ZIP downloads; a click does not imply success.
document.addEventListener("click", function (event) {
var link = event.target instanceof Element ? event.target.closest("a[href]") : null;
if (!link || event.defaultPrevented || link.getAttribute("aria-disabled") === "true") return;
var url = new URL(link.href, window.location.href);
if (url.origin !== window.location.origin) return;
if (/^\/(manuals|download)\//.test(url.pathname) && /\.(pdf|zip)$/i.test(url.pathname)) goal("download_click");
if (link.closest(".related-reading")) goal("related_content_click");
var queryInput = document.querySelector("#library-query");
if (link.closest(".search-results") && queryInput && queryInput.value.trim()) goal("search_used");
});
document.addEventListener("keydown", function (event) {
if (event.key === "Enter" && !event.isComposing && !event.repeat && event.target.id === "library-query" && event.target.value.trim()) goal("search_used");
});
document.addEventListener("videoreg:search-zero-results", function () { goal("search_zero_results"); });
function fragment() {
var id;
try { id = decodeURIComponent(window.location.hash.slice(1).split(":~:")[0]); } catch (_) { return; }
if (id && !document.getElementById(id) && !document.getElementsByName(id).length) problem("missing_fragment");
}
function imageFailed(image) {
if (image instanceof HTMLImageElement && image.closest("main") && image.getAttribute("src")) problem("image_failed");
}
document.addEventListener("error", function (event) { imageFailed(event.target); }, true);
if (document.querySelector("main.not-found")) problem("not_found");
document.querySelectorAll("main img").forEach(function (image) {
if (image.complete && image.naturalWidth === 0) imageFailed(image);
});
if (document.readyState === "complete") fragment();
else window.addEventListener("load", fragment, { once: true });
window.addEventListener("hashchange", fragment);
var article = document.querySelector("article.article-body");
if (!article) return;
var visibleMs = 0, depth = 0, sent = false, last = performance.now();
var active = document.visibilityState === "visible" && document.hasFocus();
function sample() {
var now = performance.now();
var nextActive = document.visibilityState === "visible" && document.hasFocus();
// Conservative cap avoids counting background/throttled gaps as reading.
if (active && nextActive) visibleMs += Math.min(now - last, 1500);
last = now; active = nextActive;
if (nextActive) {
var rect = article.getBoundingClientRect();
if (rect.height > 0) depth = Math.max(depth, Math.min(1, (window.innerHeight - rect.top) / rect.height));
}
if (!sent && visibleMs >= 60000 && depth >= 0.75) {
sent = true; goal("engaged_read"); clearInterval(timer);
}
}
var timer = setInterval(sample, 1000);
window.addEventListener("scroll", sample, { passive: true });
window.addEventListener("focus", sample);
window.addEventListener("blur", sample);
document.addEventListener("visibilitychange", sample);
})();