UNPKG

@koiztech/next-yandex-metrika

Version:
134 lines (128 loc) 4.34 kB
'use strict'; var Script = require('next/script'); var jsxRuntime = require('react/jsx-runtime'); var react = require('react'); var router = require('next/router'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var Script__default = /*#__PURE__*/_interopDefault(Script); 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__ */ jsxRuntime.jsx(Script__default.default, { 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__ */ jsxRuntime.jsx("noscript", { id: "yandex-metrika-pixel", children: /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.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__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ /* @__PURE__ */ jsxRuntime.jsx( YandexMetrikaTag, { yid, clickmap, trackLinks, accurateTrackBounce, webvisor, strategy } ), /* @__PURE__ */ jsxRuntime.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 } = {}) { react.useEffect(() => { if (disabled) { return; } const handleRouteChange = (url) => { trackPageView(url); }; if (typeof window !== "undefined") { router.Router.events.on("routeChangeComplete", handleRouteChange); if (!ignoreHashChange) { router.Router.events.on("hashChangeComplete", handleRouteChange); } return () => { router.Router.events.off("routeChangeComplete", handleRouteChange); if (!ignoreHashChange) { router.Router.events.off("hashChangeComplete", handleRouteChange); } }; } }, [router.Router.events, ignoreHashChange, disabled]); } exports.YandexMetrika = YandexMetrika; exports.usePageViews = usePageViews; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map