Skip to content

Commit 7b29a6f

Browse files
authored
Merge pull request #80 from VSEphpbb/develop
Migrate from older analytics.js to newer gtag.js
2 parents 6a0a05e + cc986be commit 7b29a6f

6 files changed

Lines changed: 97 additions & 18 deletions

File tree

event/listener.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function load_google_analytics()
6767
{
6868
$this->template->assign_vars(array(
6969
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
70+
'GOOGLEANALYTICS_TAG' => $this->config['googleanalytics_tag'],
7071
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
7172
'S_ANONYMIZE_IP' => $this->config['ga_anonymize_ip'],
7273
));
@@ -105,6 +106,17 @@ public function add_googleanalytics_configs($event)
105106
'type' => 'radio:yes_no',
106107
'explain' => true,
107108
),
109+
'googleanalytics_tag' => array(
110+
'lang' => 'ACP_GOOGLEANALYTICS_TAG',
111+
'validate' => 'int',
112+
'type' => 'select',
113+
'function' => 'build_select',
114+
'params' => array(array(
115+
0 => 'ACP_GA_ANALYTICS_TAG',
116+
1 => 'ACP_GA_GTAGS_TAG',
117+
), '{CONFIG_VALUE}'),
118+
'explain' => true,
119+
),
108120
);
109121

110122
// Add the new config vars after warnings_expire_days in the display_vars config array

language/en/googleanalytics_acp.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@
4040
$lang = array_merge($lang, array(
4141
'ACP_GOOGLEANALYTICS' => 'Google Analytics',
4242
'ACP_GOOGLEANALYTICS_ID' => 'Google Analytics ID',
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>.',
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 <i class="icon fa-external-link fa-fw" aria-hidden="true"></i></a>.',
4444
'ACP_GOOGLEANALYTICS_ID_INVALID' => '“%s” is not a valid Google Analytics ID code.<br />It should be in the form “UA-0000000-00”.',
4545
'ACP_GA_ANONYMIZE_IP' => 'Turn on IP Anonymization',
4646
'ACP_GA_ANONYMIZE_IP_EXPLAIN' => 'Enable this option if you want the data collected by Google Analytics to be compliant with the EU‘s General Data Protection Regulation (GDPR). Note that enabling this option may slightly reduce the accuracy of geographic reporting.',
47+
'ACP_GOOGLEANALYTICS_TAG' => 'Google Analytics Script Tag',
48+
'ACP_GOOGLEANALYTICS_TAG_EXPLAIN' => 'Choose your preferred Google Analytics code snippet. Global site tag (gtag.js) is the current snippet recommended by Google. Google Analytics tag (analytics.js) is the legacy code snippet. <a href="https://developers.google.com/analytics/devguides/collection/gtagjs/migration">Click for more information <i class="icon fa-external-link fa-fw" aria-hidden="true"></i></a>.',
49+
'ACP_GA_ANALYTICS_TAG' => 'Google Analytics Tag (analytics.js)',
50+
'ACP_GA_GTAGS_TAG' => 'Global Site Tag (gtag.js)',
4751
));

migrations/v10x/m3_tag_option.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
*
4+
* Google Analytics extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2019 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\googleanalytics\migrations\v10x;
12+
13+
/**
14+
* Migration stage 3: Add Google Analytics tag config option
15+
*/
16+
class m3_tag_option extends \phpbb\db\migration\migration
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public static function depends_on()
22+
{
23+
return array('\phpbb\googleanalytics\migrations\v10x\m1_initial_data');
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function effectively_installed()
30+
{
31+
return $this->config->offsetExists('googleanalytics_tag');
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*
37+
* Note setting this option: If there is an existing Google Analytics ID set,
38+
* we will set this to 0 so we continue using the legacy GA code snippet. If
39+
* no Analytics ID is set, lets set this to 1 so that we promote using the new
40+
* Global Tag snippet in new installations.
41+
*/
42+
public function update_data()
43+
{
44+
return array(
45+
array('config.add', array('googleanalytics_tag', (int) empty($this->config['googleanalytics_id']))),
46+
);
47+
}
48+
}
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
{% if GOOGLEANALYTICS_ID %}
2-
<script>
3-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
4-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
5-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
6-
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
2+
{# 0 = Legacy (analytics.js) - Google Analytics #}
3+
{# 1 = Global site tag (gtag.js) - Google Analytics #}
4+
{% if GOOGLEANALYTICS_TAG == 1 %}
5+
<!-- Global site tag (gtag.js) - Google Analytics -->
6+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ GOOGLEANALYTICS_ID }}"></script>
7+
<script>
8+
window.dataLayer = window.dataLayer || [];
9+
function gtag(){dataLayer.push(arguments);}
10+
gtag('js', new Date());
711

8-
ga('create', '{{ GOOGLEANALYTICS_ID }}', 'auto');
9-
{% if S_REGISTERED_USER -%}
10-
ga('set', 'userId', {{ GOOGLEANALYTICS_USER_ID }});
11-
{% endif -%}
12-
{% if S_ANONYMIZE_IP -%}
13-
ga('set', 'anonymizeIp', true);
14-
{% endif -%}
15-
{% EVENT phpbb_googleanalytics_alter_ga_requirements -%}
16-
ga('send', 'pageview');
17-
</script>
12+
gtag('config', '{{ GOOGLEANALYTICS_ID }}', {
13+
{%- EVENT phpbb_googleanalytics_gtag_options -%}
14+
{%- if S_REGISTERED_USER %}'user_id': '{{ GOOGLEANALYTICS_USER_ID }}',{% endif -%}
15+
{%- if S_ANONYMIZE_IP %}'anonymize_ip': true,{% endif -%}
16+
});
17+
</script>
18+
{% else %}
19+
<script>
20+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
21+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
22+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
23+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
24+
25+
ga('create', '{{ GOOGLEANALYTICS_ID }}', 'auto');
26+
{% if S_REGISTERED_USER %}ga('set', 'userId', {{ GOOGLEANALYTICS_USER_ID }});{% endif %}
27+
{% if S_ANONYMIZE_IP %}ga('set', 'anonymizeIp', true);{% endif %}
28+
{% EVENT phpbb_googleanalytics_alter_ga_requirements -%}
29+
ga('send', 'pageview');
30+
</script>
31+
{% endif %}
1832
{% endif %}

tests/event/listener_test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function test_load_google_analytics()
9393
->method('assign_vars')
9494
->with(array(
9595
'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'],
96+
'GOOGLEANALYTICS_TAG' => $this->config['googleanalytics_tag'],
9697
'GOOGLEANALYTICS_USER_ID' => $this->user->data['user_id'],
9798
'S_ANONYMIZE_IP' => $this->config['ga_anonymize_ip'],
9899
));
@@ -113,7 +114,7 @@ public function add_googleanalytics_configs_data()
113114
array( // expected config and mode
114115
'settings',
115116
array('vars' => array('warnings_expire_days' => array())),
116-
array('warnings_expire_days', 'legend_googleanalytics', 'googleanalytics_id', 'ga_anonymize_ip'),
117+
array('warnings_expire_days', 'legend_googleanalytics', 'googleanalytics_id', 'ga_anonymize_ip', 'googleanalytics_tag'),
117118
),
118119
array( // unexpected mode
119120
'foobar',

tests/functional/google_analytics_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ public function test_set_acp_settings()
8282
public function test_google_analytics_code()
8383
{
8484
$crawler = self::request('GET', 'index.php');
85-
$this->assertContains($this->sample_ga_code, $crawler->filter('head > script')->text());
85+
$this->assertContains($this->sample_ga_code, $crawler->filter('head > script')->eq(1)->text());
8686
}
8787
}

0 commit comments

Comments
 (0)