| 1 | package Dancer2::Plugin::Sixpack; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Dancer2::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use WWW::Sixpack; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | my $conf; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Dancer2::Plugin::Sixpack - Dancer2's plugin for WWW::Sixpack | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This plugin gives you the ability to do A/B testing within Dancer2 easily, | ||||||||||||||||||||||||||||
| 21 | using L<http://sixpack.seatgeek.com/>. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | It handles the client_id transparantly through Dancer2's Session plugin. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use Dancer2; | ||||||||||||||||||||||||||||
| 28 | use Dancer2::Plugin::Sixpack; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | get '/route' => sub { | ||||||||||||||||||||||||||||
| 31 | my $variant = experiment 'decimal_dot_comma', [ 'comma', 'dot' ]; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $price =~ s/\.(?=[0-9]{2})$/,/ | ||||||||||||||||||||||||||||
| 34 | if( $variant eq 'comma' ); | ||||||||||||||||||||||||||||
| 35 | # ... | ||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | get '/some_click' => sub { | ||||||||||||||||||||||||||||
| 39 | convert 'decimal_dot_comma', 'click'; | ||||||||||||||||||||||||||||
| 40 | redirect $somewhere; | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | get '/confirmation' => sub { | ||||||||||||||||||||||||||||
| 44 | convert 'decimal_dot_comma'; | ||||||||||||||||||||||||||||
| 45 | # ... | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | There are no mandatory settings. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | plugins: | ||||||||||||||||||||||||||||
| 54 | Sixpack: | ||||||||||||||||||||||||||||
| 55 | host: http://localhost:5000 | ||||||||||||||||||||||||||||
| 56 | experiments: | ||||||||||||||||||||||||||||
| 57 | decimal_dot_comma: | ||||||||||||||||||||||||||||
| 58 | - comma | ||||||||||||||||||||||||||||
| 59 | - dot | ||||||||||||||||||||||||||||
| 60 | beer: | ||||||||||||||||||||||||||||
| 61 | - duvel | ||||||||||||||||||||||||||||
| 62 | - budweiser | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The experiments can be generated on the fly without defining them. See below | ||||||||||||||||||||||||||||
| 65 | for more information. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 KEYWORDS | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 experiment | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Fetch the alternative used for the experiment name passed in. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | The experiment and its' alternatives may be defined in the configuration. If | ||||||||||||||||||||||||||||
| 74 | they're not defined, the experiment will be created (if you provided the | ||||||||||||||||||||||||||||
| 75 | alternatives arrayref). | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Examples: | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # experiment defined in config: | ||||||||||||||||||||||||||||
| 80 | my $variant = exeriment 'known-experiment'; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # experiment not defined | ||||||||||||||||||||||||||||
| 83 | my $variant = experiment 'on-the-fly', [ 'alt-1', 'alt-2' ]; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The client_id will be fetched from session, or generated if needed. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | The client's IP address and user agent string are automatically | ||||||||||||||||||||||||||||
| 88 | added to the request for bot detection. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Alternatives can be forced by params like "sixpack-force-$experiment=$alt" | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Returns the alternative name chosen. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | register experiment => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 97 | 2 | my ($dsl, $name, $alternatives) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | my $sixpack = $dsl->get_sixpack(); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # stored alternatives? | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if( !$alternatives && defined $conf->{experiments}{$name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $alternatives and defined $conf->{"experiments"}{$name}
MC/DC50%1/2
not $alternatives and defined $conf->{"experiments"}{$name}
not $alternatives
defined $conf->{"experiments"}{$name}
Truth table66%2/3
not(A) and B
A= not $alternativesB= defined $conf->{"experiments"}{$name}
| |||||||||||||||||||||||||||||
| 103 | 1 | $alternatives = $conf->{experiments}{$name}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # user info | ||||||||||||||||||||||||||||
| 107 | 2 | my %options = (); | |||||||||||||||||||||||||||
| 108 | 2 | ▶ | $options{ip_address} = $dsl->app->request->address | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | if $dsl->app->request->address; | ||||||||||||||||||||||||||||
| 110 | 2 | ▶ | $options{user_agent} = $dsl->app->request->agent | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | if $dsl->app->request->agent; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # force if requested | ||||||||||||||||||||||||||||
| 114 | 2 | ▶ | $options{force} = $dsl->app->request->param("sixpack-force-$name") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | if $dsl->app->request->param("sixpack-force-$name"); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | my $alt = $sixpack | |||||||||||||||||||||||||||
| 118 | ->participate( $name, $alternatives, \%options ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | my $experiments = $dsl->app->session->read('sixpack_experiments') || { }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$dsl->app->session->read('sixpack_experiments') || {}
MC/DC100%1/1
$dsl->app->session->read('sixpack_experiments') || {}
$dsl->app->session->read('sixpack_experiments')
Truth table100%2/2
A
A= $dsl->app->session->read('sixpack_experiments')
| |||||||||||||||||||||||||||||
| 121 | 2 | $experiments->{$name} = $alt->{alternative}{name}; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | $dsl->app->session->write('sixpack_id', $alt->{client_id}); | |||||||||||||||||||||||||||
| 124 | 2 | $dsl->app->session->write('sixpack_experiments', $experiments); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | return $alt->{alternative}{name}; | |||||||||||||||||||||||||||
| 127 | 2 | }; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 convert | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Convert a user. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Provide the experiment and (optional) a KPI to track conversion on. | ||||||||||||||||||||||||||||
| 134 | If the KPI doesn't exist yet, it will be created. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | When no experiment name is given, we try to fetch the experiments | ||||||||||||||||||||||||||||
| 137 | from the user's session and convert on all of the found experiments. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Returns a hashref with { 'experiment' => 'status' } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | register convert => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 144 | 2 | my ($dsl, $experiment, $kpi) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 2 | my %return; | |||||||||||||||||||||||||||
| 147 | 2 | my $sixpack = $dsl->get_sixpack(); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | ▶ | if( $experiment ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | # specific experiment given | ||||||||||||||||||||||||||||
| 151 | 1 | my $res = $sixpack->convert($experiment, $kpi); | |||||||||||||||||||||||||||
| 152 | 1 | $return{$experiment} = $res->{status}; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | # no experiments given, look them up | ||||||||||||||||||||||||||||
| 155 | 1 | ▶ | my $experiments = $dsl->app->session->read('sixpack_experiments') || { }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$dsl->app->session->read('sixpack_experiments') || {}
MC/DC0%0/1
$dsl->app->session->read('sixpack_experiments') || {}
$dsl->app->session->read('sixpack_experiments')
Truth table50%1/2
A
A= $dsl->app->session->read('sixpack_experiments')
| |||||||||||||||||||||||||||||
| 156 | 1 | for my $exp (keys %{$experiments}) { | |||||||||||||||||||||||||||
| 157 | 2 | my $res = $sixpack->convert($exp); | |||||||||||||||||||||||||||
| 158 | 2 | $return{$exp} = $res->{status}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | return \%return; | |||||||||||||||||||||||||||
| 163 | 2 | }; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 get_sixpack | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Internal method to construct the L<WWW::Sixpack> object. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | sub get_sixpack { | ||||||||||||||||||||||||||
|
Subroutine
get_sixpack: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 4 | my $dsl = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 4 | ▶ | $conf ||= plugin_setting(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$conf ||= plugin_setting()
MC/DC0%0/2
$conf ||= plugin_setting()
$conf
plugin_setting()
Truth table66%2/3
A ||= B
A= $confB= plugin_setting()
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 4 | my %options; | |||||||||||||||||||||||||||
| 177 | 4 | my $client_id = $dsl->app->session->read('sixpack_id'); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # need to pass info on to the sixpack object? | ||||||||||||||||||||||||||||
| 180 | 4 | ▶ | $options{host} = $conf->{host} if( defined $conf->{host} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 4 | ▶ | $options{client_id} = $client_id if( defined $client_id ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 4 | ▶ | $options{ua} = $conf->{ua} if( defined $conf->{ua} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 4 | return WWW::Sixpack->new(%options); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Menno Blom, C<< <blom at cpan.org> >> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 BUGS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Please report any bugs or feature requests to C<bug-dancer2-plugin-sixpack at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 194 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer2-Plugin-Sixpack>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 195 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Copyright 2014- Menno Blom | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 204 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | register_plugin for_versions => [ 2 ] ; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 2 | 1; # End of Dancer2::Plugin::Sixpack | |||||||||||||||||||||||||||