Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Trigger pageviews on pages loaded from bfcache (#4656)

RobertJoonas799edec81cec
diff --git a/lib/plausible_web/templates/layout/_tracking.html.heex b/lib/plausible_web/templates/layout/_tracking.html.heex
index 208a7bc11c..4db5c84388 100644
--- a/lib/plausible_web/templates/layout/_tracking.html.heex
+++ b/lib/plausible_web/templates/layout/_tracking.html.heex
@@ -32,6 +32,13 @@
}
plausible('pageview', defaultEventOptions)
+
+ window.addEventListener('pageshow', function(event) {
+ if (event.persisted) {
+ // Page was restored from bfcache - trigger a pageview
+ plausible('pageview', defaultEventOptions);
+ }
+ })
</script>
<% end %>
<% end %>
diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js
index aecb95786d..c4a940dfbd 100644
--- a/tracker/src/plausible.js
+++ b/tracker/src/plausible.js
@@ -231,6 +231,15 @@
} else {
page()
}
+
+ {{#if pageleave}}
+ window.addEventListener('pageshow', function(event) {
+ if (event.persisted) {
+ // Page was restored from bfcache - trigger a pageview
+ page();
+ }
+ })
+ {{/if}}
{{/unless}}
{{#if (any outbound_links file_downloads tagged_events)}}