Skip to content

Commit

Permalink
- added also contains
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-suarez committed Jun 20, 2024
1 parent def772f commit c0003fd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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. 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).
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.

```php
# Laravel 11
Expand All @@ -49,6 +49,8 @@ return Application::configure(basePath: dirname(__DIR__))
...
```

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).

## Configuration

The configuration file `config/utm-parameter.php` allows you to control the behavior of the UTM parameters handling.
Expand Down Expand Up @@ -137,6 +139,34 @@ Simply use:
}
```

### contains_utm()

Sometimes you want to show or do something, if user might have some or specific utm-parameters.

Simply use:
- `contains_utm('source|medium|campaign|term|content', 'value')`
- `contains_not_utm('source|medium|campaign|term|content', 'value')`

```blade
@containsUtm('source', 'weekly')
<div>Some Weekly related stuff</div>
@endhasUtm
@containsNotUtm('sales')
<p>Some not Sales stuff</p>
@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.
Expand Down Expand Up @@ -187,7 +217,6 @@ use App\Http\Middleware\CustomMiddleware;
})
```


## Resources
Explore additional use cases and resources on the [wiki pages](https://github.com/toni-suarez/laravel-utm-parameter/wiki)

Expand Down
9 changes: 9 additions & 0 deletions src/Providers/UtmParameterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ 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);
});

}
}
15 changes: 15 additions & 0 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ function has_not_utm($key, $value = null)
return !UtmParameter::has($key, $value);
}
}


if (!function_exists('contains_utm')) {
function contains_utm($key, $value)
{
return UtmParameter::contains($key, $value);
}
}

if (!function_exists('contains_not_utm')) {
function contains_not_utm($key, $value)
{
return !UtmParameter::contains($key, $value);
}
}
14 changes: 14 additions & 0 deletions tests/UtmParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ public function test_it_should_determine_if_an_utm_contains_a_non_string_value()
$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');
Expand Down

0 comments on commit c0003fd

Please sign in to comment.