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

Hash changes are reported on the wrong URL #3083

Closed
Kilian opened this issue Dec 2, 2024 · 3 comments · Fixed by #3087
Closed

Hash changes are reported on the wrong URL #3083

Kilian opened this issue Dec 2, 2024 · 3 comments · Fixed by #3087
Labels
bug Something isn't working

Comments

@Kilian
Copy link
Contributor

Kilian commented Dec 2, 2024

Describe the Bug

If you have a page that updates the URL's hash value, those URL changes are reported on the top level / rather than the page itself.

For example, https://polypane.app/css-specificity-calculator/ will append e.g. #selector=header%20h1%23sitetitle%20%3E%20.logo depending on the value added to the input field on that page.

All of these show up as top level links:
Selection_2886

Database

PostgreSQL

Relevant log output

n/a

Which Umami version are you using? (if relevant)

No response

Which browser are you using? (if relevant)

all

How are you deploying your application? (if relevant)

node on vps

@franciscao633 franciscao633 added the bug Something isn't working label Dec 3, 2024
@mikecao
Copy link
Collaborator

mikecao commented Dec 4, 2024

I think I know what's going on. We hook into the history.pushState method which passes the changed url. However it looks like it doesn't pass the entire url but just the fragment.

You can try this, edit the tracker/index.js file and change

  const handlePush = (state, title, url) => {
    if (!url) return;

    currentRef = currentUrl;
    // currentUrl = parseURL(url.toString());
    // change to this
    currentUrl = parseURL(location.href);

    if (currentUrl !== currentRef) {
      setTimeout(track, delayDuration);
    }
  };

Then run yarn build-tracker. Let me know if this works for you.

@Kilian
Copy link
Contributor Author

Kilian commented Dec 4, 2024

Right, so what you'll want to do is this:

currentUrl = new URL(url, location.href).href;

This will use the built-in URL API to resolve a potentially relative URL against the current document and then .href will return the combined full url.

I also looked at the parseURL implementation and using origin is wrong, because links can be one of three types, The following links all resolve to the same page:

  • fully specified: https://example.com/foo/bar
  • relative to the root: /foo/bar
  • relative to the current page: ../bar when you're on https://example.com/foo/baz

Using origin will fail the last case.

edit actually it's that very origin causing the issue here.

Kilian added a commit to Kilian/umami that referenced this issue Dec 4, 2024
mikecao added a commit that referenced this issue Dec 4, 2024
fix #3083 improve the parseURL function to handle page-relative URLs
@Kilian
Copy link
Contributor Author

Kilian commented Dec 9, 2024

Fixed with the above PR

@Kilian Kilian closed this as completed Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants