1<?php 2 3use ComboStrap\Canonical; 4use ComboStrap\DokuPath; 5use ComboStrap\Page; 6use ComboStrap\PluginUtility; 7 8if (!defined('DOKU_INC')) die(); 9 10/** 11 * 12 * 13 */ 14class action_plugin_combo_canonical extends DokuWiki_Action_Plugin 15{ 16 17 const CONF_CANONICAL_FOR_GA_PAGE_VIEW = "useCanonicalValueForGoogleAnalyticsPageView"; 18 const CANONICAL = Canonical::PROPERTY_NAME; 19 20 21 function __construct() 22 { 23 // enable direct access to language strings 24 // ie $this->lang 25 $this->setupLocale(); 26 } 27 28 public function register(Doku_Event_Handler $controller) 29 { 30 31 /** 32 * Add canonical to javascript 33 */ 34 $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'addCanonicalToJavascript', array()); 35 36 } 37 38 39 40 /** 41 * Add the canonical value to JSON 42 * to be able to report only on canonical value and not on path 43 * @param $event 44 * @noinspection SpellCheckingInspection 45 */ 46 function addCanonicalToJavascript($event) 47 { 48 49 global $JSINFO; 50 $page = Page::createPageFromRequestedPage(); 51 if ($page->getCanonical() !== null) { 52 $JSINFO[Canonical::PROPERTY_NAME] = $page->getCanonical(); 53 if (isset($JSINFO["ga"]) && PluginUtility::getConfValue(self::CONF_CANONICAL_FOR_GA_PAGE_VIEW, 1)) { 54 // 55 // The path portion of a URL. This value should start with a slash (/) character. 56 // As said here 57 // https://developers.google.com/analytics/devguides/collection/analyticsjs/pages#pageview_fields 58 // 59 // 60 // For the modification instructions 61 // https://developers.google.com/analytics/devguides/collection/analyticsjs/pages#pageview_fields 62 $pageViewCanonical = str_replace(DokuPath::PATH_SEPARATOR, "/", $page->getCanonical()); 63 if ($pageViewCanonical[0] != "/") { 64 $pageViewCanonical = "/$pageViewCanonical"; 65 } 66 $JSINFO["ga"]["pageview"] = $pageViewCanonical; 67 } 68 } 69 } 70 71} 72