Skip to content

Commit 38c8454

Browse files
committed
Add google analytics privacy information
1 parent f1d39a3 commit 38c8454

File tree

4 files changed

+145
-13
lines changed

4 files changed

+145
-13
lines changed

event/listener.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ public function __construct(config $config, language $language, template $templa
6060
public static function getSubscribedEvents()
6161
{
6262
return [
63+
'core.page_header' => 'load_google_analytics',
6364
'core.acp_board_config_edit_add' => 'add_googleanalytics_configs',
64-
'core.page_header' => 'load_google_analytics',
65-
'core.validate_config_variable' => 'validate_googleanalytics_id',
65+
'core.validate_config_variable' => 'validate_googleanalytics_id',
66+
'core.page_footer_after' => 'append_agreement',
6667
];
6768
}
6869

@@ -171,4 +172,21 @@ public function validate_googleanalytics_id($event)
171172
// Update error event data
172173
$event['error'] = $error;
173174
}
175+
176+
/**
177+
* Append additional agreement details to the privacy agreement.
178+
*
179+
* @return void
180+
*/
181+
public function append_agreement()
182+
{
183+
if (!$this->template->retrieve_var('S_AGREEMENT') || ($this->template->retrieve_var('AGREEMENT_TITLE') !== $this->language->lang('PRIVACY')))
184+
{
185+
return;
186+
}
187+
188+
$this->language->add_lang('googleanalytics_ucp', 'phpbb/googleanalytics');
189+
190+
$this->template->append_var('AGREEMENT_TEXT', $this->language->lang('PHPBB_ANALYTICS_PRIVACY_POLICY', $this->config['sitename']));
191+
}
174192
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
*
4+
* Google Analytics extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2025 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
/**
12+
* DO NOT CHANGE
13+
*/
14+
if (!defined('IN_PHPBB'))
15+
{
16+
exit;
17+
}
18+
19+
if (empty($lang) || !is_array($lang))
20+
{
21+
$lang = [];
22+
}
23+
24+
// DEVELOPERS PLEASE NOTE
25+
//
26+
// All language files should use UTF-8 as their encoding and the files must not contain a BOM.
27+
//
28+
// Placeholders can now contain order information, e.g. instead of
29+
// 'Page %s of %s' you can (and should) write 'Page %1$s of %2$s', this allows
30+
// translators to re-order the output of data while ensuring it remains correct
31+
//
32+
// You do not need this where single placeholders are used, e.g. 'Message %d' is fine
33+
// equally where a string contains only two placeholders which are used to wrap text
34+
// in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine
35+
//
36+
// Some characters you may want to copy&paste:
37+
// ’ » “ ” …
38+
//
39+
40+
$lang = array_merge($lang, [
41+
'PHPBB_ANALYTICS_PRIVACY_POLICY' => '
42+
<br><br>
43+
<h3>Analytics</h3>
44+
“%1$s” may use Google Analytics, a web analytics service provided by Google LLC (“Google”), to help us understand how visitors use the site. Google Analytics uses cookies and similar technologies to collect information about your interactions with the site, including the pages you visit, the time spent on each page, and general usage patterns.
45+
<br><br>
46+
The information generated by these cookies about your use of “%1$s” (including your IP address) is transmitted to and stored by Google on servers in the United States or other locations. Google uses this information to evaluate your use of the site, compile reports on website activity for us, and provide other services relating to website activity and internet usage.
47+
<br><br>
48+
Google may also transfer this information to third parties where required to do so by law, or where such third parties process the information on Google’s behalf. To learn more about how Google collects and processes data, please see Google’s Privacy Policy at: <a href="https://policies.google.com/privacy" target="_blank">https://policies.google.com/privacy</a>.
49+
<br><br>
50+
You can opt out of Google Analytics by installing the Google Analytics opt-out browser add-on, available at: <a href="https://tools.google.com/dlpage/gaoptout" target="_blank">https://tools.google.com/dlpage/gaoptout</a>.
51+
',
52+
]);

tests/event/listener_test.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace phpbb\googleanalytics\tests\event;
1212

13-
require_once __DIR__ . '/../../../../../includes/functions_acp.php';
14-
1513
class listener_test extends \phpbb_test_case
1614
{
1715
/** @var \phpbb\googleanalytics\event\listener */
@@ -21,14 +19,23 @@ class listener_test extends \phpbb_test_case
2119
protected $config;
2220

2321
/** @var \phpbb\language\language */
24-
protected $lang;
22+
protected $language;
2523

2624
/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\template\template */
2725
protected $template;
2826

2927
/** @var \phpbb\user */
3028
protected $user;
3129

30+
public static function setUpBeforeClass(): void
31+
{
32+
$acp_functions = __DIR__ . '/../../../../../includes/functions_acp.php';
33+
if (is_file($acp_functions))
34+
{
35+
require_once $acp_functions;
36+
}
37+
}
38+
3239
/**
3340
* Setup test environment
3441
*/
@@ -46,8 +53,8 @@ protected function setUp(): void
4653
$this->template = $this->getMockBuilder('\phpbb\template\template')
4754
->getMock();
4855
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
49-
$this->lang = new \phpbb\language\language($lang_loader);
50-
$this->user = new \phpbb\user($this->lang, '\phpbb\datetime');
56+
$this->language = new \phpbb\language\language($lang_loader);
57+
$this->user = new \phpbb\user($this->language, '\phpbb\datetime');
5158
$this->user->data['user_id'] = 2;
5259
$this->user->data['is_registered'] = true;
5360
}
@@ -59,7 +66,7 @@ protected function set_listener()
5966
{
6067
$this->listener = new \phpbb\googleanalytics\event\listener(
6168
$this->config,
62-
$this->lang,
69+
$this->language,
6370
$this->template,
6471
$this->user
6572
);
@@ -80,9 +87,10 @@ public function test_construct()
8087
public function test_getSubscribedEvents()
8188
{
8289
self::assertEquals([
83-
'core.acp_board_config_edit_add',
8490
'core.page_header',
91+
'core.acp_board_config_edit_add',
8592
'core.validate_config_variable',
93+
'core.page_footer_after',
8694
], array_keys(\phpbb\googleanalytics\event\listener::getSubscribedEvents()));
8795
}
8896

@@ -91,8 +99,6 @@ public function test_getSubscribedEvents()
9199
*/
92100
public function test_load_google_analytics()
93101
{
94-
$this->set_listener();
95-
96102
$this->template->expects(self::once())
97103
->method('assign_vars')
98104
->with([
@@ -152,7 +158,7 @@ public function test_add_googleanalytics_configs($mode, $display_vars, $expected
152158

153159
$event_data = ['display_vars', 'mode'];
154160
$event_data_after = $dispatcher->trigger_event('core.acp_board_config_edit_add', compact($event_data));
155-
extract($event_data_after, EXTR_OVERWRITE);
161+
extract($event_data_after);
156162

157163
$keys = array_keys($display_vars['vars']);
158164

@@ -243,8 +249,55 @@ public function test_validate_googleanalytics_id($cfg_array, $expected_error)
243249
{
244250
self::assertArrayHasKey($expected, $event_data_after);
245251
}
246-
extract($event_data_after, EXTR_OVERWRITE);
252+
extract($event_data_after);
247253

248254
self::assertEquals($expected_error, $error);
249255
}
256+
257+
/**
258+
* Data for test_append_agreement
259+
*
260+
* @return array
261+
*/
262+
public function append_agreement_data()
263+
{
264+
return [
265+
[false, 'PRIVACY', 0], // No agreement
266+
[true, 'TERMS', 0], // Wrong title
267+
[true, 'PRIVACY', 1], // Correct conditions
268+
];
269+
}
270+
271+
/**
272+
* Test the append_agreement method
273+
*
274+
* @dataProvider append_agreement_data
275+
* @param mixed $s_agreement S_AGREEMENT template variable value
276+
* @param mixed $agreement_title AGREEMENT_TITLE template variable value
277+
* @param int $expected_append_calls Expected append_var calls
278+
*/
279+
public function test_append_agreement($s_agreement, $agreement_title, $expected_append_calls)
280+
{
281+
$this->config['sitename'] = 'Test Forum';
282+
283+
$this->template->expects(self::atMost(2))
284+
->method('retrieve_var')
285+
->withConsecutive(['S_AGREEMENT'], ['AGREEMENT_TITLE'])
286+
->willReturnOnConsecutiveCalls($s_agreement, $this->language->lang($agreement_title));
287+
288+
if ($expected_append_calls > 0)
289+
{
290+
$this->template->expects(self::once())
291+
->method('append_var')
292+
->with('AGREEMENT_TEXT', $this->language->lang('PHPBB_ANALYTICS_PRIVACY_POLICY', 'Test Forum'));
293+
}
294+
else
295+
{
296+
$this->template->expects(self::never())
297+
->method('append_var');
298+
}
299+
300+
$this->set_listener();
301+
$this->listener->append_agreement();
302+
}
250303
}

tests/functional/google_analytics_test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,13 @@ public function test_google_analytics_code()
8484
$crawler = self::request('GET', 'index.php');
8585
self::assertStringContainsString($this->sample_ga_code, $crawler->filter('head > script')->eq(1)->text());
8686
}
87+
88+
/**
89+
* Test Analytics agreement appears as expected
90+
*/
91+
public function test_ucp_agreement()
92+
{
93+
$crawler = self::request('GET', 'ucp.php?mode=privacy');
94+
$this->assertStringContainsString($this->lang('PHPBB_ANALYTICS_PRIVACY_POLICY', 'yourdomain.com'), $crawler->filter('.agreement')->html());
95+
}
8796
}

0 commit comments

Comments
 (0)