1. Go to this page and download the library: Download ziming/laravel-statsig library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ziming / laravel-statsig example snippets
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Ziming\LaravelStatsig\Commands\StatsigSendCommand;
use Ziming\LaravelStatsig\Commands\StatsigSyncCommand;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
// https://docs.statsig.com/server/phpSDK#sync
$schedule->command(StatsigSyncCommand::class)->everyMinute();
// https://docs.statsig.com/server/phpSDK#send
$schedule->command(StatsigSendCommand::class)->everyMinute();
}
use Statsig\Adapters\LocalFileDataAdapter;
use Statsig\Adapters\LocalFileLoggingAdapter;
return [
'secret' => env('STATSIG_SECRET_KEY'),
'data_adapter' => LocalFileDataAdapter::class,
// arguments to the Data Adapter class constructor
'data_adapter_arguments' => [
// '/tmp/statsig/', // empty array for the default directory for the default Data Adapter
],
'logging_adapter' => LocalFileLoggingAdapter::class,
// arguments to the Logging Adapter class constructor
'logging_adapter_arguments' => [
// '/tmp/statsig.logs', // empty array for the default file path for the default Logging Adapter
],
];
use Illuminate\Support\Facades\App;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Auth;
use Statsig\StatsigUser;
use Ziming\LaravelStatsig\Facades\LaravelStatsig;
use Ziming\LaravelStatsig\LaravelStatsigEvent;
use Ziming\LaravelStatsig\LaravelStatsigUserConfiguration;
$laravelStatsig = new Ziming\LaravelStatsig();
$user = Auth::user();
$laravelStatsig->checkGate($user, 'gate_name');
// The Facade Version is fine too
LaravelStatsig::checkGate($user, 'gate_name');
// You can set add this to 1 of your ServiceProviders boot() method to
// override the default laravel user to Statsig user conversion code too if you want
LaravelStatsigUserConfiguration::setConversionCallable(function (User $laravelUser): StatsigUser {
$statsigUser = StatsigUser::withUserID((string) $laravelUser->getAuthIdentifier());
$statsigUser->setEmail($laravelUser->getEmailForVerification());
$statsigUser->setIP(request()->ip());
$statsigUser->setLocale(App::currentLocale());
$statsigUser->setUserAgent(request()->userAgent());
// $statsigUser->setCountry('US');
return $statsigUser;
});
// Lastly you can also use LaravelStatsigEvent instead of StatsigEvent
// as it accepts a laravel user object
// See Useful References at the end of the read me for some best practices to follow for events naming conventions
$statsigEvent = new LaravelStatsigEvent('event_name');
// Giving it a value is optional
$statsigEvent->setValue('string-or-float-or-int-or-whatever-primitive-type');
// Extra event metadata is optional too. See the official Statsig docs on how many you can send
$statsigEvent->setMetadata([
'key' => 'value'
]);
// You can also use this convenience method
LaravelStatsig::logEventWithAuthUser($statsigEvent);
// or this
$statsigEvent->setUser(Auth::user());
$laravelStatsig->logEvent($statsigEvent);