Skip to content

Commit c8f5d7c

Browse files
authored
Analytics: send ?status to the backend (#626)
Required by readthedocs/readthedocs.org#12307
1 parent 4132fa6 commit c8f5d7c

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/analytics.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import { default as fetch } from "unfetch";
44

55
import { ajv } from "./data-validation";
6-
import { AddonBase, CLIENT_VERSION } from "./utils";
6+
import { AddonBase, getMetadataValue, CLIENT_VERSION } from "./utils";
7+
import { default as objectPath } from "object-path";
78

89
export const API_ENDPOINT = "/_/api/v2/analytics/";
910

@@ -29,17 +30,32 @@ export class AnalyticsAddon extends AddonBase {
2930
loadConfig(config) {
3031
this.config = config;
3132

32-
// Only register pageviews on non-external versions
33-
if (this.config.versions.current.type !== "external") {
34-
this.registerPageView();
33+
const versionType = objectPath.get(
34+
this.config,
35+
"versions.current.type",
36+
null,
37+
);
38+
39+
// Don't register pageviews on external versions
40+
if (versionType === "external") {
41+
return;
3542
}
43+
44+
this.registerPageView();
3645
}
3746

3847
registerPageView() {
48+
const httpStatus = getMetadataValue("readthedocs-http-status");
49+
const versionSlug = objectPath.get(
50+
this.config,
51+
"versions.current.slug",
52+
null,
53+
);
3954
const params = {
4055
project: this.config.projects.current.slug,
41-
version: this.config.versions.current.slug,
56+
version: versionSlug,
4257
absolute_uri: window.location.href,
58+
status: httpStatus,
4359
};
4460

4561
const url = API_ENDPOINT + "?" + new URLSearchParams(params).toString();

src/data-validation.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,6 @@ const addons_analytics = {
4848
},
4949
},
5050
},
51-
versions: {
52-
type: "object",
53-
required: ["current"],
54-
properties: {
55-
current: {
56-
type: "object",
57-
required: ["slug", "type"],
58-
properties: {
59-
slug: { type: "string" },
60-
type: { type: "string" },
61-
},
62-
},
63-
},
64-
},
6551
},
6652
};
6753

tests/analytics.test.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<html>
2+
<head>
3+
<meta name="readthedocs-http-status" content="200" />
4+
</head>
25
<body>
36
<script type="module">
47
import { default as sinon } from "sinon";
@@ -71,6 +74,7 @@
7174
project: "project",
7275
version: "version",
7376
absolute_uri: window.location.href,
77+
status: 200,
7478
};
7579

7680
const url =

0 commit comments

Comments
 (0)