Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

enableAutoOutboundTracking breaks target="_blank" and probably noopener security #12

Open

Description

@SimonBackx

Versions

  • 0.3.1

Describe the bug

When loading plausible with link tracking, links with target _blank are no longer opened in a new tab.

Expected behavior

Instead, the link should open in a new tab.

Steps to reproduce

Steps:

  1. Install normally
  2. Set it up
  3. Enable enableAutoOutboundTracking
    import Plausible from 'plausible-tracker'
    const plausible = Plausible({
        domain: 'example.com',
        trackLocalhost: true
    })

    // This tracks the current page view and all future ones as well
    plausible.enableAutoPageviews()
    plausible.enableAutoOutboundTracking()
  1. Add a button
<a rel="nofollow noopener" target="_blank" href="https://google.com">Test</a>
  1. Click on the button. The page opens in the same tab, not in a new tab

Your Environment

  • OS: Mac
  • Browser: Safari, Chrome, (probably all)
  • Versions: all

Additional context

Might also be interesting to check if the rel attributes are respected (noopener) from a security perspective.

Activity

  1. added
    bug
    on Apr 11, 2021
  2. changed the title [-]enableAutoOutboundTracking breaks target="_blank" [/-] [+]enableAutoOutboundTracking breaks target="_blank" and probably noopener security[/+] on Apr 11, 2021
  3. ukutaht commented on Apr 12, 2021

    @ukutaht
    Contributor

    Yes, the timeout in

    setTimeout(() => {
    should only trigger for links that are not target="_blank".

    For target="_blank" links, browser default behaviour should be allowed to happen as we do in our main tracker script here: https://github.com/plausible/analytics/blob/master/tracker/src/plausible.js#L87

  4. ukutaht commented on Apr 12, 2021

    @ukutaht
    Contributor

    Also, an issue we had on the main tracker script was middle clicking on the mouse to open the link in a new tab. Outbound link tracking was messing with the default behaviour there which was fixed. Is this issue also present in this tracker script?

  5. SimonBackx commented on Apr 13, 2021

    @SimonBackx
    Author

    I'm not sure but shouldn't you also replace the location.href = link.href; with something that respects the rel and target attributes?
    https://dev.to/dhilipkmr/why-should-you-use-noopener-beware-of-security-flaws-3i57

  6. ukutaht commented on Apr 13, 2021

    @ukutaht
    Contributor

    When using target="_blank", we just allow the default browser behaviour. This line:

    location.href = link.href;

    does not run when using target="_blank". So it already respects target="_blank" rel="noopener" links.

  7. SimonBackx commented on Apr 13, 2021

    @SimonBackx
    Author

    Okay great! To prevent all the special cases for each browser, it might be an idea to update the way you delay the click event. If you redispatch the event, you don't need to check on shift keys, middle mouse etc.

    I've made an example
    https://github.com/SimonBackx/link-delay-test

    Demo at https://simonbackx.github.io/link-delay-test/

  8. SimonBackx commented on Apr 13, 2021

    @SimonBackx
    Author

    BTW, the current behaviour in plausible does stop noopener when not using target="blank"

  9. ukutaht commented on Apr 13, 2021

    @ukutaht
    Contributor

    BTW, the current behaviour in plausible does stop noopener when not using target="blank"

    Darn, yeah that's true.

    I do like the idea of just falling back to browser defaults as much as possible. Your demo is great, thanks for that. However, middle clicking and shift-clicking does not open the link in a new tab for me :/ Running FireFox on Elementary OS.

  10. SimonBackx commented on Apr 13, 2021

    @SimonBackx
    Author

    BTW, the current behaviour in plausible does stop noopener when not using target="blank"

    Darn, yeah that's true.

    I do like the idea of just falling back to browser defaults as much as possible. Your demo is great, thanks for that. However, middle clicking and shift-clicking does not open the link in a new tab for me :/ Running FireFox on Elementary OS.

    Oh too bad, does work on Chrome and Safari on MacOS.

  11. ukutaht commented on Apr 28, 2021

    @ukutaht
    Contributor

    I tested again:

    1. Firefox on Elementary: middle click nor ctrl-click work to open in a new tab
    2. Chromium on Elmentary: middle click does not work, ctrl-click does work

    The only thing I can think of is the second time the event is dispatched, it has isTrusted: false. I wonder if there's some security feature in the browser that doesn't allow JS to trigger a new tab link click programmatically. I didn't find much information about why it would be prevented.

    Thanks @SimonBackx for making this demo, it's great. But I cannot merge it since it doesn't work consistently everywhere.

    I think the best solution is switching to navigator.sendBeacon to send the event itself. That way we don't need to mess with the browser event at all, we can leave everything as default browser behaviour but still get the desired result of sending the event to our backend. What do you think?

  12. SimonBackx commented on Apr 28, 2021

    @SimonBackx
    Author

    Yes, the sendBeacon approach seems the right one!

  13. maurice-g commented on Sep 2, 2021

    @maurice-g

    Not sure if this belongs here, but enableAutoOutboundTracking also broke an interactive site of ours: We had a simple anchor (without href) with onclick event handler attached. The event handler would trigger the opening of a modal. After clicking the link, however, the entire page would reload, killing the modal that was just opening again. Assume this line to be the culprit:

    location.href = this.href;

  14. ukutaht commented on Sep 2, 2021

    @ukutaht
    Contributor

    Thanks for reporting @maurice-g.

    I can't see a way to fix it other than moving to a completely different method for tracking like sendBeacon or something else.

  15. maurice-g commented on Sep 2, 2021

    @maurice-g

    I understand. But I do think this would warrant an entry in the docs to warn users that this function might break functionality.

  16. 24 remaining items

  17. Barbapapazes commented on Feb 3, 2024

    @Barbapapazes

    Hello,

    Since this package have not been update for two years, I made a fork.

    I rewrite the tracker to be more modular and I fix this issue.

    https://github.com/Barbapapazes/plausible-tracker

  18. cschweda commented on Mar 1, 2024

    @cschweda

    Any update on this besides @Barbapapazes new fork (and the fix above from @abett)?

    The fix doesn't work for me (under Nuxt 3) -- and I'd like to stick with the canonical plausible instead of a fork.

  19. reatlat commented on May 28, 2024

    @reatlat

    Hey @metmarkosaric! Since the package doesn't get updated regularly, do you think you can make @Barbapapazes the maintainer of this package and let him update it? It seems like his package is much better than the original one!

    Well done @Barbapapazes!

  20. orvn commented on Jul 5, 2024

    @orvn

    @Barbapapazes, have you considered making a PR into this repo?

  21. Barbapapazes commented on Jul 5, 2024

    @Barbapapazes

    @orvn yes but actually, since there is no update, I have no real reason to do it. Also, the fork is a full rewrite so a PR will be difficult.

    However, I've been approached by @ukutaht on Twitter but no response since a month so I don't really know what I have to do 🤷‍♂️

  22. yhabib commented on Feb 24, 2025

    @yhabib

    Hi @ukutaht, any updates on this?

  23. vitonsky commented on May 9, 2025

    @vitonsky

    Huh, it looks a bugs is just does not matter for Plausible team.
    This bug is opened 4 years ago, have a lot of comments, even couple PRs and still had not been fixed.

    Isn't it a nice selling point.

  24. orvn commented on May 9, 2025

    @orvn

    Huh, it looks a bugs is just does not matter for Plausible team.
    This bug is opened 4 years ago, have a lot of comments, even couple PRs and still had not been fixed.

    Yeah, believe it or not, I stopped using and recommending Plausible because of this trivial issue. I use the fixed fork on legacy instances that still use it.

  25. vitonsky commented on May 10, 2025

    @vitonsky

    @orvn @SimonBackx i love plausible, but it looks current package are rusty and not actively maintain anymore, because they did not merge pull request with fix already 4 years.

    So i have re-implemented analytics client, now it does not block a user clicks, works fine with target="_blank" and send analytics well even for static sites (like sites built with Astro), because uses modern browser API (that is enough mature and widely available now).

    So you are welcome to try https://github.com/vitonsky/plausible-client

    Use plausible as you used to, but with no hassle and with live maintainer 😀

  26. Barbapapazes commented on May 10, 2025

    @Barbapapazes

    Hello,

    Since this package have not been update for two years, I made a fork.

    I rewrite the tracker to be more modular and I fix this issue.

    https://github.com/Barbapapazes/plausible-tracker

    🙄

  27. cschweda commented on May 10, 2025

    @cschweda

    Hello,
    Since this package have not been update for two years, I made a fork.
    I rewrite the tracker to be more modular and I fix this issue.
    https://github.com/Barbapapazes/plausible-tracker

    🙄

    Your fork works great and fixes the issue. Thank you!

  28. vitonsky commented on May 10, 2025

    @vitonsky

    @Barbapapazes it does make sense. However i just checked your solution and as i see in the code of your implementation - there are not resolved a problem with events loss for static sites (since this solution uses fetch with no keepalive), and this solution still uses legacy code style based on callbacks instead of modern Promise API.

    All that problems is solved in plausible-client.

    Anyway it's cool that Plausible community are live, unlike current repository, and there are developers like you and me who solves a problems.

    Let's keep going!

  29. macobo commented on Aug 12, 2025

    @macobo
    Contributor

    Hey! Karl from Plausible here. We've recently rewritten our tracking library to share the same codebase for both the NPM library and web snippet, keeping both up to date. The new NPM library can be found here: https://www.npmjs.com/package/@plausible-analytics/tracker

    The new library doesn't have the reported problem.

    Caution that we're not yet at a 1.0.0 release - this will be happening in the upcoming weeks-months. Do share feedback under the main repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions