Skip to content

Commit 2845307

Browse files
committed
Update user tracking implementation
1 parent d68214d commit 2845307

7 files changed

Lines changed: 59 additions & 83 deletions

File tree

event/listener.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ static public function getSubscribedEvents()
6666
public function load_google_analytics()
6767
{
6868
$this->template->assign_vars(array(
69-
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
70-
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
71-
'S_GOOGLEANALYTICS_USER_ID' => ($this->config['googleanalytics_track_user_id'] && $this->user->data['is_registered']),
69+
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
70+
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
71+
'S_GOOGLEANALYTICS_USER_ID' => $this->config['googleanalytics_track_user_id'] && $this->user->data['is_registered'],
7272
));
7373
}
7474

@@ -81,34 +81,34 @@ public function load_google_analytics()
8181
*/
8282
public function add_googleanalytics_configs($event)
8383
{
84-
// Load language file
85-
$this->user->add_lang_ext('phpbb/googleanalytics', 'googleanalytics_acp');
86-
87-
// Add a config to the settings mode, after board_timezone
88-
if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['board_timezone']))
84+
// Add a config to the settings mode, after warnings_expire_days
85+
if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['warnings_expire_days']))
8986
{
87+
// Load language file
88+
$this->user->add_lang_ext('phpbb/googleanalytics', 'googleanalytics_acp');
89+
9090
// Store display_vars event in a local variable
9191
$display_vars = $event['display_vars'];
9292

9393
// Define the new config vars
9494
$ga_config_vars = array(
95+
'legend_googleanalytics' => 'ACP_GOOGLEANALYTICS',
9596
'googleanalytics_id' => array(
96-
'lang' => 'ACP_GOOGLEANALYTICS_ID',
97-
'validate' => 'googleanalytics_id',
98-
'type' => 'text:40:20',
99-
'explain' => true,
97+
'lang' => 'ACP_GOOGLEANALYTICS_ID',
98+
'validate' => 'googleanalytics_id',
99+
'type' => 'text:40:20',
100+
'explain' => true,
100101
),
101102
'googleanalytics_track_user_id' => array(
102-
'lang' => 'ACP_GOOGLEANALYTICS_TRACK_USER_ID',
103-
'validate' => 'bool',
104-
'type' => 'radio:yes_no',
105-
'explain' => true,
103+
'lang' => 'ACP_GOOGLEANALYTICS_TRACK_USER_ID',
104+
'validate' => 'bool',
105+
'type' => 'radio:yes_no',
106+
'explain' => true,
106107
),
107108
);
108109

109-
// Add the new config vars after board_timezone in the display_vars config array
110-
$insert_after = array('after' => 'board_timezone');
111-
$display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
110+
// Add the new config vars after warnings_expire_days in the display_vars config array
111+
$display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, array('after' => 'warnings_expire_days'));
112112

113113
// Update the display_vars event with the new array
114114
$event['display_vars'] = $display_vars;

language/en/googleanalytics_acp.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
//
3939

4040
$lang = array_merge($lang, array(
41+
'ACP_GOOGLEANALYTICS' => 'Google Analytics',
4142
'ACP_GOOGLEANALYTICS_ID' => 'Google Analytics ID',
4243
'ACP_GOOGLEANALYTICS_ID_EXPLAIN' => 'Enter your Google Analytics ID code, e.g.: <samp>UA-0000000-00</samp>.',
4344
'ACP_GOOGLEANALYTICS_ID_INVALID' => '“%s” is not a valid Google Analytics ID code.<br />It should be in the form “UA-0000000-00”.',
44-
'ACP_GOOGLEANALYTICS_TRACK_USER_ID' => 'Track user id',
45-
'ACP_GOOGLEANALYTICS_TRACK_USER_ID_EXPLAIN' => 'If enabled, Google Analytics will track the unique userId value for each authenticated user. This functionality must be enabled and set up according to <a href="https://support.google.com/analytics/answer/3123666">this article</a>.',
45+
'ACP_GOOGLEANALYTICS_TRACK_USER_ID' => 'Enable user ID tracking',
46+
'ACP_GOOGLEANALYTICS_TRACK_USER_ID_EXPLAIN' => 'If enabled, Google Analytics will track your registered users across multiple devices and sessions, for a more accurate user count in your reports. This functionality must also be enabled and configured in your Google Analytics account. <a href="https://support.google.com/analytics/answer/3123666">Click for more information</a>.',
4647
));

language/ru/googleanalytics_acp.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

migrations/v10x/m2_track_user_id.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ static public function depends_on()
3030
);
3131
}
3232

33+
/**
34+
* Check if the migration is effectively installed
35+
*
36+
* @return bool True if this migration is installed, False if this migration is not installed
37+
*/
38+
public function effectively_installed()
39+
{
40+
return $this->config->offsetExists('googleanalytics_track_user_id');
41+
}
42+
3343
/**
3444
* Add Google Analytics data to the database.
3545
*
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<!-- IF GOOGLEANALYTICS_ID -->
1+
{% if GOOGLEANALYTICS_ID %}
22
<script>
33
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
44
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
55
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
66
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
77

8-
ga('create', '{GOOGLEANALYTICS_ID}', 'auto');
9-
<!-- IF S_GOOGLEANALYTICS_USER_ID -->
10-
ga('set', 'userId', {GOOGLEANALYTICS_USER_ID});
11-
<!-- ENDIF -->
12-
<!-- EVENT phpbb_googleanalytics_alter_ga_requirements -->
8+
ga('create', '{{ GOOGLEANALYTICS_ID }}', 'auto');
9+
{% if S_GOOGLEANALYTICS_USER_ID %}
10+
ga('set', 'userId', {{ GOOGLEANALYTICS_USER_ID }});
11+
{% endif %}
12+
{% EVENT phpbb_googleanalytics_alter_ga_requirements %}
1313
ga('send', 'pageview');
1414
</script>
15-
<!-- ENDIF -->
15+
{% endif %}

tests/event/listener_test.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ public function setUp()
4040
$phpbb_extension_manager = new \phpbb_mock_extension_manager($phpbb_root_path);
4141

4242
// Load/Mock classes required by the event listener class
43-
$this->config = new \phpbb\config\config(array('googleanalytics_id' => 'UA-000000-01'));
43+
$this->config = new \phpbb\config\config(array(
44+
'googleanalytics_id' => 'UA-000000-01',
45+
'googleanalytics_track_user_id' => 1,
46+
));
47+
4448
$this->template = $this->getMockBuilder('\phpbb\template\template')
4549
->getMock();
50+
4651
$this->user = new \phpbb\user('\phpbb\datetime');
52+
$this->user->data['user_id'] = 2;
53+
$this->user->data['is_registered'] = true;
4754
}
4855

4956
/**
@@ -87,8 +94,12 @@ public function test_load_google_analytics()
8794
$this->set_listener();
8895

8996
$this->template->expects($this->once())
90-
->method('assign_var')
91-
->with('GOOGLEANALYTICS_ID', $this->config['googleanalytics_id']);
97+
->method('assign_vars')
98+
->with(array(
99+
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
100+
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
101+
'S_GOOGLEANALYTICS_USER_ID' => $this->config['googleanalytics_track_user_id'] && $this->user->data['is_registered'],
102+
));
92103

93104
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
94105
$dispatcher->addListener('core.page_header', array($this->listener, 'load_google_analytics'));
@@ -105,13 +116,13 @@ public function add_googleanalytics_configs_data()
105116
return array(
106117
array( // expected config and mode
107118
'settings',
108-
array('vars' => array('board_timezone' => array())),
109-
array('board_timezone', 'googleanalytics_id', 'googleanalytics_track_user_id'),
119+
array('vars' => array('warnings_expire_days' => array())),
120+
array('warnings_expire_days', 'legend_googleanalytics', 'googleanalytics_id', 'googleanalytics_track_user_id'),
110121
),
111122
array( // unexpected mode
112123
'foobar',
113-
array('vars' => array('board_timezone' => array())),
114-
array('board_timezone'),
124+
array('vars' => array('warnings_expire_days' => array())),
125+
array('warnings_expire_days'),
115126
),
116127
array( // unexpected config
117128
'settings',

tests/functional/google_analytics_test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public function test_set_acp_settings()
4545
// Load ACP board settings page
4646
$crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=settings&sid=' . $this->sid);
4747

48-
// Test that GA settings field is found in the correct position (after SYSTEM_TIMEZONE)
48+
// Test that GA settings field is found in the correct position (after WARNINGS_EXPIRE)
4949
$nodes = $crawler->filter('#acp_board > fieldset > dl > dt > label')->extract(array('_text'));
5050
foreach ($nodes as $key => $config_name)
5151
{
52-
if (strpos($config_name, $this->lang('SYSTEM_TIMEZONE')) !== 0)
52+
if (strpos($config_name, $this->lang('WARNINGS_EXPIRE')) !== 0)
5353
{
5454
continue;
5555
}

0 commit comments

Comments
 (0)