ubeeqo / laravel-sift by aspitzer

A Laravel 5 package for Sift Science
4,097
0
2
Package Data
Maintainer Username: aspitzer
Maintainer Contact: dev@ubeeqo.com (ubeeqo)
Package Create Date: 2017-07-14
Package Last Update: 2017-07-14
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:18:13
Package Statistics
Total Downloads: 4,097
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel Sift

A Laravel 5 package for Sift Science.

Installation

Install the package using Composer

composer require ubeeqo/laravel-sift

Next, add the following to the providers array in config/app.php

ubeeqo\LaravelSift\SiftServiceProvider::class,

Register the facade by adding the following line to the aliases array in config/app.php

'SiftScience' => ubeeqo\LaravelSift\Facades\SiftScience::class,

Config

You can publish the configuration file using the following artisan command

php artisan vendor:publish --provider="ubeeqo\LaravelSift\SiftServiceProvider"

The default config file will check for a SIFT_API_KEY and SIFT_JAVASCRIPT_KEY in your .env file.

Usage

JavaScript Snippet

To add the JavaScript snippet to your pages, include the following line immediately after the opening body tag in your master blade template:

@include('sift::snippet')

This will track user interaction with your site using a session ID as well as the user's email when authenticated. For more information on the JavaScript snippet itself, consult the Sift Science documentation.

Customizing the Sift User ID

By default the package uses the getAuthIdentifier() method to get an identifier (the user ID in most cases) for a user when tracking auth events. This value can be customized by adding a getSiftId() method to your user model that will return the value you want to track. This is useful if you don't want to expose your user IDs (the value is visible in the JavaScript snippet) or have another value you'd like to use. Keep in mind that using email addresses (or any other value that may change) may not be a good idea because you will lose your reference to the user in Sift if the value changes.

It is recommended that you use SiftScience::getUserId($user) to get a user's Sift ID when reporting your own events, as this is the method used internally. The $user arg is optional and will default to the currently authenticated user. It currently uses the value from the previously mentioned getSiftId() method and will fall back to getAuthIdentifier().

If you are customizing your identifier be aware that if you change it in the future, another user will be created in Sift and their score may be impacted. Also be sure to check out the allowed characters).

Tracking Events

By default the package tracks successful and failed logins as well as logouts, but you'll probably want to track other actions like transactions.

To track events, you'll want to interact with the SiftClient class, which can be accessed as shown below:

// Import the facade
use SiftScience;

// Returns the SiftClient class
SiftScience::client();

// Track an event
SiftScience::client()->track('$transaction', [
    '$user_id'          => SiftScience::getUserId(),
    '$amount'           => 1000000,
    '$currency_code'    => 'USD',
    '$user_email'       => $user->email,
    '$transaction_type' => '$sale',
    '$transaction_status' => '$success',
    '$session_id'       => SiftScience::getSessionId(),
]);

In the example above you'll notice SiftScience::getSessionId(). This gets an identifier stored in the current session using the key 'sift_session_id'. You can also pass a session store as an argument for situations (like queues) where there is not a current session.

For more on how to use the SiftClient class, consult the sift-php documentation and the events API reference.

Queues

You may want to use a queue to track certain events to avoid slowing down your application. The built-in support for auth event tracking uses the queue, but as of now you will have to implement queueing yourself. Suggestions are welcome for an elegant way of adding queue support directly to this package.