GoogleAnalyticsPlugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to use Google Analytics
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Plugin to use Google Analytics
  34. *
  35. * This plugin will spoot out the correct JavaScript spell to invoke Google Analytics on a page.
  36. *
  37. * Note that Google Analytics is not compatible with the Franklin Street Statement; consider using
  38. * Piwik (http://www.piwik.org/) instead!
  39. *
  40. * @category Plugin
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. *
  46. * @see Event
  47. */
  48. class GoogleAnalyticsPlugin extends Plugin
  49. {
  50. var $code;
  51. var $domain;
  52. const VERSION = '0.2';
  53. function __construct($code=null)
  54. {
  55. if (!empty($code)) {
  56. global $config;
  57. $config['googleanalytics']['code'] = $code;
  58. }
  59. parent::__construct();
  60. }
  61. function onEndShowScripts($action)
  62. {
  63. $code = common_config('googleanalytics', 'code');
  64. if (empty($code)) {
  65. $code = $this->code;
  66. }
  67. $domain = common_config('googleanalytics', 'domain');
  68. if (empty($domain)) {
  69. $domain = $this->domain;
  70. }
  71. $js = <<<ENDOFSCRIPT0
  72. var _gaq = _gaq || [];
  73. _gaq.push(['_setAccount', '{$code}']);
  74. _gaq.push(['_trackPageview']);
  75. ENDOFSCRIPT0;
  76. if (!empty($domain)) {
  77. $js .= <<<ENDOFSCRIPT1
  78. _gaq.push(['_setDomainName', '{$domain}']);
  79. _gaq.push(['_setAllowHash', false]);
  80. ENDOFSCRIPT1;
  81. }
  82. $js .= <<<ENDOFSCRIPT2
  83. (function() {
  84. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  85. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  86. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  87. })();
  88. ENDOFSCRIPT2;
  89. $action->inlineScript($js);
  90. }
  91. function onPluginVersion(&$versions)
  92. {
  93. $versions[] = array('name' => 'GoogleAnalytics',
  94. 'version' => self::VERSION,
  95. 'author' => 'Evan Prodromou',
  96. 'homepage' => 'http://status.net/wiki/Plugin:GoogleAnalytics',
  97. 'rawdescription' =>
  98. // TRANS: Plugin description.
  99. _m('Use <a href="http://www.google.com/analytics/">Google Analytics</a>'.
  100. ' to track web access.'));
  101. return true;
  102. }
  103. }