PHPackages                             frnkly/laravel-keen - 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. [API Development](/categories/api)
4. /
5. frnkly/laravel-keen

ActiveLibrary[API Development](/categories/api)

frnkly/laravel-keen
===================

An efficient Keen.io integration for Laravel

v0.5.1(8y ago)1198MITPHPPHP &gt;=7.0

Since Oct 10Pushed 8y ago1 watchersCompare

[ Source](https://github.com/frnkly/laravel-keen)[ Packagist](https://packagist.org/packages/frnkly/laravel-keen)[ RSS](/packages/frnkly-laravel-keen/feed)WikiDiscussions dev Synced 2mo ago

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

[Laravel](https://laravel.com) + [Keen.io](https://keen.io)
===========================================================

[](#laravel--keenio)

Defer sending event data to [Keen.io](https://keen.io) until after a request has been fulfilled–automatically and transparently.

[![Latest Stable Version](https://camo.githubusercontent.com/2368dc2962cb425e221a520a6cb7c925a73abbcccf60b66636b621d340f1580c/68747470733a2f2f706f7365722e707567782e6f72672f66726e6b6c792f6c61726176656c2d6b65656e2f76657273696f6e)](https://packagist.org/packages/frnkly/laravel-keen)[![Build Status](https://camo.githubusercontent.com/0c4f297c0560212e41a0c78e705da6d1309ec79fe7c58bacabaced7f33d1367a/68747470733a2f2f7472617669732d63692e6f72672f66726e6b6c792f6c61726176656c2d6b65656e2e706e67)](https://travis-ci.org/frnkly/laravel-keen)[![Total Downloads](https://camo.githubusercontent.com/fda803d52e35199d869f1548e04e4425d652c74b3a8ac865eddc439b87ab1a70/68747470733a2f2f706f7365722e707567782e6f72672f66726e6b6c792f6c61726176656c2d6b65656e2f646f776e6c6f616473)](https://packagist.org/packages/frnkly/laravel-keen)[![License](https://camo.githubusercontent.com/332131c5a9917f1d46a4bfa4c8689de5e94da5e895743955d2e8e15e38107a12/68747470733a2f2f706f7365722e707567782e6f72672f66726e6b6c792f6c61726176656c2d6b65656e2f6c6963656e7365)](https://packagist.org/packages/frnkly/laravel-keen)

This package provides a light wrapper around the [Keen PHP client](https://github.com/keenlabs/KeenClient-PHP), as well as a middleware configured to defer sending event data to Keen until after each request has been fulfilled. That means that as the number of tracked events increase, the impact on request time remains virtually non-existent. For convenience, the middleware can also track every request automatically.

- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
- [Getting started](#getting-started)
- [Automatic tracking of every request](#automatic-tracking-of-every-request)
    - [Data enrichment](#data-enrichment)
    - [Using your own middleware](#using-your-own-middleware)
    - [Skipping requests](#skipping-requests)
- [Type-hinting](#type-hinting)

Features
========

[](#features)

- Deferred event tracking 🙌
- Optional automatic tracking of every request
- Support data enrichment out of the box
    - [IP to Geo](https://keen.io/docs/streams/ip-to-geo-enrichment)
    - [User Agent](https://keen.io/docs/streams/user-agent-enrichment)
    - [Referrer](https://keen.io/docs/streams/referrer-enrichment)
- Extensible middleware
- Option to ignore specific HTTP status codes (e.g. `100`, `302`, etc.)
    - Or any request based on custom logic
- More to come!

Installation
============

[](#installation)

Install the Laravel + Keen package using [Composer](https://getcomposer.org):

```
$ composer require frnkly/laravel-keen

```

This will also install the Keen PHP client as a dependency.

Configuration
=============

[](#configuration)

Your Keen project ID and keys should be defined in the `config/services.php`configuration file, but will usually come from the [environment configuration](https://laravel.com/docs/configuration):

```
return [
    // Other 3rd-party Services
    // ...

    // Add your Keen information here
    'keen' => [
        'id'     => env('KEEN_PROJECT_ID'),
        'master' => env('KEEN_MASTER_KEY'),
        'write'  => env('KEEN_WRITE_KEY'),
    ],
];
```

And in your `.env` file, you might add something like:

```
KEEN_PROJECT_ID=your-project-id
KEEN_MASTER_KEY=your-master-key
KEEN_WRITE_KEY=your-write-key

```

Getting started
===============

[](#getting-started)

Register the middleware globally in `app/Http/Kernel.php`:

```
protected $middleware = [
    // Other Middleware
    // ...

    \Frnkly\LaravelKeen\Middleware::class,
];
```

Or within a middleware group:

```
protected $middlewareGroups = [
    'web' => [
        // Other "Web" Middleware
        // ...

        \Frnkly\LaravelKeen\Middleware::class,
    ],

    'api' => [
        // Other "API" Middleware
        // ...

        \Frnkly\LaravelKeen\Middleware::class,
    ],
];
```

Thanks to [package discovery](https://laravel.com/docs/packages#package-discovery)in Laravel 5.5, the service provider should already be available. It can also be manually registered through the `config/app.php` configuration file:

```
'providers' => [
    // Other Service Providers
    // ...

    Frnkly\LaravelKeen\ServiceProvider::class,
]
```

Automatic tracking of every request
===================================

[](#automatic-tracking-of-every-request)

Automatic tracking is enabled by default. You may disable this behaviour through the configuration file:

```
'keen' => [
    // Other Keen options
    // ...

    'track_requests' => false,
],
```

Data enrichment
---------------

[](#data-enrichment)

To automatically add [data enrichment](https://keen.io/docs/api/?php#data-enrichment)to each request, use the following configuration options:

```
'keen' => [
    // Other Keen settings
    // ...

    'addons' => [
        'ip_to_geo'       => true,  // IP to Geo parser
        'ua_parser'       => true,  // User Agent parser
        'referrer_parser' => true,  // Referrer parser
    ],
],
```

Each data enrichment object will appear in your Keen stream under the same name as above (i.e. `ip_to_geo` and `ua_parser`).

Using your own middleware
-------------------------

[](#using-your-own-middleware)

The included middleware makes it simple to extend and gain more granular control over the data that gets sent to Keen. You can create your own middleware using `Artisan`, and then have it extend `\Frnkly\LaravelKeen\Middleware`:

```
$ php artisan make:middleware TracksRequests

```

Inside the new middleware, override the protected method `buildRequestEventData`:

```
