Also as a follow-up to my solution above, although this got me almost all of the way there, the one thing I was missing was a tie into my GoatCounter analytics platform as this bypasses any of my html that includes my analytics scripts.
So I’ve adjusted things so that the following happens…
- nginx redirect goes to an html file
- html file includes my GoatCounter script
I spent some time hacking on this between a meta refresh, javascript refresh, etc. but couldn’t get things exactly where I wanted it from not returning to the originating download page, to not opening in a new tab/window, etc.
In the end and after a few minutes with ChatGPT, here’s the solution it generated:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Automatic File Download, Redirect, and GoatCounter Tracking</title>
</head>
<body>
<!-- GoatCounter tracking script -->
<script data-goatcounter="foo" async src="bar/count.js"></script>
<script>
// Function to initiate download and redirect
function initiateDownloadAndRedirect() {
// Replace 'path/to/your/file.pdf' with the actual path to your file
var fileUrl = 'path/to/your/file.pdf';
var fileName = 'filename.pdf';
var redirectUrl = 'https://www.example.com'; // Replace with your desired redirect URL
// Create a hidden anchor element
var downloadAnchor = document.createElement('a');
downloadAnchor.href = fileUrl;
downloadAnchor.download = fileName;
// Append the anchor to the body and trigger a click event
document.body.appendChild(downloadAnchor);
downloadAnchor.click();
// Redirect to another page after the download starts
setTimeout(function() {
document.body.removeChild(downloadAnchor);
window.location.href = redirectUrl;
}, 100);
}
// Check if GoatCounter script has finished loading
function checkGoatCounter() {
if (typeof goatcounter !== 'undefined') {
// GoatCounter script has loaded, call the function to initiate download and redirect
setTimeout(initiateDownloadAndRedirect(), 1000);
} else {
// Retry after a short delay if GoatCounter script is not yet loaded
setTimeout(checkGoatCounter, 50);
}
}
// Call the function to check GoatCounter script loading status
checkGoatCounter();
</script>
<!-- Manual download link -->
<noscript>
<p>If your browser doesn't support JavaScript or you've disabled it, you can <a href="path/to/your/file.pdf" download="filename.pdf">download the file manually</a>.</p>
</noscript>
</body>
</html>
There of course is a bit of a refresh flash when the workflow happens, but this makes everything work pretty great from my side. I now have a dedicated URL that can be used to download the latest file, my analytics gets called and to increment my download file it’s just a simple HTML file adjustment.
I would agree this is a bit hacky with the creation of an anchor that gets automatically clicked, but this works better than any solution I was able to come up with after a bit of back and forth between BBEdit and my web browser.
UPDATED: GoatCounter wasn’t always capturing the download and from what I can tell the redirect might have been happening before the script completed. So I wrapped the initiateDownloadAndRedirect in a 1 second setTimeout and that makes this work much better.