Skip to content

Commit 9795b05

Browse files
committed
Make it possible to switch between gtag and analytics tag
1 parent 21aa41d commit 9795b05

5 files changed

Lines changed: 95 additions & 14 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@
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</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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
{% if GOOGLEANALYTICS_ID %}
2-
<!-- https://developers.google.com/analytics/devguides/collection/gtagjs/migration -->
3-
<!-- Global site tag (gtag.js) - Google Analytics -->
4-
<script async src="https://www.googletagmanager.com/gtag/js?id={{ GOOGLEANALYTICS_ID }}"></script>
5-
<script>
6-
window.dataLayer = window.dataLayer || [];
7-
function gtag(){dataLayer.push(arguments);}
8-
gtag('js', new Date());
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());
911

10-
gtag('config', '{{ GOOGLEANALYTICS_ID }}', {
11-
{% EVENT phpbb_googleanalytics_gtag_options %}
12-
{% if S_REGISTERED_USER %}'user_id': '{{ GOOGLEANALYTICS_USER_ID }}',{% endif %}
13-
{% if S_ANONYMIZE_IP %}'anonymize_ip': true,{% endif %}
14-
});
15-
</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 %}
1632
{% 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',

0 commit comments

Comments
 (0)