reapziq / next-yandex-metrica Goto Github PK
View Code? Open in Web Editor NEWYandex Metrica integration for Next.js
License: MIT License
Yandex Metrica integration for Next.js
License: MIT License
Для next 13 нужно добавить
"use client"
в начало файла YandexMetricaProvider.tsx
The name of this software comes from the russian word 'Metrika', not the english 'metrics'
Next v14.0.4
next-yandex-metrica v1.1.1
Warning: Cannot render <noscript> outside the main document. Try moving it into the root <head> tag.
if i move to Head, then wrapper is not wrapper... functions like reachGoal is not working
layout.js
export default function RootLayout({ children }) {
return (
<html lang="ru" className="scroll-smooth">
<body>
<YaMetricaWrapper>
{children}
</YaMetricaWrapper>
</body>
</html>
)
}
YaMetrikaWrapper.js
'use client'
import { YandexMetricaProvider } from 'next-yandex-metrica'
export const YaMetricaWrapper = ({ children }) => (
<YandexMetricaProvider
tagID={***} // my tag here
initParameters={{
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
}}
>
{children}
</YandexMetricaProvider>
)
export default YaMetricaWrapper
i use latest nextjs with the /app
folder, can i call ym(id, "reachGoal", "smth")
in a backend app route e.g. /app/api/metrics/route.ts
? build fails when i call the ym(.....)
in such an api route with this error, looks like it thinks it's a page:
> Build error occurred
Error: Failed to collect page data for /api/bot
at /node_modules/next/dist/build/utils.js:1217:15
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
type: 'Error'
}
Creating an optimized production build . Collecting page data .error: script "build" exited with code 1
here is my function:
import { db } from "@/prisma/pool";
import { ym } from "next-yandex-metrica";
export async function reachGoal(goal: string, tgId?: string | number) {
try {
const user = await db.user.findUnique({
where: {
tgId: String(tgId),
},
select: {
tgId: true,
startParam: true,
},
});
const options = user
? {
params: {
tgId: user.tgId,
startParam: user.startParam,
},
userParams: {
tgId: user.tgId,
startParam: user.startParam,
},
}
: undefined;
ym(
Number(process.env.NEXT_PUBLIC_YANDEX_METRICS_ID!),
"reachGoal",
goal,
options
);
console.log("LOG_YM_REACH_GOAL", goal);
} catch (error) {
console.error("CANT_LOG_YM_REACH_GOAL", goal, error);
}
}
i then import it in e.g. app/api/any_file.ts
like import { reachGoal } from "@/lib/yandex-metrics/reactGoal";
and it fails with the error when i run next build
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.