Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage in redirect routes #88

Open
SevenOutman opened this issue Jul 25, 2023 · 9 comments
Open

Usage in redirect routes #88

SevenOutman opened this issue Jul 25, 2023 · 9 comments

Comments

@SevenOutman
Copy link

Hi, thanks for providing this convenient SDK and I enjoy using it in my Next.js projects. I'm just wondering how am I supposed to use it in routes that redirects (so that the <Analytics /> component won't render)? FYI in my case I'm doing redirects in getServerSideProps, and I want to track visitors and views to this /some-route path.

// pages/some-route.js

export async function getServerSideProps() {
  if (await shouldRedirect()) {
    return {
      destination: 'https://my.destination',
      permanent: false
    }
  }

  return {
    props: {}
  }
}
@tobiaslins
Copy link
Collaborator

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!

@SevenOutman
Copy link
Author

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?

@tobiaslins
Copy link
Collaborator

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?

@SevenOutman
Copy link
Author

SevenOutman commented Jul 28, 2023

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)

@tobiaslins
Copy link
Collaborator

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?

@SevenOutman
Copy link
Author

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?

@tobiaslins
Copy link
Collaborator

tobiaslins commented Jul 31, 2023

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!

@SevenOutman
Copy link
Author

SevenOutman commented Jul 31, 2023

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.

@asibs
Copy link

asibs commented Jun 1, 2024

Would also be interested in this functionality. Assume it's still not available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants