diff --git a/config/vufind/config.ini b/config/vufind/config.ini index c67a0e24e87270e647086db202cc1726420e227b..098a661d1aadbc892f8cd4536894a6836411263c 100644 --- a/config/vufind/config.ini +++ b/config/vufind/config.ini @@ -1054,9 +1054,13 @@ treeSearchLimit = 100 ; field size in the tags database table. max_tag_length = 64 -; Uncomment this section and provide your API key to enable Google Analytics +; Uncomment this section and provide your API key to enable Google Analytics. Be +; sure to set the "universal" setting to true once your account is upgraded to +; Universal Analytics; see: +; https://developers.google.com/analytics/devguides/collection/upgrade/guide ;[GoogleAnalytics] ;apiKey = "mykey" +;universal = false ; Uncomment portions of this section to activate tabs in the search box for switching ; between search modules. Keys are search backend names, values are labels for use in diff --git a/module/VuFind/src/VuFind/Config/Upgrade.php b/module/VuFind/src/VuFind/Config/Upgrade.php index 65925e0d468a5cbb1bc6004f12a0c0cad7c3019d..e989b1a8f8c0f4596072cca3529be201f742c955 100644 --- a/module/VuFind/src/VuFind/Config/Upgrade.php +++ b/module/VuFind/src/VuFind/Config/Upgrade.php @@ -512,6 +512,16 @@ class Upgrade . 'longer supported due to changes in Google APIs.' ); } + if (isset($newConfig['GoogleAnalytics']['apiKey'])) { + if (!isset($newConfig['GoogleAnalytics']['universal']) + || !$newConfig['GoogleAnalytics']['universal'] + ) { + $this->addWarning( + 'The [GoogleAnalytics] universal setting is off. See config.ini ' + . 'for important information on how to upgrade your Analytics.' + ); + } + } // Disable unused, obsolete setting: unset($newConfig['Index']['local']); diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php index d12e28bf3618cf440d9e0ff05e7b12995bdba5a8..01079869e876ebaab84f387053f489ee6a036d0e 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php @@ -197,7 +197,9 @@ class Factory $config = $sm->getServiceLocator()->get('VuFind\Config')->get('config'); $key = isset($config->GoogleAnalytics->apiKey) ? $config->GoogleAnalytics->apiKey : false; - return new GoogleAnalytics($key); + $universal = isset($config->GoogleAnalytics->universal) + ? $config->GoogleAnalytics->universal : false; + return new GoogleAnalytics($key, $universal); } /** diff --git a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php index 13a6b00a2803f5be5377a1ee922dc28475a24d71..fae09860b3fb53a4c5cdae599151df95b7c4645e 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php @@ -45,14 +45,23 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper */ protected $key; + /** + * Are we using Universal Analytics? + * + * @var bool + */ + protected $universal; + /** * Constructor * - * @param string|bool $key API key (false if disabled) + * @param string|bool $key API key (false if disabled) + * @param bool $universal Are we using Universal Analytics? */ - public function __construct($key) + public function __construct($key, $universal = false) { $this->key = $key; + $this->universal = $universal; } /** @@ -65,18 +74,31 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper if (!$this->key) { return ''; } - $code = 'var key = "' . $this->key . '";' . "\n" - . "var _gaq = _gaq || [];\n" - . "_gaq.push(['_setAccount', key]);\n" - . "_gaq.push(['_trackPageview']);\n" - . "(function() {\n" - . "var ga = document.createElement('script'); " - . "ga.type = 'text/javascript'; ga.async = true;\n" - . "ga.src = ('https:' == document.location.protocol ? " - . "'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n" - . "var s = document.getElementsByTagName('script')[0]; " - . "s.parentNode.insertBefore(ga, s);\n" - . "})();"; + if (!$this->universal) { + $code = 'var key = "' . $this->key . '";' . "\n" + . "var _gaq = _gaq || [];\n" + . "_gaq.push(['_setAccount', key]);\n" + . "_gaq.push(['_trackPageview']);\n" + . "(function() {\n" + . "var ga = document.createElement('script'); " + . "ga.type = 'text/javascript'; ga.async = true;\n" + . "ga.src = ('https:' == document.location.protocol ? " + . "'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n" + . "var s = document.getElementsByTagName('script')[0]; " + . "s.parentNode.insertBefore(ga, s);\n" + . "})();"; + } else { + $code = '(function(i,s,o,g,r,a,m){' + . "i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" + . '(i[r].q=i[r].q||[]).push(arguments)},' + . 'i[r].l=1*new Date();a=s.createElement(o),' + . 'm=s.getElementsByTagName(o)[0];a.async=1;a.src=g;' + . 'm.parentNode.insertBefore(a,m)' + . "})(window,document,'script'," + . "'//www.google-analytics.com/analytics.js','ga');" + . "ga('create', '{$this->key}', 'auto');" + . "ga('send', 'pageview');"; + } $inlineScript = $this->getView()->plugin('inlinescript'); return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET'); } diff --git a/module/VuFind/tests/fixtures/configs/googlewarnings/config.ini b/module/VuFind/tests/fixtures/configs/googlewarnings/config.ini new file mode 100644 index 0000000000000000000000000000000000000000..f459455e30d578673ab00b7268ab31982fd9252e --- /dev/null +++ b/module/VuFind/tests/fixtures/configs/googlewarnings/config.ini @@ -0,0 +1,5 @@ +[GoogleAnalytics] +apiKey = 'fake' + +[GoogleSearch] +fake = fake \ No newline at end of file diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Config/UpgradeTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Config/UpgradeTest.php index 98bc79bd164cb48153114c5cdb61c5344acf24c1..8605200057234efc3487a732725a2f14916c15e9 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Config/UpgradeTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Config/UpgradeTest.php @@ -211,6 +211,32 @@ class UpgradeTest extends \VuFindTest\Unit\TestCase ); } + /** + * Test Google-related warnings. + * + * @return void + */ + public function testGoogleWarnings() + { + $upgrader = $this->getUpgrader('googlewarnings'); + $upgrader->run(); + $warnings = $upgrader->getWarnings(); + $this->assertTrue( + in_array( + 'The [GoogleSearch] section of config.ini is no ' + . 'longer supported due to changes in Google APIs.', + $warnings + ) + ); + $this->assertTrue( + in_array( + 'The [GoogleAnalytics] universal setting is off. See config.ini ' + . 'for important information on how to upgrade your Analytics.', + $warnings + ) + ); + } + /** * Test "meaningful line" detection in SolrMarc properties files. * diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/GoogleAnalyticsTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/GoogleAnalyticsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..04e4668b8afc7797b5a8bcf0d0a703b9974da9a7 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/GoogleAnalyticsTest.php @@ -0,0 +1,71 @@ +<?php +/** + * GoogleAnalytics view helper Test Class + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +namespace VuFindTest\View\Helper\Root; +use VuFind\View\Helper\Root\GoogleAnalytics; + +/** + * GoogleAnalytics view helper Test Class + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/vufind2:unit_tests Wiki + */ +class GoogleAnalyticsTest extends \VuFindTest\Unit\ViewHelperTestCase +{ + /** + * Test the helper (old mode) + * + * @return void + */ + public function testOldSetup() + { + $helper = new GoogleAnalytics('myfakekey', false); + $helper->setView($this->getPhpRenderer()); + $output = $helper()->__toString(); + $this->assertTrue(false !== strstr($output, 'ga.js')); + $this->assertFalse(strstr($output, 'analytics.js')); + $this->assertTrue(false !== strstr($output, 'myfakekey')); + } + + /** + * Test the helper (Universal Analytics mode) + * + * @return void + */ + public function testNewSetup() + { + $helper = new GoogleAnalytics('myfakekey', true); + $helper->setView($this->getPhpRenderer()); + $output = $helper()->__toString(); + $this->assertTrue(false !== strstr($output, 'analytics.js')); + $this->assertFalse(strstr($output, 'ga.js')); + $this->assertTrue(false !== strstr($output, 'myfakekey')); + } +} \ No newline at end of file