Giter Site home page Giter Site logo

laravel-ab's Introduction

Latest Version Build Status Quality Score Code Coverage

This package helps you to find out which content works on your site and which doesn't.

It allows you to create experiments and goals. The visitor will receive randomly the next experiment and you can customize your site to that experiment. The view and the goal conversion will be tracked and you can view the results in a report.

Installation

This package can be used in Laravel 5.6 or higher.

You can install the package via composer:

composer require ben182/laravel-ab

Config

After installation publish the config file:

php artisan vendor:publish --provider="Ben182\AbTesting\AbTestingServiceProvider"

You can define your experiments and goals in there.

Finally, run the newly added migration

php artisan migrate

Two new migrations should be added.

Usage

Experiments

@if (AbTesting::isExperiment('logo-big'))

    <div class="logo-big"></div>

@elseif (AbTesting::isExperiment('logo-grayscale'))

    <div class="logo-greyscale"></div>

@elseif (AbTesting::isExperiment('brand-name'))

    <h1>Brand name</h1>

@endif

That's the most basic usage of the package. You don't have to initialize anything. The package handles everything for you if you call isExperiment

Alternatively you can use a custom blade if statement:

@ab('logo-big')

    <div class="logo-big"></div>

@elseab('logo-grayscale')

    <div class="logo-greyscale"></div>

@elseab('brand-name')

    <h1>Brand name</h1>

@endab

This will work exactly the same way.

If you don't want to make any continual rendering you can call

AbTesting::pageView()

directly and trigger a new page view with a random experiment. This function will also be called from isExperiment.

Under the hood a new session item will keep track of the current experiment. A session will only get one experiment and only trigger one page view.

You can grab the current experiment with:

// get the underlying model
AbTesting::getExperiment()

// get the experiment name
AbTesting::getExperiment()->name

// get the visitor count
AbTesting::getExperiment()->visitors

Alternatively there is a request helper for you:

public function index(Request $request) {
    // the same as 'AbTesting::getExperiment()'
    $request->abExperiment()
}

Goals

To complete a goal simply call:

AbTesting::completeGoal('signup')

The function will increment the conversion of the goal assigned to the active experiment. If there isn't an active experiment running for the session one will be created. You can only trigger a goal conversion once per session. This will be prevented with another session item. The function returns the underlying goal model.

To get all completed goals for the current session:

AbTesting::getCompletedGoals()

Bots and crawlers

The package can try to ignore bots and crawlers from registering pageviews. Just enable the ignore_crawlers option in the config.

Report

To get a report of the page views, completed goals and conversion call the report command:

php artisan ab:report

This prints something like this:

+---------------+----------+-------------+
| Experiment    | Visitors | Goal signup |
+---------------+----------+-------------+
| big-logo      | 2        | 1 (50%)     |
| small-buttons | 1        | 0 (0%)      |
+---------------+----------+-------------+

Reset

To reset all your visitors and goal completions call the reset command:

php artisan ab:reset

Events

In addition you can hook into two events:

  • ExperimentNewVisitor gets triggered once an experiment gets assigned to a new visitor. You can grab the experiment as a property of the event.
  • GoalCompleted gets triggered once a goal is completed. You can grab the goal as a property of the event.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

laravel-ab's People

Contributors

adevade avatar ben182 avatar kalizi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-ab's Issues

Problem with lock

Database: Percona 8.0.15

Exception:
ErrorException: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: update ab_experiments set visitors = visitors + 1, updated_at = 2019-04-09 15:11:48 where id = 1) (View: /var/www/resources/views/user/busca/busca.blade.php)

Support for Laravel 7.x

Looking forward to using your fantastic work in my application.

I'm running into an issue when trying with the latest Laravel 7.x

Problem 1
- Installation request for ben182/laravel-ab ^1.2 -> satisfiable by ben182/laravel-ab[1.2.0].
- Conclusion: remove laravel/framework v7.1.3
- Conclusion: don't install laravel/framework v7.1.3
- ben182/laravel-ab 1.2.0 requires illuminate/support 5.6.* | 5.7.* | 5.8.* | ^6.0 -> satisfiable by illuminate/support[5.6.x-dev, 5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, 6.x-dev, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.36, v5.8.4, v5.8.8, v5.8.9, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4, v6.1.0, v6.10.0, v6.11.0, v6.12.0, v6.13.0, v6.13.1, v6.14.0, v6.15.0, v6.15.1, v6.16.0, v6.17.0, v6.17.1, v6.18.0, v6.18.1, v6.2.0, v6.3.0, v6.4.1, v6.5.0, v6.5.1, v6.5.2, v6.6.0, v6.6.1, v6.6.2, v6.7.0, v6.8.0].
- don't install illuminate/support 5.6.x-dev|don't install laravel/framework v7.1.3
- Installation request for laravel/framework (locked at v7.1.3, required as ^7.0) -> satisfiable by laravel/framework[v7.1.3].

Installation failed, reverting ./composer.json to its original content.

Multiple A/B Testing at once?

I think there are cases for many when we may want to run multiple experiment at once. For an instance, I may want to pick a font from an array and at the same time pick a different header logo. We can do something like this in ab-testing.php:


$experimentLists= '{

    "font": ["system-font","inter-font"],
    "home-welcome": ["home-welcomeMainTop","home-welcomeTopStories"]
}'; 
   return [ 'experiments' => json_decode($experimentLists)......... ] 

This way, it always pick one item for each array of an experimentLists object.

This would enhance this tool because there are no any other A/B testing tools with this feature.

Laravel 8 support

I'm getting the following error while trying to install it for Laravel 8.* project:

composer require ben182/laravel-ab
Using version ^2.0 for ben182/laravel-ab
./composer.json has been updated
Running composer update ben182/laravel-ab
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - ben182/laravel-ab[2.0.0, ..., 2.0.2] require illuminate/support ^6.0 | ^7.0 -> found illuminate/support[v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires ben182/laravel-ab ^2.0 -> satisfiable by ben182/laravel-ab[2.0.0, 2.0.1, 2.0.2].

Trying to get property 'name' of non-object when crawled

return $this->getExperiment()->name === $name;

When crawled by Google bot there is no session, so getExperiment() won't return an object with a name property. This will break the page and google will see a 500 while normal users have a 200.

Maybe:

public function isExperiment(string $name)
    {
        if ((new CrawlerDetect)->isCrawler()) {
            return false;
        }

        $this->pageView();

        return $this->getExperiment()->name === $name;
    }

N+1 queries while creating the experiments collection.

Thanks for the great package, Ben! I noticed N+1 queries:

Example: I created two experiments and two goals. If a user is visiting a page with an included a/b test the package fetches the experiment one, then separately the two goals. After that it fetches the second experiment and the two goals separately as well. Then, it is updating the visitors counter. So in the end 7 queries are performed.

The query problem is due to the nested foreach in the start() method in the ABTesting class. The goals model should be eager loaded.

It would be great if you have a quick solution for the problem. Otherwise I will create a PR in the next weeks.

Can't complete a goal

Hi,

I tried a couple of ways to complete a goal. I tried to put {{AbTesting::completeGoal('order-placed')}} inside the blade template that renders when a order is placed. I added the goal in the config file and placed an a/b experiment on my homepage. The ab_goals table does not show any data. The other thing that I tried is to call $test = (new AbTesting())->completeGoal('order-placed'); or AbTesting::completeGoal('order-placed'); inside the controller. Neither produce any results.

not working with Laravel 5.8?

New (empty) laravel project, tried to use your example code, and got error:

Call to a member function incrementVisitor() on null

No support for Laravel 5.8

I see in your composer.json that you specify that this package requires illuminate/support 5.5.* || 5.6.* || 5.7.* .

Is there a reason not to add 5.8.* as well?

Some issues you may encounter if you upgrade:

  1. You probably want to require orchestra/testbench 3.8.* as well
  2. php test will most likely fail unless you change the setUp method signature in TestCase.php to public function setUp(): void

No support for PHP 8

Trying to install for laravel 8 and I get the following output

▶ composer require ben182/laravel-ab

                                                                                                                            
  [InvalidArgumentException]                                                                                                
  Package ben182/laravel-ab has a PHP requirement incompatible with your PHP version, PHP extensions and Composer version:  
    - ben182/laravel-ab 1.2.0 requires php ^7.0 which does not match your installed version 8.0.8.    

[Feature Request] Disable pageView for bots and crawlers

Hey Ben!
First of all, thank you for your package! It's slim and seems to work great! Just started testing it on one of my sites.

However, I quickly found that a lot of bots and crawlers are skewing my stats.
I think it would be nice to have an option in the config to maybe (try to) ignore these page views.

Do you agree? I feel that I need to write it for myself, so might as well push it upstream to your repo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.