mirror of
https://github.com/werkraum-media/abtest.git
synced 2024-11-14 18:16:10 +01:00
Code improvements
This commit is contained in:
parent
8570c2e3cb
commit
5d57e42391
7 changed files with 154 additions and 125 deletions
|
@ -9,6 +9,7 @@ namespace WapplerSystems\ABTest2;
|
||||||
* LICENSE.txt file that was distributed with this source code.
|
* LICENSE.txt file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use TYPO3\CMS\Core\Utility\DebugUtility;
|
||||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
|
||||||
use TYPO3\CMS\Frontend\Page\PageRepository;
|
use TYPO3\CMS\Frontend\Page\PageRepository;
|
||||||
|
@ -22,27 +23,6 @@ use TYPO3\CMS\Frontend\Page\PageRepository;
|
||||||
class Helper
|
class Helper
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var int|null */
|
|
||||||
protected $currentPageId = null;
|
|
||||||
|
|
||||||
/** @var int|null */
|
|
||||||
protected $rootpage_id = null;
|
|
||||||
|
|
||||||
/** @var array */
|
|
||||||
protected $realurlConfig = null;
|
|
||||||
|
|
||||||
/** @var int|null */
|
|
||||||
protected $selectBSite = null;
|
|
||||||
|
|
||||||
/** @var int|null */
|
|
||||||
protected $cookieLifeTime = null;
|
|
||||||
|
|
||||||
/** @var int|null */
|
|
||||||
protected $randomAbPageId = null;
|
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
protected $additionalHeaderData;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param array $params
|
* @param array $params
|
||||||
|
@ -56,20 +36,20 @@ class Helper
|
||||||
// only try to change the page if it's not the googlebot.
|
// only try to change the page if it's not the googlebot.
|
||||||
if (false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) {
|
if (false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) {
|
||||||
|
|
||||||
$this->currentPageId = $pObj->id;
|
$currentPageId = $randomPageId = $pObj->id;
|
||||||
|
|
||||||
// Get the rootpage_id from realurl config.
|
// Get the rootpage_id from realurl config.
|
||||||
$this->realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl'];
|
$realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl'];
|
||||||
if (array_key_exists($_SERVER['SERVER_NAME'], $this->realurlConfig)) {
|
if (array_key_exists($_SERVER['SERVER_NAME'], $realurlConfig)) {
|
||||||
$this->rootpage_id = $this->realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id'];
|
$rootpage_id = $realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id'];
|
||||||
} else {
|
} else {
|
||||||
$this->rootpage_id = $this->realurlConfig['_DEFAULT']['pagePath']['rootpage_id'];
|
$rootpage_id = $realurlConfig['_DEFAULT']['pagePath']['rootpage_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the ID is NULL, then we set this value to the rootpage_id. NULL is the "Home"page, ID is a specific sub-page, e.g. www.domain.de (NULL) - www.domain.de/page.html (ID)
|
// If the ID is NULL, then we set this value to the rootpage_id. NULL is the "Home"page, ID is a specific sub-page, e.g. www.domain.de (NULL) - www.domain.de/page.html (ID)
|
||||||
if (!$this->currentPageId) {
|
if (!$currentPageId) {
|
||||||
if ($this->rootpage_id) {
|
if ($rootpage_id) {
|
||||||
$this->currentPageId = $this->rootpage_id;
|
$currentPageId = $rootpage_id;
|
||||||
} else {
|
} else {
|
||||||
// Leave the function because we can not determine the ID.
|
// Leave the function because we can not determine the ID.
|
||||||
return;
|
return;
|
||||||
|
@ -77,37 +57,62 @@ class Helper
|
||||||
}
|
}
|
||||||
|
|
||||||
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
|
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
|
||||||
$currentPagePropertiesArray = $pageRepository->getPage($this->currentPageId);
|
$currentPagePropertiesArray = $pageRepository->getPage($currentPageId);
|
||||||
|
|
||||||
$this->selectBSite = $currentPagePropertiesArray['tx_abtest2_b_id'];
|
$pageBPageId = $currentPagePropertiesArray['tx_abtest2_b_id'];
|
||||||
$this->cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time'];
|
$cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time'];
|
||||||
|
|
||||||
if ($this->selectBSite) {
|
if ($pageBPageId) {
|
||||||
if ((int)$_COOKIE['abtest2-' . $this->currentPageId] > 0) {
|
/* page b id exists */
|
||||||
$this->randomAbPageId = (int)$_COOKIE['abtest2-' . $this->currentPageId];
|
$cookiePageId = (int)$_COOKIE['abtest2-' . $currentPageId];
|
||||||
|
|
||||||
|
if ($cookiePageId > 0 && ($cookiePageId === $pageBPageId || $cookiePageId === $currentPageId)) {
|
||||||
|
/* valid cookie page id -> select cookie page id */
|
||||||
|
$randomPageId = $cookiePageId;
|
||||||
} else {
|
} else {
|
||||||
$randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site.
|
/* select least used page */
|
||||||
if ($randomPage) {
|
$pageBPropertiesArray = $pageRepository->getPage($pageBPageId);
|
||||||
$this->randomAbPageId = $this->selectBSite;
|
if ((int)$currentPagePropertiesArray['tx_abtest2_counter'] > (int)$pageBPropertiesArray['tx_abtest2_counter']) {
|
||||||
|
$randomPageId = $pageBPageId;
|
||||||
|
$currentPagePropertiesArray = $pageBPropertiesArray;
|
||||||
|
|
||||||
|
} elseif ((int)$currentPagePropertiesArray['tx_abtest2_counter'] < (int)$pageBPropertiesArray['tx_abtest2_counter']) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->randomAbPageId = $this->currentPageId;
|
/* random */
|
||||||
|
$randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site.
|
||||||
|
if ($randomPage) {
|
||||||
|
$randomPageId = $pageBPageId;
|
||||||
|
$currentPagePropertiesArray = $pageBPropertiesArray;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setcookie('abtest2-' . $this->currentPageId, $this->randomAbPageId, time() + $this->cookieLifeTime);
|
|
||||||
|
/* rise counter */
|
||||||
|
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid='. (int)$randomPageId, array('tx_abtest2_counter' => $currentPagePropertiesArray['tx_abtest2_counter'] + 1));
|
||||||
|
|
||||||
|
setcookie('abtest2-' . $currentPageId, $randomPageId, time() + $cookieLifeTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If current page ID is different from the random page ID we set the correct page ID.
|
// If current page ID is different from the random page ID we set the correct page ID.
|
||||||
if ($this->currentPageId !== $this->randomAbPageId) {
|
if ($currentPageId !== $randomPageId) {
|
||||||
$pObj->contentPid = $this->randomAbPageId;
|
$pObj->contentPid = $randomPageId;
|
||||||
$GLOBALS['TSFE']->page['content_from_pid'] = $this->randomAbPageId;
|
$pObj->page['content_from_pid'] = $randomPageId;
|
||||||
$GLOBALS['TSFE']->page['no_cache'] = true;
|
}
|
||||||
|
|
||||||
|
$pObj->page['no_cache'] = true;
|
||||||
|
|
||||||
|
|
||||||
|
if ($currentPagePropertiesArray) {
|
||||||
|
$additionalHeaderData = $currentPagePropertiesArray['tx_abtest2_header'];
|
||||||
|
$additionalFooterData = $currentPagePropertiesArray['tx_abtest2_footer'];
|
||||||
|
if ($additionalHeaderData) {
|
||||||
|
$pObj->additionalHeaderData['abtest2'] = $additionalHeaderData;
|
||||||
|
}
|
||||||
|
if ($additionalFooterData) {
|
||||||
|
$pObj->additionalFooterData['abtest2'] = $additionalFooterData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// If additional headerdata is present then we specify additionalHeaderData.
|
|
||||||
$randomPagePropertiesArray = $pageRepository->getPage($this->randomAbPageId);
|
|
||||||
$this->additionalHeaderData = $randomPagePropertiesArray['tx_abtest2_header'];
|
|
||||||
if ($this->additionalHeaderData) {
|
|
||||||
$GLOBALS['TSFE']->additionalHeaderData['abtest2'] = $this->additionalHeaderData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,11 +35,18 @@
|
||||||
'type' => 'text'
|
'type' => 'text'
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
|
'tx_abtest2_footer' => array(
|
||||||
|
'exclude' => 1,
|
||||||
|
'label' => 'LLL:EXT:abtest2/Resources/Private/Language/locallang_db.xlf:pages.tx_abtest2_footer',
|
||||||
|
'config' => array(
|
||||||
|
'type' => 'text'
|
||||||
|
)
|
||||||
|
),
|
||||||
'tx_abtest2_counter' => array(
|
'tx_abtest2_counter' => array(
|
||||||
'exclude' => 1,
|
'exclude' => 1,
|
||||||
'label' => 'LLL:EXT:abtest2/Resources/Private/Language/locallang_db.xlf:pages.tx_abtest2_counter',
|
'label' => 'LLL:EXT:abtest2/Resources/Private/Language/locallang_db.xlf:pages.tx_abtest2_counter',
|
||||||
'config' => array(
|
'config' => array(
|
||||||
'type' => 'text'
|
'type' => 'input'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,48 +1,57 @@
|
||||||
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
||||||
<xliff version="1.0">
|
<xliff version="1.0">
|
||||||
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z" product-name="abtest2">
|
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z"
|
||||||
|
product-name="abtest2">
|
||||||
<header/>
|
<header/>
|
||||||
<body>
|
<body>
|
||||||
<trans-unit id="pages.tx_abtest2_b_id">
|
<trans-unit id="pages.tx_abtest2_b_id">
|
||||||
<source>Site B</source>
|
<source>Site B</source>
|
||||||
<target>Seite B</target>
|
<target>Seite B</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="pages.tx_abtest2_cookie_time">
|
<trans-unit id="pages.tx_abtest2_cookie_time">
|
||||||
<source>Cookie Lifetime</source>
|
<source>Cookie Lifetime</source>
|
||||||
<target>Cookie Lebenszeit</target>
|
<target>Cookie Lebenszeit</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="pages.tx_abtest2_header">
|
<trans-unit id="pages.tx_abtest2_header">
|
||||||
<source>Additional Header Information</source>
|
<source>Additional Header Information</source>
|
||||||
<target>Zusätzliche Header Informationen</target>
|
<target>Zusätzliche Header Informationen</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="palette_title">
|
<trans-unit id="pages.tx_abtest2_footer">
|
||||||
<source>AB Test Settings</source>
|
<source>Additional footer information</source>
|
||||||
<target>AB Test Einstellungen</target>
|
<target>Zusätzliche Footer Informationen</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_month">
|
<trans-unit id="pages.tx_abtest2_counter">
|
||||||
<source>1 month</source>
|
<source>Counter</source>
|
||||||
<target>1 Monat</target>
|
<target>Zähler</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_week">
|
<trans-unit id="palette_title">
|
||||||
<source>1 week</source>
|
<source>AB Test Settings</source>
|
||||||
<target>1 Woche</target>
|
<target>AB Test Einstellungen</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_day">
|
<trans-unit id="cookie_1_month">
|
||||||
<source>1 day</source>
|
<source>1 month</source>
|
||||||
<target>1 Tag</target>
|
<target>1 Monat</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_12_day">
|
<trans-unit id="cookie_1_week">
|
||||||
<source>1/2 day</source>
|
<source>1 week</source>
|
||||||
<target>1/2 Tag</target>
|
<target>1 Woche</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_hour">
|
<trans-unit id="cookie_1_day">
|
||||||
<source>1 hour</source>
|
<source>1 day</source>
|
||||||
<target>1 Stunde</target>
|
<target>1 Tag</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_minute">
|
<trans-unit id="cookie_12_day">
|
||||||
<source>1 minute</source>
|
<source>1/2 day</source>
|
||||||
<target>1 Minute</target>
|
<target>1/2 Tag</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="cookie_1_hour">
|
||||||
|
<source>1 hour</source>
|
||||||
|
<target>1 Stunde</target>
|
||||||
|
</trans-unit>
|
||||||
|
<trans-unit id="cookie_1_minute">
|
||||||
|
<source>1 minute</source>
|
||||||
|
<target>1 Minute</target>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
|
@ -1,38 +1,45 @@
|
||||||
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
||||||
<xliff version="1.0">
|
<xliff version="1.0">
|
||||||
<file source-language="en" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z" product-name="abtest2">
|
<file source-language="en" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z"
|
||||||
|
product-name="abtest2">
|
||||||
<header/>
|
<header/>
|
||||||
<body>
|
<body>
|
||||||
<trans-unit id="pages.tx_abtest2_b_id">
|
<trans-unit id="pages.tx_abtest2_b_id">
|
||||||
<source>Site B</source>
|
<source>Site B</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="pages.tx_abtest2_cookie_time">
|
<trans-unit id="pages.tx_abtest2_cookie_time">
|
||||||
<source>Cookie Lifetime</source>
|
<source>Cookie Lifetime</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="pages.tx_abtest2_header">
|
<trans-unit id="pages.tx_abtest2_header">
|
||||||
<source>Additional Header Information</source>
|
<source>Additional header information</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="palette_title">
|
<trans-unit id="pages.tx_abtest2_footer">
|
||||||
<source>AB Test Settings</source>
|
<source>Additional footer information</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_month">
|
<trans-unit id="pages.tx_abtest2_counter">
|
||||||
<source>1 month</source>
|
<source>Counter</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_week">
|
<trans-unit id="palette_title">
|
||||||
<source>1 week</source>
|
<source>AB Test Settings</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_day">
|
<trans-unit id="cookie_1_month">
|
||||||
<source>1 day</source>
|
<source>1 month</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_12_day">
|
<trans-unit id="cookie_1_week">
|
||||||
<source>1/2 day</source>
|
<source>1 week</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_hour">
|
<trans-unit id="cookie_1_day">
|
||||||
<source>1 hour</source>
|
<source>1 day</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="cookie_1_minute">
|
<trans-unit id="cookie_12_day">
|
||||||
<source>1 minute</source>
|
<source>1/2 day</source>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
|
<trans-unit id="cookie_1_hour">
|
||||||
|
<source>1 hour</source>
|
||||||
|
</trans-unit>
|
||||||
|
<trans-unit id="cookie_1_minute">
|
||||||
|
<source>1 minute</source>
|
||||||
|
</trans-unit>
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
$EM_CONF[$_EXTKEY] = array(
|
$EM_CONF[$_EXTKEY] = array(
|
||||||
'title' => 'AB Test Pages',
|
'title' => 'AB Test Pages',
|
||||||
'description' => 'With this extension, administrators can deliver different content for the same URL (AB test), depending on cookies or parameters.',
|
'description' => 'With this extension, administrators can deliver different content for the same URL (AB test), depending on cookies or least showed page to realize a most accurate possible test.',
|
||||||
'category' => 'misc',
|
'category' => 'misc',
|
||||||
'author' => 'Sven Wappler',
|
'author' => 'Sven Wappler',
|
||||||
'author_email' => 'typo3YYYY@wappler.systems',
|
'author_email' => 'typo3YYYY@wappler.systems',
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
defined('TYPO3_MODE') or die();
|
defined('TYPO3_MODE') or die();
|
||||||
|
|
||||||
|
|
||||||
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->SelectId';
|
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->determineContentId';
|
|
@ -2,5 +2,6 @@ CREATE TABLE pages (
|
||||||
tx_abtest2_b_id int(11) DEFAULT '0' NOT NULL,
|
tx_abtest2_b_id int(11) DEFAULT '0' NOT NULL,
|
||||||
tx_abtest2_cookie_time int(11) DEFAULT '86400' NOT NULL,
|
tx_abtest2_cookie_time int(11) DEFAULT '86400' NOT NULL,
|
||||||
tx_abtest2_header text,
|
tx_abtest2_header text,
|
||||||
|
tx_abtest2_footer text,
|
||||||
tx_abtest2_counter int(11) DEFAULT '0' NOT NULL
|
tx_abtest2_counter int(11) DEFAULT '0' NOT NULL
|
||||||
);
|
);
|
Loading…
Reference in a new issue