Skip to content

Commit f28ca14

Browse files
authored
Fixes #21: Add noscript fallback functionality and setting (#22)
1 parent 44724fc commit f28ca14

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

config/matomo.settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"cache": 0,
2323
"last_cache": 0,
2424
"translation_set": 0,
25+
"noscript": 0,
2526
"codesnippet_before": "",
2627
"codesnippet_after": "",
2728
"js_scope": "header",

matomo.admin.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ function matomo_admin_settings_form($form_state) {
389389
);
390390
}
391391

392+
$form['advanced']['noscript'] = array(
393+
'#type' => 'checkbox',
394+
'#title' => t('Also track when JavaScript is disabled'),
395+
'#description' => t('If checked, a <em>noscript</em> fallback will be added to the page, to also track when JavaScript is disabled in a browser.'),
396+
'#default_value' => $config->get('noscript'),
397+
);
398+
392399
// Code snippet settings.
393400
$user_access_add_js_snippets = !user_access('add js snippets for matomo');
394401
$user_access_add_js_snippets_permission_warning = $user_access_add_js_snippets ? ' <em>' . t('This field has been disabled because you do not have sufficient permissions to edit it.') . '</em>' : '';

matomo.install

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ function matomo_update_1000() {
9898

9999
$config->save();
100100
}
101+
102+
/**
103+
* Add the noscript setting.
104+
*/
105+
function matomo_update_1001() {
106+
$config = config('matomo.settings');
107+
$noscript = $config->get('noscript');
108+
if (!isset($noscript)) {
109+
$config->set('noscript', 0);
110+
$config->save();
111+
}
112+
}

matomo.module

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ function matomo_config_info() {
8787
}
8888

8989
/**
90-
* Implements hook_page_alter() to insert JavaScript to the appropriate scope/region of the page.
90+
* Implements hook_preprocess_page().
91+
*
92+
* Insert JavaScript to the appropriate scope/region of the page, insert the
93+
* noscript fallback to page_bottom if enabled.
9194
*/
92-
function matomo_preprocess_layout(&$variables) {
93-
global $user;
95+
function matomo_preprocess_page(&$variables) {
96+
global $user, $is_https;
9497
$config = config('matomo.settings');
9598

9699
$id = $config->get('site_id');
@@ -361,6 +364,14 @@ function matomo_preprocess_layout(&$variables) {
361364

362365
// Add tracker code to scope.
363366
backdrop_add_js($script, array('scope' => $scope, 'type' => 'inline'));
367+
368+
// Add the noscript fallback markup to the page_bottom string.
369+
if ($config->get('noscript')) {
370+
$src_url = ($is_https && !empty($url_https)) ? check_url($url_https) : check_url($url_http);
371+
$src_url = rtrim($src_url, '/') . "/matomo.php?idsite=$id&amp;rec=1";
372+
$img = '<img src="' . $src_url . '" style="border:0" alt="" />';
373+
$variables['page_bottom'] .= "<!-- Matomo --><noscript><p>$img</p></noscript><!-- End Matomo -->";
374+
}
364375
}
365376
}
366377

tests/matomo.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,37 @@ class MatomoBasicTest extends BackdropWebTestCase {
208208
$this->assertRaw('t2.trackPageView', '[testMatomoTrackingCode]: After codesnippet with "t2" tracker has been found.');
209209
}
210210

211+
/**
212+
* Test the noscript fallback tracking code.
213+
*/
214+
public function testMatomoNojsFallback() {
215+
$config = config('matomo.settings');
216+
$config->set('site_id', '5');
217+
$config->set('noscript', FALSE);
218+
$config->set('url_http', 'https://example.com/');
219+
$config->set('url_https', 'https://example.com/');
220+
$config->save();
221+
$fallback_code = '<!-- Matomo --><noscript><p><img src="https://example.com/matomo.php?idsite=5&amp;rec=1" style="border:0" alt="" /></p></noscript><!-- End Matomo -->';
222+
223+
$this->backdropGet('<front>');
224+
$this->assertNoRaw($fallback_code, '[testMatomoTrackingCode]: Fallback code not added when disabled.');
225+
226+
$config->set('noscript', TRUE);
227+
$config->save();
228+
cache('page')->flush();
229+
$this->backdropGet('<front>');
230+
$this->assertRaw($fallback_code, '[testMatomoTrackingCode]: Fallback code present when enabled.');
231+
232+
// Now only track anonymous.
233+
$config->set('roles', array(BACKDROP_ANONYMOUS_ROLE => BACKDROP_ANONYMOUS_ROLE));
234+
$config->save();
235+
$this->backdropGet('<front>');
236+
$this->assertNoRaw($fallback_code, '[testMatomoTrackingCode]: Fallback code not added when role not tracked.');
237+
238+
$this->backdropLogout();
239+
$this->backdropGet('<front>');
240+
$this->assertRaw($fallback_code, '[testMatomoTrackingCode]: Fallback code present when role is tracked.');
241+
}
211242
}
212243

213244
/**

0 commit comments

Comments
 (0)