@koiztech/next-yandex-metrika
Version:
Yandex Metrika for Next.js >= 14
127 lines (124 loc) • 4.01 kB
JavaScript
import Script from 'next/script';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useEffect } from 'react';
import { Router } from 'next/router';
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
function YandexMetrikaTag({
yid,
clickmap = true,
trackLinks = true,
accurateTrackBounce = true,
webvisor = false,
strategy = "afterInteractive"
}) {
return /* @__PURE__ */ jsx(Script, { id: "yandex-metrika-tag", strategy, children: `
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.webvisor.org/metrika/tag_ww.js", "ym");
ym(${yid}, "init", {
clickmap:${clickmap},
trackLinks:${trackLinks},
accurateTrackBounce:${accurateTrackBounce},
webvisor:${webvisor}
});
` });
}
function YandexMetrikaPixel({ yid }) {
const src = `https://mc.yandex.ru/watch/${yid}`;
return /* @__PURE__ */ jsx("noscript", { id: "yandex-metrika-pixel", children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(
"img",
{
src,
style: { position: "absolute", left: "-9999px" },
alt: ""
}
) }) });
}
function YandexMetrika({
yid,
clickmap = true,
trackLinks = true,
accurateTrackBounce = true,
webvisor = false,
strategy = "afterInteractive"
}) {
yid = yid != null ? yid : Number(process.env.NEXT_PUBLIC_YANDEX_ID);
if (!yid) {
console.warn("Yandex Metrika ID is not set");
return null;
}
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(
YandexMetrikaTag,
{
yid,
clickmap,
trackLinks,
accurateTrackBounce,
webvisor,
strategy
}
),
/* @__PURE__ */ jsx(YandexMetrikaPixel, { yid })
] });
}
var trackPageView = (url, options) => {
if (typeof window !== "undefined" && "ym" in window) {
const ym = window.ym;
const yid = Number(process.env.NEXT_PUBLIC_YANDEX_ID);
if (!ym || !yid) {
return;
}
ym(yid, "hit", url, __spreadProps(__spreadValues({}, options), {
title: document.title
}));
}
};
function usePageViews({
ignoreHashChange,
disabled
} = {}) {
useEffect(() => {
if (disabled) {
return;
}
const handleRouteChange = (url) => {
trackPageView(url);
};
if (typeof window !== "undefined") {
Router.events.on("routeChangeComplete", handleRouteChange);
if (!ignoreHashChange) {
Router.events.on("hashChangeComplete", handleRouteChange);
}
return () => {
Router.events.off("routeChangeComplete", handleRouteChange);
if (!ignoreHashChange) {
Router.events.off("hashChangeComplete", handleRouteChange);
}
};
}
}, [Router.events, ignoreHashChange, disabled]);
}
export { YandexMetrika, usePageViews };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map