PHPackages                             victord11/laravel-utm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. victord11/laravel-utm

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

victord11/laravel-utm
=====================

Keeps track of the UTM parameters

1.0(1y ago)01.0k[1 PRs](https://github.com/VictoRD11/laravel-utm/pulls)MITPHPPHP ^8.1

Since Oct 29Pushed 5mo agoCompare

[ Source](https://github.com/VictoRD11/laravel-utm)[ Packagist](https://packagist.org/packages/victord11/laravel-utm)[ Docs](https://github.com/victord11/laravel-utm)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/victord11-laravel-utm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (4)Used By (0)

\####This package is an updated fork of [adzbuck/laravel-utm](https://github.com/adzbuck/laravel-utm)

Keeps track of UTMs and/or other parameters
===========================================

[](#keeps-track-of-utms-andor-other-parameters)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3dfdffe68e52101747a7b9a6e3be8a94758f88b7bb52df16abf0fb8e4ca45d5d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f766963746f726431312f6c61726176656c2d75746d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/victord11/laravel-utm)[![GitHub Tests Action Status](https://camo.githubusercontent.com/051614884e5c1fe30c7b74056f4fe5c61d75308ac032373256b03a6e1b54bff2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f766963746f726431312f6c61726176656c2d75746d2f72756e2d74657374732e796d6c3f6c6162656c3d5465737473)](https://github.com/victord11/laravel-utm/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/a4078323af8d9d62be7edd44fadfc3d1d147e2a4b12af4cb8daf63baca0376f3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f766963746f726431312f6c61726176656c2d75746d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/victord11/laravel-utm)

This package allows you to easily track first and last touch query parameters and headers via session. You can then easily access these parameters so you can add them to a form submission or a link to another domain you track.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require victord11/laravel-utm
```

The package works via a middleware that needs to be added to the `web` stack in your `kernel.php` file. Make sure to register this middleware after the `StartSession` middleware.

```
// app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        // ...
        \Illuminate\Session\Middleware\StartSession::class,
        // ...
        \VictoRD11\LaravelUTM\Middleware\ParameterTrackerMiddleware::class,
    ],
];
```

To configure the tracked parameters or how they're mapped on the URL parameters, you can publish the config file using:

```
php artisan vendor:publish --provider="VictoRD11\LaravelUTM\ServiceProvider"
```

This is the contents of the published config file:

```
use VictoRD11\LaravelUTM\Sources;

return [

    /**
     * How the data will be stored
     */
    'store' => StoreType::Cookie,

    /*
     * These are the analytics parameters that will be tracked when a user first visits
     * the application. The configuration consists of the parameter's key and the
     * source to extract this key from.
     *
     * Available sources can be found in the `\VictoRD11\LaravelUTM\Sources` namespace.
     */
    'tracked_parameters' => [
        [
            'key' => 'utm_source',
            'source' => Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_medium',
            'source' => Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_campaign',
            'source' => Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_term',
            'source' => Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_content',
            'source' => Sources\RequestParameter::class,
        ],
        [
            'key' => 'referer',
            'source' => Sources\CrossOriginRequestHeader::class,
        ],
    ],

    /**
     * The name of the cooke that will be set
     */
    'cookie_name' => 'utm_params',

    /**
     * Cookie secure flag
     */
    'cookie_secure' => null,

    /**
     * Cookie http flag
     */
    'cookie_http_only' => true,

    /**
     * We'll put the first touch tracked parameters in the session using this key.
     */
    'first_touch_store_key' => 'laravel_utm_parameters_first',

    /**
     * We'll put the last touch tracked parameters in the session using this key.
     */
    'last_touch_store_key' => 'laravel_utm_parameters_last',

    /**
     * If we should keep track of the first touch utm params
     */
    'first_touch' => true,

    /**
     * If we should keep track of the last touch utm params
     */
    'last_touch' => true,

    /*
     * When formatting an URL to add the tracked parameters we'll use the following
     * mapping to put tracked parameters in URL parameters.
     *
     * This is useful when using an analytics solution that ignores the utm_* parameters.
     */
    'parameter_url_mapping' => [
        'utm_source' => 'utm_source',
        'utm_medium' => 'utm_medium',
        'utm_campaign' => 'utm_campaign',
        'utm_term' => 'utm_term',
        'utm_content' => 'utm_content',
        'referer' => 'referer',
    ],
];
```

Usage
-----

[](#usage)

There are three methods of tracking:

 getFirstTouch This will get the parameters from the users first visit. getLastTouch This will get the parameters from the users last visit. getCurrent This will get the parameters from the current request.The easiest way to retrieve the tracked parameters is by resolving the `ParameterTracker` class:

```
use VictoRD11\LaravelUTM\ParameterTracker;

// returns an array of the first touch tracked parameters
$parameterTracker = app(ParameterTracker::class)

$parameterTracker->getFirstTouch();
$parameterTracker->getLastTouch();
$parameterTracker->getCurrent();
```

You can also decorate an existing URL with the tracked parameters. This is useful to forward analytics to another domain you're running analytics on.

The example uses three requests:

```
First request:
https://mywebshop.com/?utm_source=facebook&utm_campaign=blogpost

2nd Request:
https://mywebshop.com/?utm_source=google&utm_campaign=blogpost

Current Request:
https://mywebshop.com/

```

##### decorateUrl/@trackedUrl

[](#decorateurltrackedurl)

This does not use the session tracking, it simply uses the params provided.

```
