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

event.location from up:location:changed is not string when using browser back/forward #490

Closed
johndwells opened this issue May 13, 2023 · 2 comments

Comments

@johndwells
Copy link

Bug description

I am following the recommendation for tracking pageviews by listening to up:location:changed event: https://unpoly.com/analytics

When the event is triggered from following a link, event.location is a string (e.g. /news).

However when the event is triggered from clicking on back/forward buttons, event.location is a an entire Location object.

Reproduction project

See: https://satisfying-intermediate-ankle.glitch.me/

With the following test in scripts.js:

console.log('hello!');
up.on('up:location:changed', (event) => {
  console.log(event.location);
});

Steps to reproduce the behavior:

  1. Go to reproduction project linked above
  2. Open console log, verify "hello!" message appears
  3. Click on 'Step 1: Follow this link'
  4. Verify console logs "/index2.html" appears
  5. Now press "back" in browser
  6. Verify console logs an entire Location object

Expected behavior

I would expect it to be consistent. I believe the 2.x API always sent a string value, though we used up:layer:location:changed on 2.x. When we upgraded we found that up:layer:location:changed wasn't called when navigating the history via back/forward, so we referred to the Unpoly 3.x docs and found the analytics page recommending using up:location:changed.

As a temporary workaround we are able to reliably extract the value by doing:

up.on('up:location:changed', (event) => {
  let pathname = event.location.pathname || event.location;
  console.log(pathname);
});

Browser version

  • OS: Apple 13.3.1
  • Browser: Chrome 113.0.5672.92
  • Unpoly Version 3.1.1
@triskweline
Copy link
Contributor

Thanks for the report @johndwells !

@triskweline
Copy link
Contributor

The fix has been released as part of version 3.2.1.

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

2 participants