1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_googleanalytics_info()
9{
10	return [
11		'name' => tra('Google Analytics'),
12		'documentation' => 'PluginGoogleAnalytics',
13		'description' => tra('Add the tracking code for Google Analytics'),
14		'prefs' => [ 'wikiplugin_googleanalytics' ],
15		'iconname' => 'chart',
16		'format' => 'html',
17		'introduced' => 14,
18		'params' => [
19			'account' => [
20				'required' => true,
21				'name' => tra('Account Number'),
22				'description' => tr('The account number for the site. Your account number from Google looks like
23					%0. All you need to enter is %1', 'UA-XXXXXXX-YY', '<code>XXXXXXX-YY</code>'),
24				'since' => '3.0',
25				'filter' => 'text',
26				'default' => ''
27			],
28			'group_option' => [
29				'required' => true,
30				'name' => tra('Groups Option'),
31				'description' => tr('Define option for Google Analytics groups, include or exclude'),
32				'filter' => 'text',
33				'default' => ''
34			],
35			'groups' => [
36				'required' => true,
37				'name' => tra('Available Groups'),
38				'description' => tr('User groups for which Google Analytics will be available'),
39				'default' => ''
40			],
41		],
42	];
43}
44
45function wikiplugin_googleanalytics($data, $params)
46{
47	global $feature_no_cookie, $prefs;	// set according to cookie_consent_feature pref in tiki-setup.php
48
49	$showCode = WikiPlugin_Helper::showAnalyticsCode($params);
50	if (! $showCode) {
51		return;
52	}
53
54	if (empty($params['account'])) {
55		return tra('Missing parameter');
56	}
57	if ($feature_no_cookie) {
58		return '';
59	}
60	$account = htmlspecialchars($params['account'], ENT_QUOTES);
61
62	if ($prefs['site_google_analytics_gtag'] !== 'y') {
63		$ret = <<<HTML
64<script>
65(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
66    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
67m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
68})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
69
70ga('create', 'UA-$account', 'auto');  // Replace with your property ID.
71ga('send', 'pageview');
72
73</script>
74HTML;
75	} else {
76		$ret = <<<HTML
77<!-- Global site tag (gtag.js) - Google Analytics -->
78<script async src="https://www.googletagmanager.com/gtag/js?id=UA-$account"></script>
79<script>
80  window.dataLayer = window.dataLayer || [];
81  function gtag(){dataLayer.push(arguments);}
82  gtag('js', new Date());
83
84  gtag('config', 'UA-$account');
85</script>
86HTML;
87	}
88	return $ret;
89}
90