Coder Social home page Coder Social logo

Server side analytics about examples HOT 8 CLOSED

vercel avatar vercel commented on April 28, 2024 3
Server side analytics

from examples.

Comments (8)

GuiBibeau avatar GuiBibeau commented on April 28, 2024 2

👋 I'll take this over and add to @leerob's example making a more complete on in this repo! Should get released within 5-10 days.

from examples.

rbrcsk avatar rbrcsk commented on April 28, 2024

I'm seconding this! I see this mentioned as a possible use-case, but honestly, I fail to see how it could work, even after looking at the implementation inside Next.js.

from examples.

nyg avatar nyg commented on April 28, 2024

@rbrcsk I also came here looking for an example, maybe I can give an idea of how it would or could work.

The middleware allows you to "intercept" each request and perform some logic. In our case, this logic could store in a database information about the request (e.g. path, user agent, time, IP address), this way you would have statistics about each request made to your server.

The logic could also just call the API of an external web analytics service (e.g. GoatCounter) so you don't have to handle the stats yourself.

I guess it would be better to make this logic asynchronous so as not to impact request time.

from examples.

rbrcsk avatar rbrcsk commented on April 28, 2024

Thanks @nyg! I've managed to go that far, my problem is, I'd need to get access to the response that is sent, and that seems like a no-go unfortunately.

from examples.

nyg avatar nyg commented on April 28, 2024

@rbrcsk Indeed, I don't think you can get the response just before it is sent back to the browser. Can I ask what info do you need from the response?

from examples.

fredericrous avatar fredericrous commented on April 28, 2024

I tried to use firebase to do serverside analytics but I didn't succeed.
Here's my code: (of course we can't use firebase-admin because it depends on fs module)

import type { NextFetchEvent, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { ref, runTransaction } from 'firebase/database';
import { initializeApp } from 'firebase/app';
import { getDatabase } from 'firebase/database';
const app = initializeApp(/* my firebaseConfig */);
const db = getDatabase(app);

const logView = async (slug) => {
  const { snapshot } = await runTransaction(ref(db, 'views/' + slug), (currentViews) =>
    currentViews === null ? 1 : currentViews + 1
  );
  return snapshot.val();
};

export function middleware(req: NextRequest, ev: NextFetchEvent) {
  const { pathname } = req.nextUrl;
  if (!/^\/static/.test(pathname)) {
    const noSlashPathName = pathname.replace(/^\//, '').replace(/\//g, '-');
    logView(noSlashPathName).catch(console.log);
  }
  return NextResponse.next();
}

if I await the function logView, the request hangs forever

there is no error logged in the console

from examples.

leerob avatar leerob commented on April 28, 2024

I have a WIP version going here for Next.js leerob/leerob.io#431

from examples.

GuiBibeau avatar GuiBibeau commented on April 28, 2024

Closing this as we merged the example:

Code: https://github.com/vercel/examples/tree/main/edge-functions/analytics
Deployed explainer: https://edge-functions-analytics.vercel.app/

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.