Coder Social home page Coder Social logo

Usage in redirect routes about analytics HOT 8 OPEN

vercel avatar vercel commented on April 30, 2024
Usage in redirect routes

from analytics.

Comments (8)

tobiaslins avatar tobiaslins commented on April 30, 2024

Hey!
We're currently working on server-side events and they are already available on the beta package.
npm i @vercel/analytics@beta.

After that it should work like that:

import { track } from "@vercel/analytics/server"
// pages/some-route.js

export async function getServerSideProps(ctx) {
  if (await shouldRedirect()) {

    await track("Redirecting", {
       destination: "",
    }, {
       headers: ctx.req.headers
    })

    return {
      destination: 'https://my.destination',
      permanent: false
    }
  }

  return {
    props: {}
  }
}

Be sure to pass the headers to track, this will link the custom event to the pageviews from the users before.

Let me know if that works!

from analytics.

SevenOutman avatar SevenOutman commented on April 30, 2024

After that it should work like that:

@tobiaslins Thanks for your reply! The track feature looks good, but let me be more clear about my question. I'm not trying to track a "Redirecting" event, but to track the visit to this "/some-route" path, so that it's counted in the Web Analytics visitors/pageviews chart.

How am I supposed to do that?

from analytics.

tobiaslins avatar tobiaslins commented on April 30, 2024

Oh sorry, I misunderstood this. The visit to the /some-route path is automatically tracked by Web Analytics (we track all pages that are visited by the client automatically)
Are you not seeing it on the dashboard?

from analytics.

SevenOutman avatar SevenOutman commented on April 30, 2024

Are you not seeing it on the dashboard?

No, I'm not seeing it on the dashboard.

The visit to the /some-route path is automatically tracked by Web Analytics (we track all pages that are visited by the client automatically)

Let me make this clear...IIUC the tracking is done by using @vercel/analytics in Next.js app. Do you mean that it's actually handled by the Vercel server so using Vercel Analytics SDK is not even required? (That doesn't explain me not seeing it on dashboard though)

from analytics.

tobiaslins avatar tobiaslins commented on April 30, 2024

Sorry for not being clear. @vercel/analytics is still required and does the automatic pageview tracking when adding <Analytics /> component to your layout/app.tsx.
Where have you placed the component?

from analytics.

SevenOutman avatar SevenOutman commented on April 30, 2024

Sorry for not being clear. @vercel/analytics is still required and does the automatic pageview tracking when adding <Analytics /> component to your layout/app.tsx. Where have you placed the component?

I placed <Analytics /> in pages/_app.tsx as the documentation says (I'm not using app dir) and it tracks pageviews as expected. What I'm looking for with this issue is how to track pageview in redirect routes, where the client React app does not even render?

from analytics.

tobiaslins avatar tobiaslins commented on April 30, 2024

Oh, so instead of

await track("Redirecting", {
  destination: "",
}, {
  headers: ctx.req.headers
})

you would want to do something like pageview("/redirected-route") from the server?

We don't support this yet, but could also be added most probably!

from analytics.

SevenOutman avatar SevenOutman commented on April 30, 2024

you would want to do something like pageview("/redirected-route") from the server?

That's right. I want to track users' visits to this /some-route path, where they will be redirected to another destination - this is done in getServerSideProps thus /some-route does not render a client-side page. Not to track the destinations they are redirected to.

We don't support this yet, but could also be added most probably!

Alright I'll keep an eye on this.

from analytics.

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.