Skip to content

Commit 7909b9f

Browse files
authored
Analytics: add feature flag to skip tracking 404s (#9131)
* Analytics: add feature flag to skip tracking 404s * Invert feature flag * Fix tests
1 parent 28e9b5d commit 7909b9f

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

readthedocs/projects/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,7 @@ def add_features(sender, **kwargs):
17591759
LIMIT_CONCURRENT_BUILDS = "limit_concurrent_builds"
17601760
CDN_ENABLED = "cdn_enabled"
17611761
DOCKER_GVISOR_RUNTIME = "gvisor_runtime"
1762+
RECORD_404_PAGE_VIEWS = "record_404_page_views"
17621763

17631764
# Versions sync related features
17641765
SKIP_SYNC_TAGS = 'skip_sync_tags'
@@ -1850,6 +1851,10 @@ def add_features(sender, **kwargs):
18501851
DOCKER_GVISOR_RUNTIME,
18511852
_("Use Docker gVisor runtime to create build container."),
18521853
),
1854+
(
1855+
RECORD_404_PAGE_VIEWS,
1856+
_("Record 404s page views."),
1857+
),
18531858

18541859
# Versions sync related features
18551860
(

readthedocs/projects/views/private.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
Domain,
6666
EmailHook,
6767
EnvironmentVariable,
68+
Feature,
6869
Project,
6970
ProjectRelationship,
7071
WebHook,
@@ -1180,12 +1181,15 @@ def get_context_data(self, **kwargs):
11801181

11811182
# Count of views for top pages over the month
11821183
top_pages_200 = PageView.top_viewed_pages(project, limit=25)
1183-
top_pages_404 = PageView.top_viewed_pages(
1184-
project,
1185-
limit=25,
1186-
status=404,
1187-
per_version=True,
1188-
)
1184+
track_404 = project.has_feature(Feature.RECORD_404_PAGE_VIEWS)
1185+
top_pages_404 = []
1186+
if track_404:
1187+
top_pages_404 = PageView.top_viewed_pages(
1188+
project,
1189+
limit=25,
1190+
status=404,
1191+
per_version=True,
1192+
)
11891193

11901194
# Aggregate pageviews grouped by day
11911195
page_data = PageView.page_views_by_date(
@@ -1197,6 +1201,7 @@ def get_context_data(self, **kwargs):
11971201
"top_pages_200": top_pages_200,
11981202
"page_data": page_data,
11991203
"top_pages_404": top_pages_404,
1204+
"track_404": track_404,
12001205
}
12011206
)
12021207

readthedocs/proxito/tests/test_full.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,11 @@ def test_404_all_paths_checked_default_version_different_doc_type(self, storage_
878878

879879
@mock.patch.object(BuildMediaFileSystemStorageTest, "exists")
880880
def test_track_broken_link(self, storage_exists):
881+
get(
882+
Feature,
883+
feature_id=Feature.RECORD_404_PAGE_VIEWS,
884+
projects=[self.project],
885+
)
881886
self.assertEqual(PageView.objects.all().count(), 0)
882887

883888
paths = [
@@ -926,6 +931,11 @@ def test_track_broken_link(self, storage_exists):
926931
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
927932
@mock.patch.object(BuildMediaFileSystemStorageTest, "exists")
928933
def test_track_broken_link_custom_404(self, storage_exists, storage_open):
934+
get(
935+
Feature,
936+
feature_id=Feature.RECORD_404_PAGE_VIEWS,
937+
projects=[self.project],
938+
)
929939
self.assertEqual(PageView.objects.all().count(), 0)
930940

931941
paths = [

readthedocs/proxito/views/serve.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from readthedocs.core.utils.extend import SettingsOverrideObject
2020
from readthedocs.projects import constants
2121
from readthedocs.projects.constants import SPHINX_HTMLDIR
22+
from readthedocs.projects.models import Feature
2223
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
2324
from readthedocs.redirects.exceptions import InfiniteRedirectException
2425
from readthedocs.storage import build_media_storage
@@ -402,6 +403,9 @@ def get(self, request, proxito_path, template_name='404.html'):
402403

403404
def _register_broken_link(self, project, version, path, full_path):
404405
try:
406+
if not project.has_feature(Feature.RECORD_404_PAGE_VIEWS):
407+
return
408+
405409
# If the path isn't attached to a version
406410
# it should be the same as the full_path,
407411
# otherwise it would be empty.

readthedocs/templates/projects/project_traffic_analytics.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ <h3>{% trans "Daily pageview totals" %}</h3>
4848
<button type="submit" name="download" value="true">{% trans "Download all data" %}</button>
4949
</form>
5050

51+
{% if track_404 %}
5152
<h3>{% trans "Not Found (404) pages" %}</h3>
5253

5354
<div class="module-list">
@@ -76,7 +77,7 @@ <h3>{% trans "Not Found (404) pages" %}</h3>
7677
It's possible the number of times each page is viewed is undercounted because of caching in the browser or in a CDN.
7778
{% endblocktrans %}
7879
</small>
79-
80+
{% endif %}
8081

8182
{% endblock %}
8283

0 commit comments

Comments
 (0)