From b638de9b4afef623ecb26db367e91cb7e2234909 Mon Sep 17 00:00:00 2001 From: Dan Phiffer Date: Thu, 18 Jul 2024 12:05:30 -0400 Subject: [PATCH] show dom test results --- src/DomTests.php | 92 ++++++++++++++++++++++++++++++++++++++++++++-- src/Plugin.php | 35 +++--------------- src/TitleTests.php | 41 +++++++++++++++++++++ src/split-tests.js | 23 ++++++------ 4 files changed, 148 insertions(+), 43 deletions(-) diff --git a/src/DomTests.php b/src/DomTests.php index fcb49d0..6a8080f 100644 --- a/src/DomTests.php +++ b/src/DomTests.php @@ -86,11 +86,97 @@ function choose_variant($post) { $variant['click_content'] = get_field('click_content', $post->ID); } - // Increment the 'test' variable for this variant. - // $this->plugin->increment('test', $post->ID, $index); - return $variant; } + /** + * Display the split test results. + * + * @return void + */ + function show_results($post) { + $_variants = get_field('dom_variants', $post->ID); + + if (empty($_variants)) { + return; + } + + foreach ($_variants as $index => $variant) { + $_variants[$index]['description'] = count($variant['content']) . ' content changes'; + } + + $variants = [ + [ + 'name' => 'Default', + 'description' => 'No content changes' + ], + ... $_variants + ]; + + ?> + + + + + + + + + $variant) { + + $num_tests = intval($this->plugin->get_count($post->ID, 'dom', $index, 'test')); + $num_converts = intval($this->plugin->get_count($post->ID, 'dom', $index, 'convert')); + if ($num_tests > 0) { + $rate = number_format($num_converts / $num_tests * 100, 1) . '%'; + } else { + $rate = '—'; + } + + ?> + + + + + + + + +
VariantRateDescriptionTestsConversions
+ 'Default'], + ... $_variants + ]; + $results = []; + $top_rate = 0; + foreach ($variants as $index => $variant) { + $num_tests = intval($this->plugin->get_count($post_id, 'dom', $index, 'test')); + $num_converts = intval($this->plugin->get_count($post_id, 'dom', $index, 'convert')); + if ($num_tests > 0) { + $rate = $num_converts / $num_tests * 100; + if ($rate > $top_rate) { + $top_rate = $rate; + $top_index = $index; + } + $rate_str = number_format($rate, 1) . '%'; + } else { + $rate = 0; + $rate_str = '—'; + } + $results[] = "$rate_str {$variant['name']}"; + } + if (isset($top_index)) { + $results[$top_index] = "$results[$top_index]"; + } + echo implode("
\n", $results); + } } \ No newline at end of file diff --git a/src/Plugin.php b/src/Plugin.php index b8a144a..0b89d95 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -197,6 +197,8 @@ function edit_form_after_title($post) { if ($test_type == 'title') { $this->title_tests->show_results($post); + } else if ($test_type == 'dom') { + $this->dom_tests->show_results($post); } } @@ -251,7 +253,7 @@ function wp_enqueue_scripts() { $nonce = wp_create_nonce('wp_rest'); wp_localize_script('split-tests', 'split_tests', [ 'nonce' => $nonce, - 'events' => $this->increment_events, + 'onload' => $this->increment_events, 'dom' => $this->dom_tests->get_variants() ]); wp_enqueue_script('split-tests'); @@ -338,34 +340,9 @@ function manage_split_test_posts_custom_column($column, $post_id) { } $type = get_field('test_type', $post_id); if ($type == 'title') { - $target_id = get_post_meta($post_id, 'target_post_id', true); - $_variants = get_field('title_variants', $target_id); - $variants = [ - ['name' => 'Default'], - ... $_variants - ]; - $results = []; - $top_rate = 0; - foreach ($variants as $index => $variant) { - $num_tests = intval($this->get_count($post_id, 'title', $index, 'test')); - $num_converts = intval($this->get_count($post_id, 'title', $index, 'convert')); - if ($num_tests > 0) { - $rate = $num_converts / $num_tests * 100; - if ($rate > $top_rate) { - $top_rate = $rate; - $top_index = $index; - } - $rate_str = number_format($rate, 1) . '%'; - } else { - $rate = 0; - $rate_str = '—'; - } - $results[] = "$rate_str {$variant['name']}"; - } - if (isset($top_index)) { - $results[$top_index] = "$results[$top_index]"; - } - echo implode("
\n", $results); + $this->title_tests->show_results_summary($post_id); + } else if ($type == 'dom') { + $this->dom_tests->show_results_summary($post_id); } } diff --git a/src/TitleTests.php b/src/TitleTests.php index e556e24..7e92d12 100644 --- a/src/TitleTests.php +++ b/src/TitleTests.php @@ -355,6 +355,11 @@ function increment_convert_count($target_id, $variant_index) { $this->plugin->increment('convert', $split_test_id, $variant_index); } + /** + * Display the split test results. + * + * @return void + */ function show_results($post) { $target_id = get_post_meta($post->ID, 'target_post_id', true); $target_post = get_post($target_id); @@ -406,4 +411,40 @@ function show_results($post) { 'Default'], + ... $_variants + ]; + $results = []; + $top_rate = 0; + foreach ($variants as $index => $variant) { + $num_tests = intval($this->plugin->get_count($post_id, 'title', $index, 'test')); + $num_converts = intval($this->plugin->get_count($post_id, 'title', $index, 'convert')); + if ($num_tests > 0) { + $rate = $num_converts / $num_tests * 100; + if ($rate > $top_rate) { + $top_rate = $rate; + $top_index = $index; + } + $rate_str = number_format($rate, 1) . '%'; + } else { + $rate = 0; + $rate_str = '—'; + } + $results[] = "$rate_str {$variant['name']}"; + } + if (isset($top_index)) { + $results[$top_index] = "$results[$top_index]"; + } + echo implode("
\n", $results); + } } \ No newline at end of file diff --git a/src/split-tests.js b/src/split-tests.js index dbd7065..f1b97ba 100644 --- a/src/split-tests.js +++ b/src/split-tests.js @@ -3,20 +3,27 @@ window.addEventListener('load', () => { return; } - if (split_tests.events && split_tests.events.length > 0) { - fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, { + function postEvents(events) { + if (!events || events.length < 1) { + return; + } + return fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(split_tests.events) + body: JSON.stringify(events) }); - split_tests.events = []; + } + + if (split_tests.onload) { + postEvents(split_tests.onload); } if (split_tests.dom) { for (let id in split_tests.dom) { const variant = split_tests.dom[id]; + postEvents([["test", parseInt(id), variant.index]]); if (variant.noop) { continue; @@ -44,13 +51,7 @@ window.addEventListener('load', () => { } target.addEventListener('click', async e => { e.preventDefault(); - await fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify([["convert", parseInt(id), variant.index]]) - }); + await postEvents([["convert", parseInt(id), variant.index]]); window.location = e.target.getAttribute('href'); }); }