piwik.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Piwik plugin.
  4. * Adds tracking code on each page.
  5. */
  6. /**
  7. * Initialization function.
  8. * It will be called when the plugin is loaded.
  9. * This function can be used to return a list of initialization errors.
  10. *
  11. * @param $conf ConfigManager instance.
  12. *
  13. * @return array List of errors (optional).
  14. */
  15. function piwik_init($conf)
  16. {
  17. $piwikUrl = $conf->get('plugins.PIWIK_URL');
  18. $piwikSiteid = $conf->get('plugins.PIWIK_SITEID');
  19. if (empty($piwikUrl) || empty($piwikSiteid)) {
  20. $error = 'Piwik plugin error: ' .
  21. 'Please define PIWIK_URL and PIWIK_SITEID in the plugin administration page.';
  22. return array($error);
  23. }
  24. }
  25. /**
  26. * Hook render_footer.
  27. * Executed on every page redering.
  28. *
  29. * Template placeholders:
  30. * - text
  31. * - endofpage
  32. * - js_files
  33. *
  34. * Data:
  35. * - _PAGE_: current page
  36. * - _LOGGEDIN_: true/false
  37. *
  38. * @param array $data data passed to plugin
  39. *
  40. * @return array altered $data.
  41. */
  42. function hook_piwik_render_footer($data, $conf)
  43. {
  44. $piwikUrl = $conf->get('plugins.PIWIK_URL');
  45. $piwikSiteid = $conf->get('plugins.PIWIK_SITEID');
  46. if (empty($piwikUrl) || empty($piwikSiteid)) {
  47. return $data;
  48. }
  49. // Free elements at the end of the page.
  50. $data['endofpage'][] = '<!-- Piwik -->' .
  51. '<script type="text/javascript">' .
  52. ' var _paq = _paq || [];' .
  53. ' _paq.push([\'trackPageView\']);' .
  54. ' _paq.push([\'enableLinkTracking\']);' .
  55. ' (function() {' .
  56. ' var u="//' . $piwikUrl . '/";' .
  57. ' _paq.push([\'setTrackerUrl\', u+\'piwik.php\']);' .
  58. ' _paq.push([\'setSiteId\', \'' . $piwikSiteid . '\']);' .
  59. ' var d=document, g=d.createElement(\'script\'), s=d.getElementsByTagName(\'script\')[0];' .
  60. ' g.type=\'text/javascript\'; g.async=true; g.defer=true; g.src=u+\'piwik.js\'; s.parentNode.insertBefore(g,s);' .
  61. ' })();' .
  62. '</script>' .
  63. '<noscript><p><img src="//' . $piwikUrl . '/piwik.php?idsite=' . $piwikSiteid . '" style="border:0;" alt="" /></p></noscript>' .
  64. '<!-- End Piwik Code -->';
  65. return $data;
  66. }