Skip to content

Commit 113e2bd

Browse files
opaduchakfelliott
andauthored
[ENG-6958] Add pageload logging for datacite (#10940)
* When viewing a page with an assigned DOI, log a view count to datacite. --------- Co-authored-by: Fitz Elliott <[email protected]>
1 parent 6a226ac commit 113e2bd

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

website/settings/defaults.py

+4
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ def parent_dir(path):
345345
DATACITE_URL = 'https://mds.datacite.org'
346346
DATACITE_PREFIX = '10.70102' # Datacite's test DOI prefix -- update in production
347347

348+
349+
# OSF's RepoId on Datacite to track stats for DOIs. See https://support.datacite.org/docs/datacite-usage-tracker.
350+
DATACITE_TRACKER_REPO_ID = None
351+
348352
# crossref
349353
CROSSREF_USERNAME = None
350354
CROSSREF_PASSWORD = None
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function trackView(doi) {
2+
3+
const payload = {
4+
n: 'view',
5+
u: window.location.href,
6+
i: window.contextVars.dataciteTrackerRepoId,
7+
p: doi,
8+
};
9+
const r = new XMLHttpRequest();
10+
r.open('POST', `https://analytics.datacite.org/api/metric`, true);
11+
r.setRequestHeader('Content-Type', 'application/json');
12+
r.send(JSON.stringify(payload));
13+
r.onreadystatechange = () => {
14+
if (r.readyState !== 4)
15+
return;
16+
if (r.status === 400) {
17+
console.error('[DataCiteTracker] ' + r.responseText);
18+
}
19+
20+
};
21+
}
22+
23+
module.exports = {
24+
trackView: trackView,
25+
};

website/static/js/pages/project-dashboard-page.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var node = window.contextVars.node;
2929
var nodeApiUrl = ctx.node.urls.api;
3030
var nodeCategories = ctx.nodeCategories || [];
3131
var currentUserRequestState = ctx.currentUserRequestState;
32-
32+
const tracker = require('js/components/tracker');
3333

3434
// Listen for the nodeLoad event (prevents multiple requests for data)
3535
$('body').on('nodeLoad', function(event, data) {
@@ -42,6 +42,11 @@ $('body').on('nodeLoad', function(event, data) {
4242
new CitationList('#citationList');
4343
new CitationWidget('#citationStyleInput', '#citationText');
4444
}
45+
46+
if (data.node.identifiers.doi) {
47+
tracker.trackView(data.node.identifiers.doi);
48+
}
49+
4550
// Initialize nodeControl
4651
new NodeControl.NodeControl('#projectScope', data, {categories: nodeCategories, currentUserRequestState: currentUserRequestState});
4752

website/templates/base.mako

+8
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@
288288
</script>
289289
% endif
290290

291+
% if settings.DATACITE_TRACKER_REPO_ID:
292+
<script>
293+
window.contextVars = $.extend(true, {}, window.contextVars, {
294+
dataciteTrackerRepoId: ${ settings.DATACITE_TRACKER_REPO_ID | sjson, n },
295+
});
296+
</script>
297+
% endif
298+
291299

292300
${self.javascript_bottom()}
293301
</body>

0 commit comments

Comments
 (0)