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
3 changes: 3 additions & 0 deletions mfr/extensions/pdf/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mfr.core import extension
from mfr.extensions.pdf import settings
from mfr.extensions.utils import munge_url_for_localdev, escape_url_for_template
from mfr.settings import GOOGLE_ANALYTICS_TRACKING_ID

logger = logging.getLogger(__name__)

Expand All @@ -29,6 +30,7 @@ def render(self):
if self.metadata.ext.lower() not in settings.EXPORT_SUPPORTED:
logger.debug('Extension not found in supported list!')
return self.TEMPLATE.render(
ga_tracking_id=GOOGLE_ANALYTICS_TRACKING_ID,
base=self.assets_url,
url=escape_url_for_template(download_url.geturl()),
stable_id=self.metadata.stable_id,
Expand All @@ -46,6 +48,7 @@ def render(self):

self.metrics.add('needs_export', True)
return self.TEMPLATE.render(
ga_tracking_id=GOOGLE_ANALYTICS_TRACKING_ID,
base=self.assets_url,
url=escape_url_for_template(exported_url.url),
stable_id=self.metadata.stable_id,
Expand Down
6 changes: 6 additions & 0 deletions mfr/extensions/pdf/templates/viewer.mako
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ http://sourceforge.net/adobe/cmap/wiki/License/
<script>
window.MFR_STABLE_ID = '${stable_id}';
window.MFR_FILE_NAME = '${file_name}';
window.GA_TRACKING_ID = '${ga_tracking_id}';
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', window.GA_TRACKING_ID, 'auto');
</script>
<script src="/static/js/mfr.child.hypothesis.js"></script>
% endif
Expand Down
29 changes: 28 additions & 1 deletion mfr/server/static/js/mfr.child.hypothesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,34 @@
window.document.head.appendChild(script);
window.document.body.classList.add('show-hypothesis');
hypothesisLoaded = true;
});

var sidePanelOpened = false;
// window.DEFAULT_URL should be the wb link in this format:
// https://<wb-domain>/v1/resources/<preprint-guid>/providers/osfstorage/<file-id>?direct=&mode=render
// TODO: parse and validate the WB URL before retrieving the preprints GUID
Copy link
Contributor

Choose a reason for hiding this comment

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

I added a TODO comment to your code. Although the split you use here is unaware of the HTTP protocol, URL domain and path, I think for now it is good enough. A better way (probably a future improvement) is to fully parse and validate the URL.

var wbLink = window.DEFAULT_URL;
var preprintGuid;
if (wbLink.split('/').length >= 6) {
preprintGuid = wbLink.split('/')[5];
} else {
preprintGuid = 'preprint-guid-unknown';
}
var sendAnalyticsIfExpanded = function (expanded) {
if (expanded && !sidePanelOpened) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably a dumb question about hypothesis configuration. What is expanded (or layout.expanded a few lines below more specifically)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

layout.expanded denotes whether the hypothesis side panel is expanded when onLayoutChanged() is called.

sidePanelOpened = expanded;
ga('send', 'event', {
eventCategory: 'Hypothesis',
eventAction: 'Open Hypothesis Panel',
//`eventLabel` is the guid of the preprint to which the file belongs
eventLabel: preprintGuid,
});
}
};
window.hypothesisConfig = function () {
return {
"onLayoutChange": function (layout) { return sendAnalyticsIfExpanded(layout.expanded); }
};
};
});
};
})();
1 change: 1 addition & 0 deletions mfr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,4 @@ def child(key):


SENTRY_DSN = config.get_nullable('SENTRY_DSN', None)
GOOGLE_ANALYTICS_TRACKING_ID = config.get_nullable('GOOGLE_ANALYTICS_TRACKING_ID', None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can GA script handle None tracking ID? I guess so but just want to double check with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure. It'll probably throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cslzchen Confirmed that it would not cause an error when tracking ID is None.