Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Do not track first AJAX request as a pageview (#22661)
When the app boots, Ember fires a `routeWillChange` event. This was causing us to set the `_trackView` flag in our ajax library, which would cause the next request to have the `Discourse-Track-View` header, despite not being relevant to the page view. Depending on the plugins/themes installed, this could lead to 'double counting' of pageviews. (because the initial HTML request is also counted as a page view) This commit updates the the logic to ignore the first transition (by checking `transition.from`), and also introduces an acceptance test for the behaviour. Co-authored-by: Régis Hanol <regis@hanol.fr>
- Loading branch information
1 parent
f76a9aa
commit 104baab
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/assets/javascripts/discourse/tests/acceptance/page-tracking-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { acceptance } from "discourse/tests/helpers/qunit-helpers"; | ||
import { click, visit } from "@ember/test-helpers"; | ||
import { test } from "qunit"; | ||
import pretender from "discourse/tests/helpers/create-pretender"; | ||
|
||
acceptance("Page tracking", function () { | ||
test("sets the discourse-track-view header correctly", async function (assert) { | ||
const trackViewHeaderName = "Discourse-Track-View"; | ||
assert.strictEqual( | ||
pretender.handledRequests.length, | ||
0, | ||
"no requests logged before app boot" | ||
); | ||
|
||
await visit("/"); | ||
assert.strictEqual( | ||
pretender.handledRequests.length, | ||
1, | ||
"one request logged during app boot" | ||
); | ||
assert.strictEqual( | ||
pretender.handledRequests[0].requestHeaders[trackViewHeaderName], | ||
undefined, | ||
"does not track view for ajax before a transition has taken place" | ||
); | ||
|
||
await click("#site-logo"); | ||
assert.strictEqual( | ||
pretender.handledRequests.length, | ||
2, | ||
"second request logged during next transition" | ||
); | ||
assert.strictEqual( | ||
pretender.handledRequests[1].requestHeaders[trackViewHeaderName], | ||
"true", | ||
"sends track-view header for subsequent requests" | ||
); | ||
}); | ||
}); |