| 1 | |||||||||||
| 2 | =head1 NAME | ||||||||||
| 3 | |||||||||||
| 4 | CGI::Application::Plugin::AB - A/B Testing for CGI::Application-based applications. | ||||||||||
| 5 | |||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||
| 7 | |||||||||||
| 8 | use CGI::Application::Plugin::AB; | ||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | # Your application | ||||||||||
| 12 | sub run_mode { | ||||||||||
| 13 | my ($self) = ( @_); | ||||||||||
| 14 | |||||||||||
| 15 | my $version = $self->a_or_b(); | ||||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | =cut | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This module divides all visitors into an "A" group, or a "B" group, | ||||||||||
| 24 | and allows you to determine which set they are a member of. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | =head1 MOTIVATION | ||||||||||
| 29 | |||||||||||
| 30 | To test the effectiveness of marketing text, or similar, it is sometimes | ||||||||||
| 31 | useful to display two different versions of a web-page to visitors: | ||||||||||
| 32 | |||||||||||
| 33 | =over 8 | ||||||||||
| 34 | |||||||||||
| 35 | =item * | ||||||||||
| 36 | One version will show features and a price of $5.00 | ||||||||||
| 37 | |||||||||||
| 38 | =item * | ||||||||||
| 39 | One version will show features and a price of $10.00 | ||||||||||
| 40 | |||||||||||
| 41 | =back | ||||||||||
| 42 | |||||||||||
| 43 | To do this you must pick 50% of your visitors at random and show half | ||||||||||
| 44 | one template, and the other half the other. | ||||||||||
| 45 | |||||||||||
| 46 | Once the user signs up you can record which version of the page was displayed, | ||||||||||
| 47 | allowing you to incrementally improve your signup-rate. | ||||||||||
| 48 | |||||||||||
| 49 | This module helps you achieve this goal by automatically assigning all | ||||||||||
| 50 | visitors membership of the A-group, or the B-group. | ||||||||||
| 51 | |||||||||||
| 52 | You're expected to handle the logic of showing different templates and | ||||||||||
| 53 | recording the version the user viewed. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | |||||||||||
| 60 | package CGI::Application::Plugin::AB; | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | 1 | our $VERSION = '0.4'; | |||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | =head1 METHODS | ||||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | =head2 import | ||||||||||
| 70 | |||||||||||
| 71 | Force the C<a_or_b> method into the caller's namespace. | ||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | sub import | ||||||||||
| 75 | 1 | ▶ | { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 1 | my $pkg = shift; | |||||||||
| 77 | 1 | my $callpkg = caller; | |||||||||
| 78 | |||||||||||
| 79 | { | ||||||||||
| 80 | ## no critic | ||||||||||
| 81 | 1 | ▶ | no strict qw(refs); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | ## use critic | ||||||||||
| 83 | 1 | *{ $callpkg . '::a_or_b' } = \&a_or_b; | |||||||||
| 84 | } | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | =head2 a_or_b | ||||||||||
| 89 | |||||||||||
| 90 | Return whether the visitor to this site is in the A-group, or the B-group. | ||||||||||
| 91 | |||||||||||
| 92 | No significance is paid to which group the visitors are member of in this | ||||||||||
| 93 | module, it is expected you'll handle the logic yourself: | ||||||||||
| 94 | |||||||||||
| 95 | =over 8 | ||||||||||
| 96 | |||||||||||
| 97 | =item * | ||||||||||
| 98 | Show a different template to members of the different sets. | ||||||||||
| 99 | |||||||||||
| 100 | =item * | ||||||||||
| 101 | Record the set membership when a user signs up, etc. | ||||||||||
| 102 | |||||||||||
| 103 | =back | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | sub a_or_b | ||||||||||
| 108 | 0 | ▶ | { | ||||||||
|
Subroutine
a_or_b: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 109 | 0 | my $cgi_app = shift; | |||||||||
| 110 | |||||||||||
| 111 | # | ||||||||||
| 112 | # Get the IP and strip non-digits. | ||||||||||
| 113 | # | ||||||||||
| 114 | 0 | my $addr = $ENV{ 'REMOTE_ADDR' }; | |||||||||
| 115 | 0 | $addr =~ s/\D//g; | |||||||||
| 116 | |||||||||||
| 117 | # | ||||||||||
| 118 | # Sum the digits | ||||||||||
| 119 | # | ||||||||||
| 120 | 0 | my $sum = 0; | |||||||||
| 121 | 0 | for my $c (split //, $addr) | |||||||||
| 122 | { | ||||||||||
| 123 | 0 | $sum += $c; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | # | ||||||||||
| 127 | # Odd == a. | ||||||||||
| 128 | # Even == b. | ||||||||||
| 129 | 0 | ▶ | return( "A" ) if ( $sum % 2 == 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | ▶ | return( "B" ) if ( $sum % 2 == 1 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | |||||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =head1 AUTHOR | ||||||||||
| 138 | |||||||||||
| 139 | Steve Kemp <steve@steve.org.uk> | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 144 | |||||||||||
| 145 | Copyright (C) 2014 Steve Kemp <steve@steve.org.uk>. | ||||||||||
| 146 | |||||||||||
| 147 | This library is free software. You can modify and or distribute it under | ||||||||||
| 148 | the same terms as Perl itself. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | |||||||||||
| 153 | |||||||||||
| 154 | 1 | 1; | |||||||||