diff --git a/README.md b/README.md index 272b87a..98f6954 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ ![GitHub](https://img.shields.io/github/license/toni-suarez/laravel-utm-parameter) [![Statamic Addon](https://img.shields.io/badge/https%3A%2F%2Fstatamic.com%2Faddons%2Ftoni-suarez%2Futm-parameter?style=flat-square&logo=statamic&logoColor=rgb(255%2C%2038%2C%20158)&label=Statamic&link=https%3A%2F%2Fstatamic.com%2Faddons%2Ftoni-suarez%2Futm-parameter)](https://statamic.com/addons/toni-suarez/utm-parameter) - A lightweight way to handle UTM parameters session-based in your Laravel Application. ```blade @@ -28,9 +27,14 @@ Open your terminal and navigate to your Laravel project directory. Then, use Com $ composer require suarez/laravel-utm-parameter ``` +Optionally, you can publish the config file of this package with this command: +```bash +php artisan vendor:publish --tag="utm-parameter" +``` + ### Middleware Configuration -Once the package is installed, you need to add the UtmParameters middleware to your Laravel application. Open the `bootstrap/app.php` file and append the `UtmParameters::class` inside the web-group. +Once the package is installed, you can add the UtmParameters middleware to your Laravel application. Open the `bootstrap/app.php` file and append the `UtmParameters::class` inside the web-group. ```php # Laravel 11 @@ -45,33 +49,86 @@ return Application::configure(basePath: dirname(__DIR__)) ... ``` -### Middleware Alias (Optional) +Also, take a look at how to set an [alias for the middleware](https://github.com/toni-suarez/laravel-utm-parameter/wiki/Installation-Guide#step-3-alias-configuration-optional). -To enable UTM-Parameters only for certain requests or routes in your application, you can add an alias for the UtmParameters middleware. Open the bootstrap/app.php file and append the `UtmParameters::class` inside the web-group. +### Use as Facade +If you prefer not to use it as middleware, you can utilize the UtmParameter Facade directly in your controllers or other parts of your application. Once the `boot($request)`-method is called, you have access to it at any place. For example: ```php -# Laravel 11 -use Suarez\UtmParameter\Middleware\UtmParameters; +use Suarez\UtmParameter\Facades\UtmParameter; + +// Inside a controller method +class IndexController { + public function index(Request $request) + { + UtmParameter::boot($request); + } +} -->withMiddleware(function (Middleware $middleware) { - $middleware - ->alias([ - /* ... keep the existing mappings here */ - 'utm-parameters' => UtmParameters::class, - ]) - ->web(append: [ - /* ... keep the existing mappings here */ - UtmParameters::class - ]); -}) +class SomeDifferentController { + public function index(Request $request) + { + $source = UtmParameter::get('source'); + } +} ``` -To apply UTM-Parameters to specific routes, use the following middleware: `utm-parameters` +## Configuration + +The configuration file `config/utm-parameter.php` allows you to control the behavior of the UTM parameters handling. ```php -Route::middleware('utm-parameters') - ->get('landing-page/{slug}', 'LandingPageController@show'); + 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', + + /* + * Allowed UTM Parameters (default: ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_campaign_id']) + * + * This setting defines the UTM parameters that are allowed within your application. + * + * In this array, you can specify a list of allowed UTM parameter names. Each parameter should be listed as a string. + * Only parameters from this list will be stored and processed in the session. + * and any parameter without the 'utm_' prefix will be ignored regardless of its inclusion in this list. + * + * Example: To only allow the basic UTM parameters (source, medium, and campaign), you could update the array like this: + * + * 'allowed_utm_parameters' => [ + * 'utm_source', + * 'utm_medium', + * 'utm_campaign', + * ], + */ + 'allowed_utm_parameters' => [ + 'utm_source', + 'utm_medium', + 'utm_campaign', + 'utm_term', + 'utm_content', + 'utm_campaign_id' + ], +]; ``` ## Usage @@ -142,6 +199,83 @@ Simply use: } ``` +### contains_utm() + +You can conditionally show or perform actions based on the presence or absence of a portion of an UTM parameters using contains. + +Simply use: +- `contains_utm('source|medium|campaign|term|content', 'value')` +- `contains_not_utm('source|medium|campaign|term|content', 'value')` + +```blade +@containsUtm('campaign', 'weekly') +
Some Weekly related stuff
+@endhasUtm + +@containsNotUtm('campaign', 'sales') +

Some not Sales stuff

+@endhasNotUtm +``` + +```php + if (contains_utm('campaign', 'weekly')) { + redirect('to/special/page'); + } + + if (contains_not_utm('campaign', 'sale')) { + session()->flash('Did you know, we have a newsletter?'); + } +``` + +## Extending the Middleware + +You can extend the middleware to customize the behavior of accepting UTM parameters. For example, you can override the `shouldAcceptUtmParameter` method. + +First, create a new middleware using Artisan: + +```bash +php artisan make:middleware CustomMiddleware +``` + +Then, update the new middleware to extend UtmParameters and override the `shouldAcceptUtmParameter` method: + +```php +isMethod('GET') || $request->isMethod('POST'); + } +} +``` + +Finally, update your `bootstrap/app.php` to use the CustomMiddleware: + +```php +# bootstrap/app.php +use App\Http\Middleware\CustomMiddleware; + +->withMiddleware(function (Middleware $middleware) { + $middleware->web(append: [ + CustomMiddleware::class, + // other middleware... + ]); +}) +``` ## Resources Explore additional use cases and resources on the [wiki pages](https://github.com/toni-suarez/laravel-utm-parameter/wiki) diff --git a/composer.json b/composer.json index e52084b..36c8636 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", @@ -44,7 +51,10 @@ "laravel": { "providers": [ "Suarez\\UtmParameter\\Providers\\UtmParameterServiceProvider" - ] + ], + "aliases": { + "UtmParameter": "Suarez\\UtmParameter\\Facades\\UtmParameter" + } } }, "minimum-stability": "stable", diff --git a/src/Facades/UtmParameter.php b/src/Facades/UtmParameter.php new file mode 100644 index 0000000..ef7f39c --- /dev/null +++ b/src/Facades/UtmParameter.php @@ -0,0 +1,26 @@ +shouldAcceptUtmParameter($request)) { - app(UtmParameter::class)->boot(session('utm')); + UtmParameter::boot($request); } return $next($request); diff --git a/src/Providers/UtmParameterServiceProvider.php b/src/Providers/UtmParameterServiceProvider.php index 0063d52..37fccbf 100644 --- a/src/Providers/UtmParameterServiceProvider.php +++ b/src/Providers/UtmParameterServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Blade; use Suarez\UtmParameter\UtmParameter; +use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; class UtmParameterServiceProvider extends ServiceProvider @@ -16,6 +17,7 @@ class UtmParameterServiceProvider extends ServiceProvider public function register() { $this->app->singleton(UtmParameter::class, fn () => new UtmParameter()); + $this->mergeConfigFrom(__DIR__.'/../config/utm-parameter.php', 'utm-parameter'); } /** @@ -25,6 +27,8 @@ public function register() */ public function boot() { + $this->publishes([__DIR__.'/../config/utm-parameter.php' => config_path('utm-parameter.php')], 'utm-parameter'); + Blade::if('hasUtm', function (string $key, string|null $value = null) { return has_utm($key, $value); }); @@ -32,5 +36,15 @@ public function boot() Blade::if('hasNotUtm', function (string $key, string|null $value = null) { return has_not_utm($key, $value); }); + + Blade::if('containsUtm', function (string $key, string|null $value = null) { + return contains_utm($key, $value); + }); + + Blade::if('containsNotUtm', function (string $key, string|null $value = null) { + return contains_not_utm($key, $value); + }); + + AliasLoader::getInstance()->alias('UtmParameter', \Suarez\UtmParameter\Facades\UtmParameter::class); } } diff --git a/src/UtmParameter.php b/src/UtmParameter.php index 75abced..7ca3879 100644 --- a/src/UtmParameter.php +++ b/src/UtmParameter.php @@ -2,6 +2,8 @@ namespace Suarez\UtmParameter; +use Illuminate\Http\Request; + class UtmParameter { /** @@ -9,30 +11,58 @@ 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; } /** * Bootstrap UtmParameter. * - * @param array|string|null $parameters + * @param Request $request * * @return UtmParameter */ - public function boot($parameters = null) + public function boot(Request $request) { - if (!$parameters) { - $parameters = self::getParameter(); - session(['utm' => $parameters]); + $this->parameters = $this->useRequestOrSession($request); + return $this; + } + + /** + * Check which Parameters should be used. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function useRequestOrSession(Request $request) + { + $currentRequestParameter = $this->getParameter($request); + $sessionParameter = session($this->sessionKey); + + if (!empty($currentRequestParameter) && empty($sessionParameter)) { + session([$this->sessionKey => $currentRequestParameter]); + return $currentRequestParameter; } - $this->parameters = $parameters; + if (!empty($currentRequestParameter) && !empty($sessionParameter) && config('utm-parameter.override_utm_parameters')) { + $mergedParameters = array_merge($sessionParameter, $currentRequestParameter); + session([$this->sessionKey => $mergedParameters]); + return $mergedParameters; + } - return app(UtmParameter::class, $parameters); + return $sessionParameter; } /** @@ -40,9 +70,9 @@ public function boot($parameters = null) * * @return array */ - public static function all() + public function all() { - return app(UtmParameter::class)->parameters ?? []; + return $this->parameters ?? []; } /** @@ -52,10 +82,10 @@ public static function all() * * @return string|null */ - public static function get($key) + public function get(string $key) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return null; @@ -72,17 +102,17 @@ public static function get($key) * * @return bool */ - public static function has($key, $value = null) + public function has(string $key, $value = null) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return false; } if (array_key_exists($key, $parameters) && $value !== null) { - return self::get($key) === $value; + return $this->get($key) === $value; } return true; @@ -95,16 +125,16 @@ public static function has($key, $value = null) * @param string $value * @return bool */ - public static function contains($key, $value) + public function contains(string $key, string $value) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters) || !is_string($value)) { return false; } - return str_contains(self::get($key), $value); + return str_contains($this->get($key), $value); } /** @@ -112,10 +142,10 @@ public static function contains($key, $value) * * @return bool */ - public static function clear() + public function clear() { - app(UtmParameter::class)->parameters = null; - session()->forget('utm'); + session()->forget($this->sessionKey); + $this->parameters = null; return true; } @@ -124,11 +154,18 @@ public static function clear() * * @return array */ - protected static function getParameter() + protected static function getParameter(Request $request) { - return collect(request()->all()) + $allowedKeys = config('utm-parameter.allowed_utm_parameters', [ + 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content' + ]); + + return collect($request->all()) ->filter(fn ($value, $key) => substr($key, 0, 4) === 'utm_') - ->map(fn ($value) => htmlspecialchars($value, ENT_QUOTES, 'UTF-8')) + ->filter(fn ($value, $key) => in_array($key, $allowedKeys)) + ->mapWithKeys(fn ($value, $key) => [ + htmlspecialchars($key, ENT_QUOTES, 'UTF-8') => htmlspecialchars($value, ENT_QUOTES, 'UTF-8') + ]) ->toArray(); } @@ -138,7 +175,7 @@ protected static function getParameter() * @param string $key * @return string */ - protected static function ensureUtmPrefix(string $key): string + protected function ensureUtmPrefix(string $key): string { return str_starts_with($key, 'utm_') ? $key : 'utm_' . $key; } diff --git a/src/config/utm-parameter.php b/src/config/utm-parameter.php new file mode 100644 index 0000000..0e5fb44 --- /dev/null +++ b/src/config/utm-parameter.php @@ -0,0 +1,50 @@ + 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', + + /* + * Allowed UTM Parameters (default: ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_campaign_id']) + * + * This setting defines the UTM parameters that are allowed within your application. + * + * In this array, you can specify a list of allowed UTM parameter names. Each parameter should be listed as a string. + * Only parameters from this list will be stored and processed in the session. + * and any parameter without the 'utm_' prefix will be ignored regardless of its inclusion in this list. + * + * Example: To only allow the basic UTM parameters (source, medium, and campaign), you could update the array like this: + * + * 'allowed_utm_parameters' => [ + * 'utm_source', + * 'utm_medium', + * 'utm_campaign', + * ], + */ + 'allowed_utm_parameters' => [ + 'utm_source', + 'utm_medium', + 'utm_campaign', + 'utm_term', + 'utm_content', + 'utm_campaign_id' + ], +]; diff --git a/src/helper.php b/src/helper.php index 1c61eb0..2bbf8c7 100644 --- a/src/helper.php +++ b/src/helper.php @@ -1,6 +1,6 @@ sessionKey = Config::get('utm-parameter.session_key'); $parameters = [ 'utm_source' => 'google', @@ -19,8 +25,19 @@ public function setUp(): void 'utm_term' => '{targetid}', ]; - app()->singleton(UtmParameter::class, fn () => new UtmParameter()); - app(UtmParameter::class)->boot($parameters); + $request = Request::create('/test', 'GET', $parameters); + + UtmParameter::boot($request); + } + + public function tearDown() : void + { + session()->forget($this->sessionKey); + + Config::set('utm-parameter.override_utm_parameters', null); + Config::set('utm-parameter.session_key', null); + + parent::tearDown(); } public function test_it_should_be_bound_in_the_app() @@ -29,6 +46,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(); @@ -41,6 +81,7 @@ public function test_it_should_have_a_source_parameter() { $source = UtmParameter::get('source'); $this->assertNotEmpty($source); + $this->assertIsString($source); $this->assertEquals('google', $source); } @@ -48,6 +89,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); } @@ -55,6 +97,7 @@ public function test_it_should_have_a_medium_parameter() { $medium = UtmParameter::get('medium'); $this->assertNotEmpty($medium); + $this->assertIsString($medium); $this->assertEquals('cpc', $medium); } @@ -62,6 +105,7 @@ public function test_it_should_have_a_campaign_parameter() { $campaign = UtmParameter::get('campaign'); $this->assertNotEmpty($campaign); + $this->assertIsString($campaign); $this->assertEquals('{campaignid}', $campaign); } @@ -69,6 +113,7 @@ public function test_it_should_have_a_content_parameter() { $content = UtmParameter::get('content'); $this->assertNotEmpty($content); + $this->assertIsString($content); $this->assertEquals('{adgroupid}', $content); } @@ -76,6 +121,7 @@ public function test_it_should_have_a_term_parameter() { $term = UtmParameter::get('term'); $this->assertNotEmpty($term); + $this->assertIsString($term); $this->assertEquals('{targetid}', $term); } @@ -83,6 +129,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); } @@ -90,6 +137,7 @@ public function test_it_should_determine_if_utm_has_key() { $hasSource = UtmParameter::has('source'); $this->assertIsBool($hasSource); + $this->assertNotEmpty($hasSource); $this->assertTrue($hasSource); } @@ -97,6 +145,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); } @@ -104,6 +153,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); } @@ -111,6 +161,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); } @@ -118,6 +169,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); } @@ -125,6 +177,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); } @@ -132,6 +185,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); } @@ -139,6 +193,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); } @@ -146,6 +201,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); } @@ -153,35 +209,231 @@ 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); } + public function test_it_should_determine_if_a_utm_parameter_contains_a_value() + { + $isGoogle = contains_utm('source', 'goog'); + $this->assertIsBool($isGoogle); + $this->assertTrue($isGoogle); + } + + public function test_it_should_determine_if_a_utm_parameter_not_contains_a_value() + { + $isRandomSource = has_not_utm('source', 'random'); + $this->assertIsBool($isRandomSource); + $this->assertTrue($isRandomSource); + } + 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); } + + public function test_it_should_overwrite_new_utm_parameter() + { + Config::set('utm-parameter.override_utm_parameters', true); + + $source = UtmParameter::get('source'); + $this->assertEquals('google', $source); + + $parameters = [ + 'utm_source' => 'newsletter', + 'utm_medium' => 'email' + ]; + + $request = Request::create('/', 'GET', $parameters); + UtmParameter::boot($request); + + $source = UtmParameter::get('source'); + $this->assertEquals('newsletter', $source); + + $medium = UtmParameter::get('utm_medium'); + $this->assertEquals('email', $medium); + + $campaign = UtmParameter::get('campaign'); + $this->assertEquals('{campaignid}', $campaign); + } + + public function test_it_should_keep_existing_parameters() + { + Config::set('utm-parameter.override_utm_parameters', false); + + $source = UtmParameter::get('source'); + $this->assertEquals('google', $source); + + $parameters = [ + 'id' => '0123456789', + 'sorting' => 'relevance' + ]; + + $request = Request::create('/test', 'GET', $parameters); + UtmParameter::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); + + $medium = UtmParameter::get('utm_medium'); + $this->assertEquals('cpc', $medium); + + $campaign = UtmParameter::get('campaign'); + $this->assertEquals('{campaignid}', $campaign); + } + + public function test_it_should_keep_existing_parameters_while_browsing() + { + $source = UtmParameter::get('source'); + $this->assertEquals('google', $source); + + $parameters = ['id' => '0123456789', 'sorting' => 'relevance']; + $request = Request::create('/new-page', 'GET', $parameters); + UtmParameter::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); + UtmParameter::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); + } + + public function test_it_should_only_use_utm_parameters_in_the_allowed_list() + { + session()->forget($this->sessionKey); + Config::set('utm-parameter.override_utm_parameters', true); + Config::set('utm-parameter.allowed_utm_parameters', ['utm_source', 'utm_medium']); + + $parameters = [ + 'utm_source'=> 'newsletter', + 'utm_medium' => 'email', + 'utm_campaign' => 'not-allowed' + ]; + + $request = Request::create('/test', 'GET', $parameters); + UtmParameter::boot($request); + + $source = UtmParameter::get('source'); + $this->assertEquals('newsletter', $source); + $this->assertIsNotInt($source); + $this->assertNotEmpty($source); + + $medium = UtmParameter::get('medium'); + $this->assertEquals('email', $medium); + $this->assertIsNotInt($medium); + $this->assertNotEmpty($medium); + + $campaign = UtmParameter::get('campaign'); + $this->assertNull($campaign); + $this->assertEmpty($campaign); + + $term = UtmParameter::get('term'); + $this->assertNull($term); + $this->assertEmpty($term); + + $utm_campaign_id = UtmParameter::get('utm_campaign_id'); + $this->assertNull($utm_campaign_id); + $this->assertEmpty($utm_campaign_id); + } + + public function test_it_should_sanitize_utm_parameter() + { + Config::set('utm-parameter.override_utm_parameters', true); + + $parameters = [ + 'utm_source'=> 'google', + 'utm_medium' => 'cpc', + 'utm_campaign' => '