Logo

dev-resources.site

for different kinds of informations.

Introducing React Page Tracker: Simplify Navigation Tracking

Published at
11/22/2024
Categories
documentreferrer
nextjs
webdev
remix
Author
ryan-chou
Author
9 person written this
ryan-chou
open
Introducing React Page Tracker: Simplify Navigation Tracking

Preface

I recently encountered a requirement to implement a "back to the previous page" button.

Since my project uses Next.js, the straightforward solution was to call router.back(). Problem solved, right?

But here’s the catch: if the user lands on the page with the back button for the first time, clicking it would ideally navigate them back to the homepage instead of leaving the site altogether!


It seemed easy at first...

Initially, I thought simply tracking whether the user navigated between pages using pathname would suffice.

Turns out, if the user keeps clicking links to go deeper and then starts using the browser's back and forward buttons, the back button functionality breaks.

This is because pathname only tells you that the URL has changed—it doesn't indicate whether the change was triggered by a link click or by using the browser's back/forward buttons.


What about the native history API?

If the framework doesn’t offer support, surely the native history API should, right? After all, those browser back and forward buttons are actual desktop app buttons, so it makes sense that the browser itself should provide this information.

But no—it doesn’t.

That’s right, no support at all.

When you click the back or forward button, the browser triggers an onPopState event. That’s it. It doesn’t tell you how the user arrived at the current page.


A simple requirement that’s deceptively complex

I searched NPM for relevant packages but couldn’t find any. At one point, I was ready to drop this requirement altogether.

Still, I wanted to give it a try—surely, there had to be a way to solve this.

In the end, I managed to fulfill the requirement using the native history API.

Not long after, I realized something else: when tracking user navigation, the source URL is often fetched from document.referrer.

But with Next.js, document.referrer always stays fixed at the source link from the first visit.

After some investigation, I found that achieving accurate referrer tracking is impossible with just Next.js. The moment the user interacts with the back or forward buttons, the referrer data becomes entirely unreliable.


Published on NPM!

I figured I couldn’t be the only one facing this challenge. So after tidying up my code, I published it as a package on npm!

Here’s what it offers:

  • Detects whether the user is on the first page (solves the back button issue).
  • Correctly tracks document.referrer.
  • Determines how the user arrived at the current page (back, forward, or push).
  • Records the complete browsing history.

You can easily install it with:

npm i react-page-tracker  
Enter fullscreen mode Exit fullscreen mode

To make things clearer, I also created a demo so you can try it out.

For a practical example of the back button in action, I made this sample.

remix Article's
30 articles in total
Favicon
How to disable a link in React Router 7 / Remix
Favicon
Verifying Your Shopify App Embed is Actually Enabled with Remix: A Step-by-Step Guide
Favicon
Headless e-commerce structure
Favicon
🚀 OpenAI's Transition from Next.js to Remix: A Strategic Move in Web Development 🌐
Favicon
# Key New Features in React Router 7: Embracing the Remix Future
Favicon
React Router V7: A Crash Course
Favicon
Stop Running to Next.js — Remix is the Future of React, and Here’s Why You’re Missing Out
Favicon
Introducing React Page Tracker: Simplify Navigation Tracking
Favicon
Create an Admin Panel for your project in 5 minutes
Favicon
Remix Drizzle Auth Template
Favicon
I used GitHub as a CMS
Favicon
Remix vs. Next.js: Why Choose Remix?
Favicon
Choosing Remix as a Server-Side Rendering (SSR) Framework
Favicon
Next.js vs Remix: Which Framework is Better?
Favicon
Using PostHog in Remix Loaders and Actions on Cloudflare Pages
Favicon
Creating a marketplace with Stripe Connect: The onboarding process
Favicon
In-Depth Analysis of Next.js, Gatsby, and Remix
Favicon
Implementing RSS feed with Remix
Favicon
Cloudflare + Remix + PostgreSQL with Prisma Accelerate's Self Hosting
Favicon
Membangun Aplikasi Verifikasi Kode Autentikasi dengan Twilio Menggunakan Go dan Remix
Favicon
Google Analytics (GA4) implementation with React - Remix example
Favicon
Day 3 of Brylnt: Next.js vs Remix
Favicon
𝐌𝐚𝐧𝐭𝐢𝐧𝐞 𝐁𝐨𝐚𝐫𝐝𝐬 🚀
Favicon
How to Integrate Mixpanel with Remix?
Favicon
Remix, React, the Discogs API & filtermusikk.no
Favicon
How to navigate between Shopify app
Favicon
Framer Motion useAnimation()
Favicon
An Introduction to Remix
Favicon
Remix vs Next.js: Which Framework Should You Choose?
Favicon
Remix vs Next.js: Which Framework Should You Choose?

Featured ones: