Skip to content

Commit 0f83916

Browse files
committed
adding new section "user_tracking" to settings with one variable JS_CODE
1 parent 751e559 commit 0f83916

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

qiita_core/configuration_manager.py

+14
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class ConfigurationManager(object):
127127
The email address a user should write to when asking for help
128128
sysadmin_email : str
129129
The email address, Qiita sends internal notifications to a sys admin
130+
tracking_js_code : str
131+
You might want to track user on your Qiita instance. The content of the
132+
JS_CODE variable will inject this JavaScript code to the sitebase.html
133+
template, which means it will be added to basically every page of
134+
Qiita.
130135
131136
Raises
132137
------
@@ -162,6 +167,7 @@ def __init__(self):
162167
self._get_vamps(config)
163168
self._get_portal(config)
164169
self._iframe(config)
170+
self._get_tracking(config)
165171

166172
def _get_main(self, config):
167173
"""Get the configuration of the main section"""
@@ -390,3 +396,11 @@ def _get_portal(self, config):
390396

391397
def _iframe(self, config):
392398
self.iframe_qiimp = config.get('iframe', 'QIIMP', fallback=None)
399+
400+
def _get_tracking(self, config):
401+
"""Get the configuration of the 'user_tracking' section"""
402+
403+
self.tracking_js_code = config.get(
404+
'user_tracking', 'JS_CODE', fallback=None)
405+
if not self.tracking_js_code:
406+
self.tracking_js_code = None

qiita_core/support_files/config_test.cfg

+7
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,10 @@ STATS_MAP_CENTER_LONGITUDE =
196196
# On May 2024, we removed QIIMP from the code base but we will leave this
197197
# section in case we need to add access to another iframe in the future; note
198198
# that the qiita-terms are also accessed via iframe but this is internal
199+
200+
# ----------------------------- User tracking Settings ---------------------
201+
[user_tracking]
202+
# You might want to track user on your Qiita instance. You can here inject
203+
# JavaScript code to the sitebase.html template, which means it will be added
204+
# to basically every page of Qiita.
205+
JS_CODE =

qiita_core/tests/test_configuration_manager.py

+36
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,23 @@ def test_get_portal_latlong(self):
289289
obs._get_portal(self.conf)
290290
self.assertEqual(obs.stats_map_center_longitude, -105.24827)
291291

292+
def test_get_tracking(self):
293+
obs = ConfigurationManager()
294+
295+
# test for multi line content
296+
self.assertTrue(len(obs.tracking_js_code) > 520)
297+
self.assertIn("['setTrackerUrl'", obs.tracking_js_code)
298+
299+
# test that None is returned, if JS_CODE in config file, but not set
300+
self.conf.set('user_tracking', 'JS_CODE', "")
301+
obs._get_tracking(self.conf)
302+
self.assertEqual(obs.tracking_js_code, None)
303+
304+
# test that if JS_CODE is not in config file, result in None
305+
self.conf.remove_option('user_tracking', 'JS_CODE')
306+
obs._get_tracking(self.conf)
307+
self.assertEqual(obs.tracking_js_code, None)
308+
292309

293310
CONF = """
294311
# ------------------------------ Main settings --------------------------------
@@ -471,6 +488,25 @@ def test_get_portal_latlong(self):
471488
472489
# ----------------------------- iframes settings ---------------------------
473490
[iframe]
491+
# ----------------------------- User tracking Settings ---------------------
492+
[user_tracking]
493+
# You might want to track user on your Qiita instance. You can here inject
494+
# JavaScript code to the sitebase.html template, which means it will be added
495+
# to basically every page of Qiita.
496+
JS_CODE = <!-- Matomo --><script>var _paq = window._paq = window._paq || [];
497+
/* tracker methods like "setCustomDimension" should be called before
498+
"trackPageView" */
499+
_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);
500+
(function() {var u="https://piwik.cebitec.uni-bielefeld.de/";_
501+
paq.push(['setTrackerUrl', u+'matomo.php']);_
502+
paq.push(['setSiteId', '21']);
503+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName(
504+
'script')[0];
505+
g.async=true;
506+
g.src=u+'matomo.js';
507+
s.parentNode.insertBefore(g,s);})();
508+
</script><!-- End Matomo Code -->
509+
474510
"""
475511

476512
if __name__ == '__main__':

0 commit comments

Comments
 (0)