diff --git a/composer.json b/composer.json index e52084b..30fda17 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Middleware/UtmParameters.php b/src/Middleware/UtmParameters.php index a645c0b..50806c4 100644 --- a/src/Middleware/UtmParameters.php +++ b/src/Middleware/UtmParameters.php @@ -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) { diff --git a/src/UtmParameter.php b/src/UtmParameter.php index 44d4533..01a7002 100644 --- a/src/UtmParameter.php +++ b/src/UtmParameter.php @@ -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; } @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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; } diff --git a/src/config/utm-parameter.php b/src/config/utm-parameter.php index 027083a..1820930 100644 --- a/src/config/utm-parameter.php +++ b/src/config/utm-parameter.php @@ -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' ]; diff --git a/tests/UtmParameterTest.php b/tests/UtmParameterTest.php index 0db147e..de127ee 100644 --- a/tests/UtmParameterTest.php +++ b/tests/UtmParameterTest.php @@ -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', @@ -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() @@ -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(); @@ -47,6 +73,7 @@ public function test_it_should_have_a_source_parameter() { $source = UtmParameter::get('source'); $this->assertNotEmpty($source); + $this->assertIsString($source); $this->assertEquals('google', $source); } @@ -54,6 +81,7 @@ 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); } @@ -61,6 +89,7 @@ public function test_it_should_have_a_medium_parameter() { $medium = UtmParameter::get('medium'); $this->assertNotEmpty($medium); + $this->assertIsString($medium); $this->assertEquals('cpc', $medium); } @@ -68,6 +97,7 @@ public function test_it_should_have_a_campaign_parameter() { $campaign = UtmParameter::get('campaign'); $this->assertNotEmpty($campaign); + $this->assertIsString($campaign); $this->assertEquals('{campaignid}', $campaign); } @@ -75,6 +105,7 @@ public function test_it_should_have_a_content_parameter() { $content = UtmParameter::get('content'); $this->assertNotEmpty($content); + $this->assertIsString($content); $this->assertEquals('{adgroupid}', $content); } @@ -82,6 +113,7 @@ public function test_it_should_have_a_term_parameter() { $term = UtmParameter::get('term'); $this->assertNotEmpty($term); + $this->assertIsString($term); $this->assertEquals('{targetid}', $term); } @@ -89,6 +121,7 @@ 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); } @@ -96,6 +129,7 @@ public function test_it_should_determine_if_utm_has_key() { $hasSource = UtmParameter::has('source'); $this->assertIsBool($hasSource); + $this->assertNotEmpty($hasSource); $this->assertTrue($hasSource); } @@ -103,6 +137,7 @@ 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); } @@ -110,6 +145,7 @@ 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); } @@ -117,6 +153,7 @@ 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); } @@ -124,6 +161,7 @@ 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); } @@ -131,6 +169,7 @@ 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); } @@ -138,6 +177,7 @@ public function test_it_should_determine_if_a_key_does_not_exists_for_utm_parame { $hasRandomKey = has_not_utm('random-key'); $this->assertIsBool($hasRandomKey); + $this->assertNotEmpty($hasRandomKey); $this->assertTrue($hasRandomKey); } @@ -145,6 +185,7 @@ 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); } @@ -152,6 +193,7 @@ 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); } @@ -159,24 +201,25 @@ 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); } @@ -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); } @@ -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'); @@ -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); @@ -264,6 +317,14 @@ 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); @@ -271,6 +332,14 @@ public function test_it_should_keep_existing_parameters_while_browsing() $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); }