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

Allows middle-click and ctrl+click outbound clicks to properly open as user intended #494

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file.
- Improve settings UX and design plausible/analytics#412
- Improve site listing UX and design plausible/analytics#438
- Improve onboarding UX and design plausible/analytics#441
- Allows outbound link tracking script to use new tab redirection plausible/analytics#494

### Fixed
- Do not error when activating an already activated account plausible/analytics#370
Expand Down
22 changes: 16 additions & 6 deletions tracker/src/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,33 @@
}

{{#if outboundLinks}}
function registerOutboundLinkEvents() {
document.addEventListener('click', function (event) {
var link = event.target;
function handleOutbound(event) {
var link = event.target;
var middle = event.type == "auxclick" && event.which == 2;
var click = event.type == "click";
ukutaht marked this conversation as resolved.
Show resolved Hide resolved
while(link && (typeof link.tagName == 'undefined' || link.tagName.toLowerCase() != 'a' || !link.href)) {
link = link.parentNode
}

if (link && link.href && link.host && link.host !== location.host) {
if (middle || click)
plausible('Outbound Link: Click', {props: {url: link.href}})

// Delay navigation so that Plausible is notified of the click
if(!link.target || link.target.match(/^_(self|parent|top)$/i)) {
setTimeout(function() { location.href = link.href; }, 150);
event.preventDefault();
if (!(event.ctrlKey || event.metaKey || event.shiftKey) && click) {
setTimeout(function() {
location.href = link.href;
}, 150);
event.preventDefault();
}
}
}
})
}

function registerOutboundLinkEvents() {
document.addEventListener('click', handleOutbound)
document.addEventListener('auxclick', handleOutbound)
}
{{/if}}

Expand Down