Skip to content

Commit 06518bc

Browse files
adliuscslzchen
authored andcommitted
Added google analytics tracking for MFR Hypothesis
More specifically, this GA event tracks when users click open the Hypothesis side panel for each preprints.
1 parent 1c9b11d commit 06518bc

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

mfr/extensions/pdf/render.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mfr.core import extension
88
from mfr.extensions.pdf import settings
99
from mfr.extensions.utils import munge_url_for_localdev, escape_url_for_template
10+
from mfr.settings import GOOGLE_ANALYTICS_TRACKING_ID
1011

1112
logger = logging.getLogger(__name__)
1213

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

4749
self.metrics.add('needs_export', True)
4850
return self.TEMPLATE.render(
51+
ga_tracking_id=GOOGLE_ANALYTICS_TRACKING_ID,
4952
base=self.assets_url,
5053
url=escape_url_for_template(exported_url.url),
5154
stable_id=self.metadata.stable_id,

mfr/extensions/pdf/templates/viewer.mako

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ http://sourceforge.net/adobe/cmap/wiki/License/
426426
<script>
427427
window.MFR_STABLE_ID = '${stable_id}';
428428
window.MFR_FILE_NAME = '${file_name}';
429+
window.GA_TRACKING_ID = '${ga_tracking_id}';
430+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
431+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
432+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
433+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
434+
ga('create', window.GA_TRACKING_ID, 'auto');
429435
</script>
430436
<script src="/static/js/mfr.child.hypothesis.js"></script>
431437
% endif

mfr/server/static/js/mfr.child.hypothesis.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,23 @@
6262
window.document.head.appendChild(script);
6363
window.document.body.classList.add('show-hypothesis');
6464
hypothesisLoaded = true;
65-
});
6665

66+
var sidePanelOpened = false;
67+
var sendAnalyticsIfExpanded = function (expanded) {
68+
if (expanded && !sidePanelOpened) {
69+
sidePanelOpened = expanded;
70+
ga('send', 'event', {
71+
eventCategory: 'Hypothesis',
72+
eventAction: 'Open Hypothesis Panel',
73+
eventLabel: window.DEFAULT_URL.split('/')[5],
74+
});
75+
}
76+
};
77+
window.hypothesisConfig = function () {
78+
return {
79+
"onLayoutChange": function (layout) { return sendAnalyticsIfExpanded(layout.expanded); }
80+
};
81+
};
82+
});
6783
};
6884
})();

mfr/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,4 @@ def child(key):
170170

171171

172172
SENTRY_DSN = config.get_nullable('SENTRY_DSN', None)
173+
GOOGLE_ANALYTICS_TRACKING_ID = config.get_nullable('GOOGLE_ANALYTICS_TRACKING_ID', None)

0 commit comments

Comments
 (0)