Skip to content

Commit

Permalink
[FEATURE] Support mtm_ for utm_ parameters
Browse files Browse the repository at this point in the history
Before this commit only utm-GET-params were stored in UTM table. So utm_campaign value was stored in tx_lux_domain_model_utm.utm_campaign.
Now, also mtm_campaign is stored in the same column. utm_ and mtm_ parameters are merged. First win.

Respected GET-params are:
utm_sour
mtm_sour
utm_medi
mtm_medi
utm_camp
mtm_camp
utm_id',
mtm_id',
utm_term
mtm_term
utm_cont
mtm_cont

Related: https://projekte.in2code.de/issues/61371
  • Loading branch information
einpraegsam committed Jan 19, 2024
1 parent 0dbc9b3 commit 3ffd25a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
59 changes: 52 additions & 7 deletions Classes/Domain/Factory/UtmFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@
class UtmFactory
{
protected array $utmKeys = [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_id',
'utm_term',
'utm_content',
'utm_source' => [
'utm_source',
'mtm_source',
],
'utm_medium' => [
'utm_medium',
'mtm_medium',
],
'utm_campaign' => [
'utm_campaign',
'mtm_campaign',
],
'utm_id' => [
'utm_id',
'mtm_id',
],
'utm_term' => [
'utm_term',
'mtm_term',
],
'utm_content' => [
'utm_content',
'mtm_content',
],
];

/**
Expand All @@ -25,7 +43,7 @@ public function get(array $parameters, string $referrer): Utm
{
$utm = GeneralUtility::makeInstance(Utm::class);
$utm->setReferrer($referrer);
foreach ($this->getUtmKeys() as $key) {
foreach ($this->getUtmKeysForDatabase() as $key) {
if (array_key_exists($key, $parameters) === false) {
throw new ParametersException($key . ' is not existing in given array', 1666207599);
}
Expand All @@ -40,4 +58,31 @@ public function getUtmKeys(): array
{
return $this->utmKeys;
}

public function getUtmKeysForDatabase(): array
{
return array_keys($this->utmKeys);
}

/**
* Example return:
* [
* "utm_source",
* "mtm_source",
* "utm_campaign",
* ...
* ]
*
* @return array
*/
public function getAllUtmKeys(): array
{
$keys = [];
foreach ($this->utmKeys as $keysSub) {
foreach ($keysSub as $key) {
$keys[] = $key;
}
}
return $keys;
}
}
10 changes: 7 additions & 3 deletions Classes/Domain/Tracker/UtmTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public function trackNews(NewsTrackerEvent $event): void
protected function getUtm(array $arguments): Utm
{
$parameters = [];
foreach ($this->utmFactory->getUtmKeys() as $key) {
$parameters[$key] = $this->getArgumentFromCurrentUrl($key);
foreach ($this->utmFactory->getUtmKeys() as $key => $keys) {
foreach ($keys as $keySub) {
if (isset($parameters[$key]) === false) {
$parameters[$key] = $this->getArgumentFromCurrentUrl($keySub);
}
}
}
return $this->utmFactory->get($parameters, $arguments['referrer'] ?? '');
}
Expand All @@ -82,7 +86,7 @@ protected function isAnyUtmParameterGiven(): bool
{
$arguments = $this->getArgumentsFromCurrentUrl();
foreach (array_keys($arguments) as $key) {
if (in_array($key, $this->utmFactory->getUtmKeys())) {
if (in_array($key, $this->utmFactory->getAllUtmKeys())) {
return true;
}
}
Expand Down
31 changes: 29 additions & 2 deletions Tests/Unit/Domain/Factory/UtmFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testGetUtmKeysEmptyArray(): void
* @return void
* @covers ::get
*/
public function testGetUtmKeys(): void
public function testGet(): void
{
$parameters = [
'utm_source' => 'source',
Expand All @@ -69,11 +69,38 @@ public function testGetUtmKeys(): void
* @return void
* @covers ::getUtmKeys
*/
public function testGet(): void
public function testGetUtmKeys(): void
{
self::assertSame(
$this->generalValidatorMock->_get('utmKeys'),
$this->generalValidatorMock->_call('getUtmKeys')
);
}

/**
* @return void
* @covers ::getUtmKeysForDatabase
*/
public function testGetUtmKeysForDatabase(): void
{
self::assertSame(
array_keys($this->generalValidatorMock->_get('utmKeys')),
$this->generalValidatorMock->_call('getUtmKeysForDatabase')
);
}

/**
* @return void
* @covers ::getAllUtmKeys
*/
public function testGetAllUtmKeys(): void
{
$allKeys = [];
foreach ($this->generalValidatorMock->_get('utmKeys') as $keys) {
foreach ($keys as $key) {
$allKeys[] = $key;
}
}
self::assertSame($allKeys, $this->generalValidatorMock->_call('getAllUtmKeys'));
}
}

0 comments on commit 3ffd25a

Please sign in to comment.