Skip to content

Commit 1bb98a4

Browse files
Merge pull request #63 from VSEphpbb/feature-usertracking
Feature user id tracking
2 parents 532608d + dee130c commit 1bb98a4

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

event/listener.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ static public function getSubscribedEvents()
6565
*/
6666
public function load_google_analytics()
6767
{
68-
$this->template->assign_var('GOOGLEANALYTICS_ID', $this->config['googleanalytics_id']);
68+
$this->template->assign_vars(array(
69+
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
70+
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
71+
));
6972
}
7073

7174
/**
@@ -77,27 +80,28 @@ public function load_google_analytics()
7780
*/
7881
public function add_googleanalytics_configs($event)
7982
{
80-
// Load language file
81-
$this->user->add_lang_ext('phpbb/googleanalytics', 'googleanalytics_acp');
82-
83-
// Add a config to the settings mode, after board_timezone
84-
if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['board_timezone']))
83+
// Add a config to the settings mode, after warnings_expire_days
84+
if ($event['mode'] === 'settings' && isset($event['display_vars']['vars']['warnings_expire_days']))
8585
{
86+
// Load language file
87+
$this->user->add_lang_ext('phpbb/googleanalytics', 'googleanalytics_acp');
88+
8689
// Store display_vars event in a local variable
8790
$display_vars = $event['display_vars'];
8891

8992
// Define the new config vars
9093
$ga_config_vars = array(
94+
'legend_googleanalytics' => 'ACP_GOOGLEANALYTICS',
9195
'googleanalytics_id' => array(
92-
'lang' => 'ACP_GOOGLEANALYTICS_ID',
93-
'validate' => 'googleanalytics_id',
94-
'type' => 'text:40:20',
95-
'explain' => true,
96+
'lang' => 'ACP_GOOGLEANALYTICS_ID',
97+
'validate' => 'googleanalytics_id',
98+
'type' => 'text:40:20',
99+
'explain' => true,
96100
),
97101
);
98102

99-
// Add the new config vars after board_timezone in the display_vars config array
100-
$insert_after = array('after' => 'board_timezone');
103+
// Add the new config vars after warnings_expire_days in the display_vars config array
104+
$insert_after = array('after' => 'warnings_expire_days');
101105
$display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $ga_config_vars, $insert_after);
102106

103107
// Update the display_vars event with the new array

language/en/googleanalytics_acp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
//
3939

4040
$lang = array_merge($lang, array(
41+
'ACP_GOOGLEANALYTICS' => 'Google Analytics',
4142
'ACP_GOOGLEANALYTICS_ID' => 'Google Analytics ID',
42-
'ACP_GOOGLEANALYTICS_ID_EXPLAIN' => 'Enter your Google Analytics ID code, e.g.: <samp>UA-0000000-00</samp>.',
43+
'ACP_GOOGLEANALYTICS_ID_EXPLAIN' => 'Enter your Google Analytics ID code, e.g.: <samp>UA-0000000-00</samp>.<br /><br />Google Analytics can track your registered users across multiple devices and sessions, for a more accurate user count in your reports. To enable this enhanced functionality User ID tracking must be configured in your Google Analytics account. <a href="https://support.google.com/analytics/answer/3123666">Click for more information</a>.',
4344
'ACP_GOOGLEANALYTICS_ID_INVALID' => '“%s” is not a valid Google Analytics ID code.<br />It should be in the form “UA-0000000-00”.',
4445
));
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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-
<!-- EVENT phpbb_googleanalytics_alter_ga_requirements -->
8+
ga('create', '{{ GOOGLEANALYTICS_ID }}', 'auto');
9+
{% if S_REGISTERED_USER -%}
10+
ga('set', 'userId', {{ GOOGLEANALYTICS_USER_ID }});
11+
{% endif -%}
12+
{% EVENT phpbb_googleanalytics_alter_ga_requirements -%}
1013
ga('send', 'pageview');
1114
</script>
12-
<!-- ENDIF -->
15+
{% endif %}

tests/event/listener_test.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function setUp()
4444
$this->template = $this->getMockBuilder('\phpbb\template\template')
4545
->getMock();
4646
$this->user = new \phpbb\user('\phpbb\datetime');
47+
$this->user->data['user_id'] = 2;
48+
$this->user->data['is_registered'] = true;
4749
}
4850

4951
/**
@@ -87,8 +89,11 @@ public function test_load_google_analytics()
8789
$this->set_listener();
8890

8991
$this->template->expects($this->once())
90-
->method('assign_var')
91-
->with('GOOGLEANALYTICS_ID', $this->config['googleanalytics_id']);
92+
->method('assign_vars')
93+
->with(array(
94+
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
95+
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
96+
));
9297

9398
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
9499
$dispatcher->addListener('core.page_header', array($this->listener, 'load_google_analytics'));
@@ -105,13 +110,13 @@ public function add_googleanalytics_configs_data()
105110
return array(
106111
array( // expected config and mode
107112
'settings',
108-
array('vars' => array('board_timezone' => array())),
109-
array('board_timezone', 'googleanalytics_id'),
113+
array('vars' => array('warnings_expire_days' => array())),
114+
array('warnings_expire_days', 'legend_googleanalytics', 'googleanalytics_id'),
110115
),
111116
array( // unexpected mode
112117
'foobar',
113-
array('vars' => array('board_timezone' => array())),
114-
array('board_timezone'),
118+
array('vars' => array('warnings_expire_days' => array())),
119+
array('warnings_expire_days'),
115120
),
116121
array( // unexpected config
117122
'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)