Skip to content

A platform for community discussion. Free, open, simple.

FIX: Wrong beacon pageview referrer after browser back navigation (#39743)

Alan Guo Xiang Tan30f548a01f85
diff --git a/frontend/discourse/app/instance-initializers/page-tracking.js b/frontend/discourse/app/instance-initializers/page-tracking.js
index b37a1dc74d2..7ffe410b304 100644
--- a/frontend/discourse/app/instance-initializers/page-tracking.js
+++ b/frontend/discourse/app/instance-initializers/page-tracking.js
@@ -102,36 +102,37 @@ export default {
},
handleRouteDidChange(transition) {
- if (
- transition.isAborted ||
- (transition.urlMethod === "replace" && transition.queryParamsOnly)
- ) {
+ if (transition.isAborted) {
return;
}
- const trackingSessionId = document.querySelector(
- "meta[name=discourse-track-view-session-id]"
- )?.content;
- const referrerUrl = transition.from
- ? _preNavigationUrl
- : document.referrer.length
- ? document.referrer
- : null;
+ if (!(transition.urlMethod === "replace" && transition.queryParamsOnly)) {
+ const trackingSessionId = document.querySelector(
+ "meta[name=discourse-track-view-session-id]"
+ )?.content;
+ const referrerUrl = transition.from
+ ? _preNavigationUrl
+ : document.referrer.length
+ ? document.referrer
+ : null;
+
+ let topicId;
+ if (
+ transition.to.name === "topic.fromParamsNear" ||
+ transition.to.name === "topic.fromParams"
+ ) {
+ topicId = transition.to.parent.params.id;
+ }
- let topicId;
- if (
- transition.to.name === "topic.fromParamsNear" ||
- transition.to.name === "topic.fromParams"
- ) {
- topicId = transition.to.parent.params.id;
+ sendBeaconPageview({
+ sessionId: trackingSessionId,
+ url: window.location.href,
+ referrer: referrerUrl,
+ topicId,
+ });
}
- sendBeaconPageview({
- sessionId: trackingSessionId,
- url: window.location.href,
- referrer: referrerUrl,
- topicId,
- });
+ _preNavigationUrl = window.location.href;
},
handleRouteWillChange(transition) {
@@ -171,7 +172,6 @@ export default {
trackingUrl = new URL(path, window.location.origin).href;
trackingReferrer = window.location.href;
}
- _preNavigationUrl = window.location.href;
trackNextAjaxAsPageview(trackingSessionId, trackingUrl, trackingReferrer);
if (
diff --git a/spec/system/request_tracker_spec.rb b/spec/system/request_tracker_spec.rb
index 5f556f3f5c1..2a7169150a2 100644
--- a/spec/system/request_tracker_spec.rb
+++ b/spec/system/request_tracker_spec.rb
@@ -753,6 +753,8 @@ describe "Request tracking" do
end
context "when anonymous" do
+ let(:discovery) { PageObjects::Pages::Discovery.new }
+
it "tracks topic views correctly via beacon" do
visit "/"
@@ -774,14 +776,14 @@ describe "Request tracking" do
beacon_event =
all_events
- .select { |e| e[:event_name] == :beacon_browser_pageview }
- .find { |e| e[:params].last[:topic_id] == topic.id }
+ .select { |event| event[:event_name] == :beacon_browser_pageview }
+ .find { |event| event[:params].last[:topic_id] == topic.id }
&.dig(:params)
&.last
non_beacon_event =
all_events
- .select { |e| e[:event_name] == :browser_pageview }
- .find { |e| e[:params].last[:topic_id] == topic.id }
+ .select { |event| event[:event_name] == :browser_pageview }
+ .find { |event| event[:params].last[:topic_id] == topic.id }
&.dig(:params)
&.last
@@ -796,6 +798,43 @@ describe "Request tracking" do
expect(event[:session_id]).to be_present
end
end
+
+ it "tracks the previous URL as referrer on browser back and forward navigation via beacon" do
+ visit "/"
+ wait_for_beacon_count(1)
+
+ discovery.topic_list.visit_topic(topic)
+ wait_for_beacon_count(2)
+
+ events =
+ DiscourseEvent.track_events(:beacon_browser_pageview) do
+ page.go_back
+ wait_for_beacon_count(3)
+ end
+
+ beacon_back_event = events.first[:params].last
+
+ expect(beacon_back_event[:url]).to eq("#{Discourse.base_url_no_prefix}/")
+ expect(beacon_back_event[:referrer]).to eq(topic.url)
+
+ events =
+ DiscourseEvent.track_events(:beacon_browser_pageview) do
+ page.go_forward
+ wait_for_beacon_count(4)
+ end
+
+ beacon_forward_event = events.first[:params].last
+
+ expect(beacon_forward_event[:url]).to eq(topic.url)
+ expect(beacon_forward_event[:referrer]).to eq("#{Discourse.base_url}/")
+ end
+
+ def wait_for_beacon_count(count)
+ try_until_success do
+ CachedCounting.flush
+ expect(ApplicationRequest.stats["page_view_anon_browser_beacon_total"]).to eq(count)
+ end
+ end
end
end
end