added on local at 2026-07-01 16:01:28
| 1 | package MODS::ABForge::Onboarding; | |
| 2 | #====================================================================== | |
| 3 | # ABForge -- new-user sample data seeder | |
| 4 | # | |
| 5 | # Called from signup.cgi. Creates a sample site + a running A/B test | |
| 6 | # with two variants + lightly-pre-populated exposures/conversions so | |
| 7 | # the dashboard isn't empty. Idempotent. | |
| 8 | #====================================================================== | |
| 9 | use strict; use warnings; | |
| 10 | ||
| 11 | sub new { return bless {}, shift } | |
| 12 | ||
| 13 | sub seed_sample_data { | |
| 14 | my ($self, $db, $dbh, $DB, $user_id) = @_; | |
| 15 | return unless $user_id; | |
| 16 | ||
| 17 | # Skip if user already has a site | |
| 18 | my $r = $db->db_readwrite($dbh, qq~ | |
| 19 | SELECT COUNT(*) AS n FROM `${DB}`.sites WHERE user_id='$user_id' | |
| 20 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 21 | return if $r && $r->{n}; | |
| 22 | ||
| 23 | # Generate stable per-user tokens. | |
| 24 | my $pub = _rand_hex(20); | |
| 25 | my $sec = _rand_hex(40); | |
| 26 | ||
| 27 | # Sample site | |
| 28 | my $r1 = $db->db_readwrite($dbh, qq~ | |
| 29 | INSERT INTO `${DB}`.sites | |
| 30 | SET user_id='$user_id', | |
| 31 | name='Demo Site (example)', | |
| 32 | domain='demo.example.com', | |
| 33 | public_token='$pub', | |
| 34 | secret_token='$sec', | |
| 35 | timezone='UTC', currency='USD', | |
| 36 | enable_heatmaps=1 | |
| 37 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 38 | my $sid = ($r1 && $r1->{mysql_insertid}) ? $r1->{mysql_insertid} : 0; | |
| 39 | return unless $sid; | |
| 40 | ||
| 41 | # Sample goal | |
| 42 | my $g = $db->db_readwrite($dbh, qq~ | |
| 43 | INSERT INTO `${DB}`.goals | |
| 44 | SET site_id='$sid', name='Signup completed', goal_type='event_count', | |
| 45 | event_name='signup', is_active=1 | |
| 46 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 47 | my $gid = ($g && $g->{mysql_insertid}) ? $g->{mysql_insertid} : 0; | |
| 48 | ||
| 49 | # Sample running test | |
| 50 | my $goal_sql = $gid ? "'$gid'" : 'NULL'; | |
| 51 | my $t = $db->db_readwrite($dbh, qq~ | |
| 52 | INSERT INTO `${DB}`.ab_tests | |
| 53 | SET site_id='$sid', name='Pricing page CTA (example)', | |
| 54 | description='Example A/B test -- delete or pause. Drop the real one in.', | |
| 55 | test_type='ab', target_url_match='contains', target_url_value='/pricing', | |
| 56 | traffic_pct=100, goal_id=$goal_sql, | |
| 57 | primary_metric='conversion_rate', | |
| 58 | status='running', | |
| 59 | exposures_total=2400, conversions_total=146, | |
| 60 | started_at=DATE_SUB(NOW(), INTERVAL 7 DAY) | |
| 61 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 62 | my $tid = ($t && $t->{mysql_insertid}) ? $t->{mysql_insertid} : 0; | |
| 63 | return unless $tid; | |
| 64 | ||
| 65 | # Two variants: Control + Variant B (Variant B winning, no autowinner yet) | |
| 66 | $db->db_readwrite($dbh, qq~ | |
| 67 | INSERT INTO `${DB}`.ab_variants | |
| 68 | SET ab_test_id='$tid', name='Control', is_control=1, traffic_pct=50, | |
| 69 | changes='Original "Get Started" button copy.', | |
| 70 | exposures=1198, conversions=58, sort_order=0 | |
| 71 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 72 | $db->db_readwrite($dbh, qq~ | |
| 73 | INSERT INTO `${DB}`.ab_variants | |
| 74 | SET ab_test_id='$tid', name='Variant B - urgency copy', | |
| 75 | is_control=0, traffic_pct=50, | |
| 76 | changes='CTA changed to "Start Free Trial - 14 days, no card".', | |
| 77 | exposures=1202, conversions=88, sort_order=1 | |
| 78 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 79 | ||
| 80 | return 1; | |
| 81 | } | |
| 82 | ||
| 83 | sub _rand_hex { | |
| 84 | my ($len) = @_; | |
| 85 | if (open my $fh, '<', '/dev/urandom') { | |
| 86 | my $buf; read($fh, $buf, int(($len+1)/2)); close $fh; | |
| 87 | return substr(unpack('H*', $buf), 0, $len); | |
| 88 | } | |
| 89 | my $out = ''; | |
| 90 | while (length($out) < $len) { $out .= sprintf('%08x', int(rand(0xFFFFFFFF))); } | |
| 91 | return substr($out, 0, $len); | |
| 92 | } | |
| 93 | ||
| 94 | 1; |