Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { default as fetch } from "unfetch";

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

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

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

// Only register pageviews on non-external versions
if (this.config.versions.current.type !== "external") {
this.registerPageView();
const versionType = objectPath.get(
this.config,
"versions.current.type",
null,
);

// Don't register pageviews on external versions
if (versionType === "external") {
return;
}

this.registerPageView();
}

registerPageView() {
const httpStatus = getMetadataValue("readthedocs-http-status");
const versionSlug = objectPath.get(
this.config,
"versions.current.slug",
null,
);
const params = {
project: this.config.projects.current.slug,
version: this.config.versions.current.slug,
version: versionSlug,
absolute_uri: window.location.href,
status: httpStatus,
};

const url = API_ENDPOINT + "?" + new URLSearchParams(params).toString();
Expand Down
14 changes: 0 additions & 14 deletions src/data-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ const addons_analytics = {
},
},
},
versions: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this? Can we not express that it can be null in the validation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We validate only the fields that are required for the addon to work. If they are not required (optional), we use objectPath.get() with a default to grab the fields.

type: "object",
required: ["current"],
properties: {
current: {
type: "object",
required: ["slug", "type"],
properties: {
slug: { type: "string" },
type: { type: "string" },
},
},
},
},
},
};

Expand Down
4 changes: 4 additions & 0 deletions tests/analytics.test.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<html>
<head>
<meta name="readthedocs-http-status" content="200" />
</head>
<body>
<script type="module">
import { default as sinon } from "sinon";
Expand Down Expand Up @@ -71,6 +74,7 @@
project: "project",
version: "version",
absolute_uri: window.location.href,
status: 200,
};

const url =
Expand Down