From 818602a1e0d0b0dcb87f57669054b7a329c0909b Mon Sep 17 00:00:00 2001 From: Sven Wappler Date: Mon, 11 Dec 2017 19:45:03 +0100 Subject: [PATCH] Simplified; Sitewide cookie for a a/b test for the whole website --- Classes/Helper.php | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Classes/Helper.php b/Classes/Helper.php index fdca16d..cb65d13 100644 --- a/Classes/Helper.php +++ b/Classes/Helper.php @@ -35,7 +35,7 @@ class Helper // only try to change the page if it's not the googlebot. if (false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) { - $currentPageId = $randomPageId = $pObj->id; + $currentPageId = $targetPageId = $pObj->id; // Get the rootpage_id from realurl config. $realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl']; @@ -59,43 +59,38 @@ class Helper $currentPagePropertiesArray = $pageRepository->getPage($currentPageId); $pageBPageId = $currentPagePropertiesArray['tx_abtest2_b_id']; + /* TODO: check if page b exists */ $cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time']; if ($pageBPageId) { - /* page b id exists */ - $cookiePageId = (int)$_COOKIE['abtest2-' . $currentPageId]; - if ($cookiePageId > 0 && ($cookiePageId === $pageBPageId || $cookiePageId === $currentPageId)) { - /* valid cookie page id -> select cookie page id */ - $randomPageId = $cookiePageId; + $cookieValue = $_COOKIE['abtest2']; + + if ($cookieValue === 'b') { + $targetPageId = $pageBPageId; + } else if ($cookieValue === 'a') { + } else { + $cookieValue = 'a'; /* select least used page */ $pageBPropertiesArray = $pageRepository->getPage($pageBPageId); if ((int)$currentPagePropertiesArray['tx_abtest2_counter'] > (int)$pageBPropertiesArray['tx_abtest2_counter']) { - $randomPageId = $pageBPageId; + /* take b */ + $targetPageId = $pageBPageId; $currentPagePropertiesArray = $pageBPropertiesArray; - - } elseif ((int)$currentPagePropertiesArray['tx_abtest2_counter'] < (int)$pageBPropertiesArray['tx_abtest2_counter']) { - - } else { - /* random */ - $randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site. - if ($randomPage) { - $randomPageId = $pageBPageId; - $currentPagePropertiesArray = $pageBPropertiesArray; - } + $cookieValue = 'b'; } /* rise counter */ - $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid='. (int)$randomPageId, array('tx_abtest2_counter' => $currentPagePropertiesArray['tx_abtest2_counter'] + 1)); + $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid='. (int)$targetPageId, array('tx_abtest2_counter' => $currentPagePropertiesArray['tx_abtest2_counter'] + 1)); - setcookie('abtest2-' . $currentPageId, $randomPageId, time() + $cookieLifeTime); + setcookie('abtest2', $cookieValue, time() + $cookieLifeTime); } // If current page ID is different from the random page ID we set the correct page ID. - if ($currentPageId !== $randomPageId) { - $pObj->contentPid = $randomPageId; - $pObj->page['content_from_pid'] = $randomPageId; + if ($currentPageId !== $targetPageId) { + $pObj->contentPid = $targetPageId; + $pObj->page['content_from_pid'] = $targetPageId; } $pObj->page['no_cache'] = true;