Skip to content

Commit

Permalink
- added contains parameter for utm-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-suarez committed Jun 18, 2024
1 parent a8fa8ea commit 1ae9b2d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Tags/Utm.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public function get()
public function has()
{
$type = $this->params->get('type', 'source');
$value = $this->params->get('value', null);

if ($this->params->has('contains')) {
$value = $this->params->get('contains');
return UtmParameter::contains($type, $value);
}

$value = $this->params->get('value', null);
return UtmParameter::has($type, $value);
}
}
27 changes: 27 additions & 0 deletions src/UtmParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ public static function get($key)
return $parameters[$key];
}


/**
* Determine if a value contains inside the key
*
* @param string $key
* @param string $value
* @return bool
*/
public static function contains($key, $value)
{
$parameters = self::all();

if (strpos($key, 'utm_') === false) {
$key = 'utm_'.$key;
}

if (!array_key_exists($key, $parameters) || !is_string($value)) {
return false;
}

if (!array_key_exists($key, $parameters) && $value !== null) {
return false;
}

return str_contains(self::get($key), $value);
}

/**
* Determine if a UTM-Parameter exists.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/UtmParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,50 @@ public function test_it_should_determine_if_utm_has_key_and_value()
$this->assertTrue($hasGoogleSource);
}

public function test_it_should_determine_if_utm_has_not_a_not_defined_utm_parameter()
{
$hasWrongParameter = UtmParameter::has('utm_something');
$this->assertIsBool($hasWrongParameter);
$this->assertFalse($hasWrongParameter);
}

public function test_it_should_determine_if_utm_has_not_key_and_value()
{
$hasRandomSource = UtmParameter::has('random-source', 'random-value');
$this->assertIsBool($hasRandomSource);
$this->assertFalse($hasRandomSource);
}

public function test_it_should_determine_if_an_utm_contains_a_value()
{
$campaign = UtmParameter::contains('utm_campaign', 'campaign');
$this->assertIsBool($campaign);
$this->assertTrue($campaign);
}

public function test_it_should_determine_if_an_utm_contains_not_a_value()
{
$hasRandomCampaign = UtmParameter::contains('utm_campaign', 'some-thing');
$this->assertIsBool($hasRandomCampaign);
$this->assertFalse($hasRandomCampaign);
}

public function test_it_should_determine_if_an_utm_contains_a_non_string_value()
{
$campaign = UtmParameter::contains('utm_campaign', null);
$this->assertIsBool($campaign);
$this->assertFalse($campaign);

$term = UtmParameter::contains('utm_term', false);
$this->assertIsBool($term);
$this->assertFalse($term);

$content = UtmParameter::contains('utm_content', []);
$this->assertIsBool($content);
$this->assertFalse($content);

$medium = UtmParameter::contains('utm_medium', 1);
$this->assertIsBool($medium);
$this->assertFalse($medium);
}
}

0 comments on commit 1ae9b2d

Please sign in to comment.