Implement ProgCode identity and real shared reading UI kit
Build and deploy / deploy (push) Successful in 2m39s
Build and deploy / deploy (push) Successful in 2m39s
This commit is contained in:
1 parent
99c848189e
commit
12562baeb5
2040 files changed
+184656
-181
No files matched your search
+93
@@ -0,0 +1,93 @@
|
||||
// Existing counter; custom events never include queries, URLs or personal metadata.
|
||||
export const COUNTER_ID = 109268613;
|
||||
const goals = new Set(['engaged_read', 'search_used', 'download_click', 'related_content_click', 'search_zero_results', 'content_problem']);
|
||||
export function allowedHost() {
|
||||
return typeof window !== 'undefined' && window.location.protocol === 'https:' &&
|
||||
(window.location.hostname === 'progcode.ru' || window.location.hostname.endsWith('.progcode.ru'));
|
||||
}
|
||||
export function trackGoal(name, reason) {
|
||||
if (!allowedHost() || !goals.has(name) || typeof window.ym !== 'function') return;
|
||||
const reasons = ['not_found', 'image_failed', 'fragment_missing'];
|
||||
if (name === 'content_problem' && reasons.includes(reason)) window.ym(COUNTER_ID, 'reachGoal', name, {reason});
|
||||
else if (name !== 'content_problem') window.ym(COUNTER_ID, 'reachGoal', name);
|
||||
}
|
||||
let started = false;
|
||||
let previousURL;
|
||||
let previousPath;
|
||||
export function observePage() {
|
||||
if (!allowedHost()) return () => {};
|
||||
if (!started) {
|
||||
started = true;
|
||||
window.ym = window.ym || function () {(window.ym.a = window.ym.a || []).push(arguments);};
|
||||
window.ym.l = Date.now();
|
||||
const script = document.createElement('script');
|
||||
script.async = true;
|
||||
script.src = 'https://mc.yandex.ru/metrika/tag.js';
|
||||
document.head.append(script);
|
||||
window.ym(COUNTER_ID, 'init', {defer: true, clickmap: true, trackLinks: true, accurateTrackBounce: true, webvisor: false, trackHash: false});
|
||||
}
|
||||
const missing = !!document.querySelector('main.missing');
|
||||
const url = missing ? window.location.origin + '/404/' : window.location.href.split('#')[0];
|
||||
if (window.location.pathname !== previousPath) {
|
||||
window.ym(COUNTER_ID, 'hit', url, {title: document.title, referer: previousURL || document.referrer});
|
||||
previousURL = url;
|
||||
previousPath = window.location.pathname;
|
||||
}
|
||||
const problems = new Set();
|
||||
function problem(reason) {
|
||||
if (!problems.has(reason)) {problems.add(reason); trackGoal('content_problem', reason);}
|
||||
}
|
||||
if (missing) problem('not_found');
|
||||
const body = document.querySelector('main.article-shell .article-content');
|
||||
function imageError(event) {
|
||||
if (event.target instanceof HTMLImageElement && event.target.closest('main.article-shell .article-content') && event.target.getAttribute('src')) problem('image_failed');
|
||||
}
|
||||
document.addEventListener('error', imageError, true);
|
||||
for (const img of body?.querySelectorAll('img') || []) {
|
||||
if (img.complete && img.naturalWidth === 0 && img.getAttribute('src')) problem('image_failed');
|
||||
}
|
||||
function fragment() {
|
||||
if (!window.location.hash || window.location.hash === '#') return;
|
||||
let id;
|
||||
try {id = decodeURIComponent(window.location.hash.slice(1));} catch {problem('fragment_missing'); return;}
|
||||
if (!document.getElementById(id) && ![...document.getElementsByName(id)].length) problem('fragment_missing');
|
||||
}
|
||||
fragment();
|
||||
window.addEventListener('hashchange', fragment);
|
||||
function click(event) {
|
||||
if (!event.isTrusted || event.defaultPrevented || event.button !== 0) return;
|
||||
const link = event.target.closest?.('a[href]');
|
||||
if (!link || link.getAttribute('aria-disabled') === 'true') return;
|
||||
if (link.closest('.related')) trackGoal('related_content_click');
|
||||
if (link.hasAttribute('download')) trackGoal('download_click');
|
||||
}
|
||||
document.addEventListener('click', click, true);
|
||||
let elapsed = 0, last = performance.now(), active = document.visibilityState === 'visible' && document.hasFocus(), sent = false;
|
||||
function tick() {
|
||||
const now = performance.now();
|
||||
if (active) elapsed += now - last;
|
||||
last = now;
|
||||
active = document.visibilityState === 'visible' && document.hasFocus();
|
||||
if (!body || sent || !active || elapsed < 60000) return;
|
||||
const rect = body.getBoundingClientRect();
|
||||
if (rect.height > 0 && (window.innerHeight - rect.top) / rect.height >= 0.75) {
|
||||
sent = true;
|
||||
trackGoal('engaged_read');
|
||||
}
|
||||
}
|
||||
const interval = body ? window.setInterval(tick, 1000) : null;
|
||||
window.addEventListener('scroll', tick, {passive: true});
|
||||
window.addEventListener('focus', tick);
|
||||
window.addEventListener('blur', tick);
|
||||
document.addEventListener('visibilitychange', tick);
|
||||
return () => {
|
||||
if (interval !== null) window.clearInterval(interval);
|
||||
window.removeEventListener('scroll', tick);
|
||||
window.removeEventListener('focus', tick);
|
||||
window.removeEventListener('blur', tick);
|
||||
document.removeEventListener('visibilitychange', tick);
|
||||
window.removeEventListener('hashchange', fragment);
|
||||
document.removeEventListener('error', imageError, true);
|
||||
document.removeEventListener('click', click, true);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user