Add opt-in setting to track 404 pages - #369
Open
faisalahammad wants to merge 1 commit into
Open
Conversation
Visits that end on a 404 page are currently always dropped in Statify::is_internal(). This adds a new 'skip_404' option (default on, preserving today's behavior) so sites can opt in to recording 404 hits in the existing target column. No schema change, no migration. Both tracking paths (template_redirect and the REST endpoint used by the JS snippet) share the same is_internal() guard, so the setting covers both modes. Closes pluginkollektiv#222
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Summary
Adds a new opt-in setting under Settings → Statify → Skip tracking for ... that allows sites to record visits ending on a 404 page. The default preserves today's behavior (404 hits are still dropped); unchecking the new "Error pages (404)" checkbox writes the request to the existing
targetcolumn.Fixes #222
Changes
inc/class-statify.phpAdded
'skip_404' => 1to the default option array inStatify::init()so old saved options fall back to "skip" viawp_parse_args(backward compatible).Before:
After:
Why: 404 tracking is gated behind the new option; default keeps the existing skip behavior. Both
skip_tracking()andwp_footer()(JS snippet path) callis_internal(), so this one change covers both tracking methods.inc/class-statify-settings.phpRegistered a new settings field
statify-skip_404in the existingstatify-skipsection, with a render method that mirrors theblacklistcheckbox. Added'skip_404'to the sanitize loop so a checked value is stored as1and unchecked as0.Tests
tests/trait-statify-test-support.php: extendedinit_statify_tracking()with a$skip_404parameter (defaulttrue).tests/test-tracking.php: new testtest_track_404_opt_inexercises the opt-in path: withskip_404disabled and$wp_query->is_404 = true, the request is recorded with its target path. The existing 404-skip assertion intest_skip_trackingstill passes since the default keeps "skip" on.tests/test-settings.php: extended the manually-initialized options array and all expected output arrays with'skip_404'to match the sanitize loop.Testing
Test 1: default behavior (404 still skipped)
/this-page-does-not-exist/).Result: 404 path does not appear in today's top targets. No new row in the statify table for that request.
Test 2: opt-in to 404 tracking
Result: 404 path now appears in the top targets. A new row exists in the statify table with
target = '/this-page-does-not-exist'(or with trailing slash, depending on the site's permalink structure) andcreated = today's date.Test 3: opt-in works with both tracking methods
Repeat Test 2 once with the default tracking method and once with Tracking method → JavaScript based tracking with nonce check. In JS mode, the REST endpoint at
/wp-json/statify/v1/trackreturns 204; the row is inserted server-side.Edge cases covered by automated tests:
Automated:
All green. No new PHPCS/PHPStan issues. The pre-existing
tests/js/snippet.test.jsphpcs noise is unrelated to this change.Manual confirmation: tested on a local WordPress install, both tracking methods record 404 hits when the new checkbox is unchecked, and skip them when checked.
Backward Compatibility
No breaking changes. The new option defaults to the current behavior (skip 404). Old saved options missing the
skip_404key are filled in with the default value bywp_parse_args, so existing installs keep working without touching the settings page.Screenshot