derrickreimer/fathom-client

Fathom trackPageview always lags behind by one page

Closed this issue · 2 comments

Followed this:
https://github.com/derrickreimer/fathom-client#using-the-experimental-app-directory-v13

When I observe the network traffic to https://cdn.usefathom.com, it always sends the previous page that I was on.

And on the fathom dashboard, the live user always shows as the previous page as well.

Edit: or is this by design?

I came across this issue I believe it's due to the Fathom script examining the current URL. However, the router magic embedded in Next.js executes before the URL changes. Consequently, the hook would trigger when instructed by the router, and subsequently send the event to Fathom (which is lagging behind at this stage). This explains why the incorrect URLs were tracked.

What i used to fix it

    useEffect(() => {
        if (!pathname) {
            return;
        }

        let trackedPath = pathname + searchParams.toString();
        trackPageview({
            url: trackedPath,
            referrer: document.referrer,
        });
        
    }, [pathname, searchParams]);

Thanks for the report @Andrew-Gee. That makes sense @DDSJoshuaAllen, I recall contending with a similar sequencing issue in the past with URL state not getting properly inferred. It seems better to be explicit about what URL is being tracked here anyhow. Will update the docs.