Skip to content

Commit

Permalink
- added session_key config for utm-parameters
Browse files Browse the repository at this point in the history
- added tests
  • Loading branch information
toni-suarez committed Jun 20, 2024
1 parent 6aecb5f commit 19f1122
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 15 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"description": "A little helper to store and handle utm-parameter",
"homepage": "https://github.com/toni-suarez/laravel-utm-parameter",
"license": "MIT",
"keywords": [
"php",
"laravel",
"utm-parameter",
"social-media",
"utm"
],
"authors": [
{
"name": "Toni Suarez",
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/UtmParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle(Request $request, Closure $next)
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Request
* @return bool
*/
protected function shouldAcceptUtmParameter(Request $request)
{
Expand Down
25 changes: 17 additions & 8 deletions src/UtmParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ class UtmParameter
*
* @var array
*/
public $parameters;
public array|null $parameters;

/**
* Utm Parameter Session Key.
*
* @var string
*/
public string $sessionKey;


public function __construct(array $parameters = [])
{
$this->sessionKey = config('utm-parameter.session_key');
$this->parameters = $parameters;
}

Expand All @@ -40,16 +49,16 @@ public function boot(Request $request)
public function useRequestOrSession(Request $request)
{
$currentRequestParameter = self::getParameter($request);
$sessionParameter = session('utm');
$sessionParameter = session($this->sessionKey);

if (!empty($currentRequestParameter) && empty($sessionParameter)) {
session(['utm' => $currentRequestParameter]);
session([$this->sessionKey => $currentRequestParameter]);
return $currentRequestParameter;
}

if (!empty($currentRequestParameter) && !empty($sessionParameter) && config('utm-parameter.override_utm_parameters')) {
$mergedParameters = array_merge($sessionParameter, $currentRequestParameter);
session(['utm' => $mergedParameters]);
session([$this->sessionKey => $mergedParameters]);
return $mergedParameters;
}

Expand All @@ -73,7 +82,7 @@ public static function all()
*
* @return string|null
*/
public static function get($key)
public static function get(string $key)
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
Expand All @@ -93,7 +102,7 @@ public static function get($key)
*
* @return bool
*/
public static function has($key, $value = null)
public static function has(string $key, $value = null)
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
Expand All @@ -116,7 +125,7 @@ public static function has($key, $value = null)
* @param string $value
* @return bool
*/
public static function contains($key, $value)
public static function contains(string $key, string $value)
{
$parameters = self::all();
$key = self::ensureUtmPrefix($key);
Expand All @@ -136,7 +145,7 @@ public static function contains($key, $value)
public static function clear()
{
app(UtmParameter::class)->parameters = null;
session()->forget('utm');
session()->forget(app(UtmParameter::class)->sessionKey);
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions src/config/utm-parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@
* - Disabled (false): The initial UTM parameters will persist throughout the session.
*/
'override_utm_parameters' => false,

/*
* Session Key for UTM Parameters (default: 'utm')
*
* This key specifies the name used to access and store UTM parameters within the session data.
*
* If you're already using 'utm' for another purpose in your application,
* you can customize this key to avoid conflicts.
* Simply provide your preferred key name as a string value.
*/
'session_key' => 'utm'
];
81 changes: 75 additions & 6 deletions tests/UtmParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

class UtmParameterTest extends TestCase
{
protected $sessionKey;
public function setUp(): void
{
parent::setUp();
Config::set('utm-parameter.override_utm_parameters', false);
Config::set('utm-parameter.session_key', 'custom_utm_key');
$this->sessionKey = Config::get('utm-parameter.session_key');

$parameters = [
'utm_source' => 'google',
Expand All @@ -26,7 +29,7 @@ public function setUp(): void

app()->singleton(UtmParameter::class, fn () => new UtmParameter());
app(UtmParameter::class)->boot($request);
session(['utm' => $parameters]);
session([$this->sessionKey => $parameters]);
}

public function test_it_should_be_bound_in_the_app()
Expand All @@ -35,6 +38,29 @@ public function test_it_should_be_bound_in_the_app()
$this->assertInstanceOf(UtmParameter::class, $utm);
}

public function test_it_should_have_a_session_key()
{
$this->assertIsString($this->sessionKey);
}

public function test_it_should_have_a_session()
{
$sessionContent = session($this->sessionKey);
$this->assertIsArray($sessionContent);
$this->assertArrayHasKey('utm_source', $sessionContent);
$this->assertIsNotString(session($this->sessionKey));
}

public function test_it_should_also_clear_a_session()
{
$sessionContent = session($this->sessionKey);
$this->assertIsArray($sessionContent);

$sessionEmptyContent = session()->forget($this->sessionKey);
$this->assertIsNotArray($sessionEmptyContent);
$this->assertNull($sessionEmptyContent);
}

public function test_it_should_have_an_utm_attribute_bag()
{
$utm = UtmParameter::all();
Expand All @@ -47,136 +73,153 @@ public function test_it_should_have_a_source_parameter()
{
$source = UtmParameter::get('source');
$this->assertNotEmpty($source);
$this->assertIsString($source);
$this->assertEquals('google', $source);
}

public function test_it_should_have_work_with_utm_inside_key()
{
$source = UtmParameter::get('utm_source');
$this->assertNotEmpty($source);
$this->assertIsString($source);
$this->assertEquals('google', $source);
}

public function test_it_should_have_a_medium_parameter()
{
$medium = UtmParameter::get('medium');
$this->assertNotEmpty($medium);
$this->assertIsString($medium);
$this->assertEquals('cpc', $medium);
}

public function test_it_should_have_a_campaign_parameter()
{
$campaign = UtmParameter::get('campaign');
$this->assertNotEmpty($campaign);
$this->assertIsString($campaign);
$this->assertEquals('{campaignid}', $campaign);
}

public function test_it_should_have_a_content_parameter()
{
$content = UtmParameter::get('content');
$this->assertNotEmpty($content);
$this->assertIsString($content);
$this->assertEquals('{adgroupid}', $content);
}

public function test_it_should_have_a_term_parameter()
{
$term = UtmParameter::get('term');
$this->assertNotEmpty($term);
$this->assertIsString($term);
$this->assertEquals('{targetid}', $term);
}

public function test_it_should_get_a_utm_parameter_via_helper()
{
$source = get_utm('source');
$this->assertNotEmpty($source);
$this->assertIsString($source);
$this->assertEquals('google', $source);
}

public function test_it_should_determine_if_utm_has_key()
{
$hasSource = UtmParameter::has('source');
$this->assertIsBool($hasSource);
$this->assertNotEmpty($hasSource);
$this->assertTrue($hasSource);
}

public function test_it_should_determine_if_utm_has_not_key()
{
$hasRandomKey = UtmParameter::has('random-key');
$this->assertIsBool($hasRandomKey);
$this->assertEmpty($hasRandomKey);
$this->assertFalse($hasRandomKey);
}

public function test_it_should_determine_if_utm_has_key_and_value()
{
$hasGoogleSource = UtmParameter::has('utm_source', 'google');
$this->assertIsBool($hasGoogleSource);
$this->assertNotEmpty($hasGoogleSource);
$this->assertTrue($hasGoogleSource);
}

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

public function test_it_should_determine_if_a_key_exists_for_utm_parameters()
{
$hasSource = has_utm('source');
$this->assertIsBool($hasSource);
$this->assertNotEmpty($hasSource);
$this->assertTrue($hasSource);
}

public function test_it_should_determine_if_a_utm_parameter_equals_a_value()
{
$isGoogle = has_utm('source', 'google');
$this->assertIsBool($isGoogle);
$this->assertNotEmpty($isGoogle);
$this->assertTrue($isGoogle);
}

public function test_it_should_determine_if_a_key_does_not_exists_for_utm_parameters()
{
$hasRandomKey = has_not_utm('random-key');
$this->assertIsBool($hasRandomKey);
$this->assertNotEmpty($hasRandomKey);
$this->assertTrue($hasRandomKey);
}

public function test_it_should_determine_if_a_utm_parameter_not_equals_a_value()
{
$isRandomSource = has_not_utm('source', 'random');
$this->assertIsBool($isRandomSource);
$this->assertNotEmpty($isRandomSource);
$this->assertTrue($isRandomSource);
}

public function test_it_should_determine_if_an_utm_contains_a_value()
{
$campaign = UtmParameter::contains('utm_campaign', 'campaign');
$this->assertIsBool($campaign);
$this->assertNotEmpty($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->assertEmpty($hasRandomCampaign);
$this->assertFalse($hasRandomCampaign);
}

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

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

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

$medium = UtmParameter::contains('utm_medium', 1);
$medium = UtmParameter::contains('utm_medium', '1');
$this->assertIsBool($medium);
$this->assertFalse($medium);
}
Expand All @@ -199,9 +242,11 @@ public function test_it_should_clear_and_remove_the_utm_parameter_again()
{
$source = UtmParameter::get('source');
$this->assertEquals('google', $source);
$this->assertArrayHasKey('utm_source', session($this->sessionKey));

UtmParameter::clear();
$emptySource = UtmParameter::get('source');
$this->assertNull(session($this->sessionKey));
$this->assertNull($emptySource);
}

Expand All @@ -217,7 +262,7 @@ public function test_it_should_overwrite_new_utm_parameter()
'utm_medium' => 'email'
];

$request = Request::create('/test', 'GET', $parameters);
$request = Request::create('/', 'GET', $parameters);
app(UtmParameter::class)->boot($request);

$source = UtmParameter::get('source');
Expand Down Expand Up @@ -245,6 +290,14 @@ public function test_it_should_keep_existing_parameters()
$request = Request::create('/test', 'GET', $parameters);
app(UtmParameter::class)->boot($request);

$id = UtmParameter::get('id');
$this->assertEmpty($id);
$this->assertNull($id);

$sorting = UtmParameter::get('sorting');
$this->assertEmpty($sorting);
$this->assertNull($sorting);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);

Expand All @@ -264,13 +317,29 @@ public function test_it_should_keep_existing_parameters_while_browsing()
$request = Request::create('/new-page', 'GET', $parameters);
app(UtmParameter::class)->boot($request);

$id = UtmParameter::get('id');
$this->assertEmpty($id);
$this->assertNull($id);

$sorting = UtmParameter::get('sorting');
$this->assertEmpty($sorting);
$this->assertNull($sorting);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);

$parameters = [];
$request = Request::create('/second-page', 'GET', $parameters);
app(UtmParameter::class)->boot($request);

$id = UtmParameter::get('id');
$this->assertEmpty($id);
$this->assertNull($id);

$sorting = UtmParameter::get('sorting');
$this->assertEmpty($sorting);
$this->assertNull($sorting);

$source = UtmParameter::get('source');
$this->assertEquals('google', $source);
}
Expand Down

0 comments on commit 19f1122

Please sign in to comment.