Get expert guidance in a few days with a Laravel code review

Laravel Posthog

qodenl/laravel-posthog image

Laravel Posthog stats

Downloads
5.4K
Stars
5
Open Issues
1
Forks
2

View on GitHub →

Laravel implementation for Posthog

Laravel Posthog implementation

This package provides a simple integration of Posthog in Laravel applications.

The package covers both Identify as Capture (events) requests which can be triggered manual or automatically using an Event Listener.

You can also easily integrate Feature Flags within your application.

This package uses the PostHog / posthog-php package. For more information about Posthog, check their documentation.

Installation

You can install the package via composer:

composer require qodenl/laravel-posthog

You can publish the config file with:

php artisan vendor:publish --provider="QodeNL\LaravelPosthog\PosthogServiceProvider"

After publishing the content, set your API key and Host in your .env file:

POSTHOG_KEY=
POSTHOG_HOST=https://posthog.com
POSTHOG_ENABLED=true

Make sure you copy the correct host from Posthog.

Posthog is enabled by default, but you can disable it with the POSTHOG_ENABLED env variable.

Make sure to disable Posthog for local/testing environments.

Usage

Manual events

use QodeNL\LaravelPosthog\Facades\Posthog;
 
Posthog::capture('event name', ['property' => 'value']);

Automatic events

You can add the PosthogListener::class listener to your EventServiceProvider. The package will create an capture automatically when the event happens.

By default, all fillable attributes from a model (available in the event) will be sent to Posthog as properties.

You can specify which attributes you want to send to Posthog by adding a PosthogAttributes property to your Model.

public $posthogAttributes = [
'first_name',
'last_name',
];

Attributes in the hidden property will always be ignored.

Identify

Events will be sent to Posthog with a unique ID for anonymous users. When the user is recognized (usually on log in), you should trigger the identify method to link the unique ID to the user.

You can pass additional information about the user to be stored in his profile.

use QodeNL\LaravelPosthog\Facades\Posthog;
 
Posthog::identify('email@user.com', ['first_name' => 'John', 'last_name' => 'Doe']);

Alias

If you want to assign a session ID to a user (for example a front-end session ID) you can use the alias method.

The Session ID argument will be assigned to the auto-generated ID of the user.

Posthog::alias('Session ID here');

Feature flags

Get all feature flags

use QodeNL\LaravelPosthog\Facades\Posthog;
 
Posthog::getAllFlags();

Get all feature flags with boolean if enabled for user or not.

Check if feature is enabled

use QodeNL\LaravelPosthog\Facades\Posthog;
 
Posthog::isFeatureEnabled('myFeatureFlagKey');

Check if feature is enabled for user. Returns boolean.

Get feature flag

use QodeNL\LaravelPosthog\Facades\Posthog;
 
Posthog::getFeatureFlag('myFeatureFlagKey');

Get feature flag. Returns false if feature is disabled. Returns true (or payload if set).

Optional attributes

You can pass groups, personProperties and groupProperties to the isFeatureEnabled and getFeatureFlag functions.

Please check the Posthog PHP documentation for more information.

In the Posthog config you can configure if events should be sent to Posthog and if you want to evaluate events locally.

Queue / jobs

The capture, identify and alias actions are executed by jobs. Be sure you've enabled and configured queues for your applications.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Qodenl Laravel Posthog Related Articles

Manage Events, Feature Flags, and More with PostHog for Laravel image

Manage Events, Feature Flags, and More with PostHog for Laravel

Read article
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel