PiwikAnalyticsPlugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to use Piwik 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. * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Plugin to use Piwik Analytics (based on the Analytics plugin by Evan)
  35. *
  36. * This plugin will spoot out the correct JavaScript spell to invoke
  37. * Piwik Analytics on a page.
  38. *
  39. * To use this plugin add the following to your config.php
  40. *
  41. * addPlugin('PiwikAnalytics', array('piwikroot' => 'example.com/piwik/',
  42. * 'piwikId' => 'id'));
  43. *
  44. * Replace 'example.com/piwik/' with the URL to your Piwik installation and
  45. * make sure you don't forget the final /.
  46. * Replace 'id' with the ID your statusnet installation has in your Piwik
  47. * analytics setup - for example '8'.
  48. *
  49. */
  50. class PiwikAnalyticsPlugin extends Plugin
  51. {
  52. const PLUGIN_VERSION = '2.0.0';
  53. /** the base of your Piwik installation */
  54. public $piwikroot = null;
  55. /** the Piwik Id of your statusnet installation */
  56. public $piwikId = null;
  57. /**
  58. * constructor
  59. *
  60. * @param string $root Piwik root URL
  61. * @param string $id Piwik ID of this app
  62. */
  63. function __construct($root=null, $id=null)
  64. {
  65. $this->piwikroot = $root;
  66. $this->piwikId = $id;
  67. parent::__construct();
  68. }
  69. /**
  70. * Called when all scripts have been shown
  71. *
  72. * @param Action $action Current action
  73. *
  74. * @return boolean ignored
  75. */
  76. function onEndShowScripts($action)
  77. {
  78. // Slight modification to the default code.
  79. // Loading the piwik.js file from a <script> created in a document.write
  80. // meant that the browser had no way to preload it, ensuring that its
  81. // loading will be synchronous, blocking further page rendering.
  82. //
  83. // User-agents understand protocol-relative links, so instead of the
  84. // URL produced in JS we can just give a universal one. Since it's
  85. // sitting there in the DOM ready to go, the browser can preload the
  86. // file for us and we're less likely to have to wait for it.
  87. $piwikUrl = '//' . $this->piwikroot . 'piwik.js';
  88. $piwikCode = <<<ENDOFPIWIK
  89. try {
  90. var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$this->piwikroot}" : "http://{$this->piwikroot}");
  91. var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$this->piwikId});
  92. piwikTracker.trackPageView();
  93. piwikTracker.enableLinkTracking();
  94. } catch( err ) {}
  95. ENDOFPIWIK;
  96. // Don't use $action->script() here; it'll try to preface the URL.
  97. $action->element('script', array('type' => 'text/javascript', 'src' => $piwikUrl), ' ');
  98. $action->inlineScript($piwikCode);
  99. return true;
  100. }
  101. function onPluginVersion(array &$versions)
  102. {
  103. $versions[] = array('name' => 'PiwikAnalytics',
  104. 'version' => self::PLUGIN_VERSION,
  105. 'author' => 'Tobias Diekershoff, Evan Prodromou',
  106. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Piwik',
  107. 'rawdescription' =>
  108. // TRANS: Plugin description.
  109. _m('Use <a href="http://piwik.org/">Piwik</a> Open Source web analytics software.'));
  110. return true;
  111. }
  112. }